实现photoshop 颜色混合的shader

2024-01-05 19:58

本文主要是介绍实现photoshop 颜色混合的shader,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这个过程很是艰辛呐!

国内博客查找到的说明基本上都是这样
颜色模式:

是采用底色的亮度以及绘图色的色相、饱和度来创建最终色。它可保护原图的灰阶层次,对于图像的色彩微调、给单色和彩色图像着色都非常有用。
Color 颜色
HcScYc =HASAYB
输出图像的亮度为下层,色调和饱和度保持为上层

甚至wiki说明也是这样:

The Color blend mode preserves the luma of the bottom layer, while adopting the hue and chroma of the top layer.

我费心的去了解查找HSL,HSV到RGB之前的转化,并去验证转化是否有错误。总之最后结果都不对!
最后通过颜色坐标系在线转化工具colorizer发现颜色混合并不是像上面说的那样计算。

查找photoshop混合的文档,但是没有找到文档,自己控制单一变量记录混合后的值,甚至想学用matlab绘制函数曲线。

下班终于找到一个老外跟我遇到相同的问题了,Fuck呀。

  • It does not convert both images to the YUV colourspace, then combine the Y (luminance) channel of one image with the U and V (chrominance) channels of the second. 根本不需要YUV转换
  • It does not involve a similar transformation/combination in the HSL (hue/saturation/lightness), or HSV (hue/saturation/value) colourspaces[1].根本不需要HSL或HSV转换

  • In fact, it does not involve a simple combination of one channel from one image with the other channels of a second image in any standard colorspace.根本不需要图像在任何标准颜色坐标系中颜色通道的转换

我之前把无关HSV HSL颜色转化的的答案都过滤掉了,事实上那些才是正确的。

Adobe’s PDF specification (specifically, PDF Blend Modes: Addendum) describes the colour blend mode as a set of three functions, which I’ve paraphrased here as pseudocode (but not the same pseudocode as in the original document), with pixel being a struct of three floating point values (Red, Green, and Blue)

float Lum(pixel colour){return (colour.Red * 0.3) + (colour.Green * 0.59) + (colour.Blue * 0.11);}pixel ClipColour(pixel colour){pixel result;float luminance = Lum(colour);float cMin = min(colour.Red, colour.Green, colour.Blue);float cMax = max(colour.Red, colour.Green, colour.Blue);if (cMin < 0.0){result.Red = luminance + (((colour.Red - luminance) * luminance) / (luminance - cMin));result.Green = luminance + (((colour.Green - luminance) * luminance) / (luminance - cMin));result.Blue = luminance + (((colour.Blue - luminance) * luminance) / (luminance - cMin));}if (cMax > 1.0){result.Red = luminance + (((colour.Red - luminance) * (1.0 - luminance)) / (cMax - luminance));result.Green = luminance + (((colour.Green - luminance) * (1.0 - luminance)) / (cMax - luminance));result.Blue = luminance + (((colour.Blue - luminance) * (1.0 - luminance)) / (cMax - luminance));}return result;}pixel SetLum(pixel colour, float luminance){pixel result;float diff = luminance - Lum(colour);result.Red = colour.Red + diff;result.Green = colour.Green + diff;result.Blue = colour.Blue + diff;return ClipColour(color);
//注:这里应该是ClipColour(result)}

我改成了python的便于查看数值正确性:

class vec3:r=0g=0b=0def __init__(self,*para):self.r = 0self.g = 0self .b =0pLen = len(para)if pLen > 0:self.r = para[0]if pLen > 1:self.g = para[1]if pLen > 2:self.b = para[2]def __str__(self):return '['+str(self.r) + " " + str(self.g) + " "+ str(self.b)+']'def toNormal(self):self.r = self.r/255.0self.g = self.g/255.0self.b = self.b/255.0return selfdef toRGB(self):self.r = int(self.r*255.0)self.g = int(self.g*255.0)self.b = int(self.b*255.0)return selfdef lumF(color):return (color.r * 0.3) + (color.g * 0.59) + (color.b * 0.11)def clipColourVec3(color):result = colorluminance = lumF(color)cMin = min(color.r, color.g, color.b)cMax = max(color.r, color.g, color.b)if cMin < 0.0:result.r = luminance + (((color.r - luminance) * luminance) / (luminance - cMin))result.g = luminance + (((color.g - luminance) * luminance) / (luminance - cMin))result.b = luminance + (((color.b - luminance) * luminance) / (luminance - cMin))if cMax > 1.0:result.r = luminance + (((color.r - luminance) * (1.0 - luminance)) / (cMax - luminance))result.g = luminance + (((color.g - luminance) * (1.0 - luminance)) / (cMax - luminance))result.b = luminance + (((color.b - luminance) * (1.0 - luminance)) / (cMax - luminance))return result.toRGB()def colorBlend(colorFront,colorBack):result = vec3(0)frontLum = lumF(colorFront)backLum = lumF(colorBack) diff = backLum - frontLumresult.r = colorFront.r + diffresult.g = colorFront.g + diffresult.b = colorFront.b + diffreturn clipColourVec3(result)colorFront = vec3(198,169,79).toNormal()
colorBack = vec3(70,90,163).toNormal()finalBlend = colorBlend(colorFront,colorBack)
print finalBlend

附老外很久以前实现的ps上的混合shader,有些可能已经跟当前效果对不上了。比如该篇博客color blend

** Copyright (c) 2012, Romain Dura romain@shazbits.com
** 
** Permission to use, copy, modify, and/or distribute this software for any 
** purpose with or without fee is hereby granted, provided that the above 
** copyright notice and this permission notice appear in all copies.
** 
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
** WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
** MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 
** SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
** WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
** ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR 
** IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*//*
** Photoshop & misc math
** Blending modes, RGB/HSL/Contrast/Desaturate, levels control
**
** Romain Dura | Romz
** Blog: http://mouaif.wordpress.com
** Post: http://mouaif.wordpress.com/?p=94
*//*
** Desaturation
*/vec4 Desaturate(vec3 color, float Desaturation)
{vec3 grayXfer = vec3(0.3, 0.59, 0.11);vec3 gray = vec3(dot(grayXfer, color));return vec4(mix(color, gray, Desaturation), 1.0);
}/*
** Hue, saturation, luminance
*/vec3 RGBToHSL(vec3 color)
{vec3 hsl; // init to 0 to avoid warnings ? (and reverse if + remove first part)float fmin = min(min(color.r, color.g), color.b);    //Min. value of RGBfloat fmax = max(max(color.r, color.g), color.b);    //Max. value of RGBfloat delta = fmax - fmin;             //Delta RGB valuehsl.z = (fmax + fmin) / 2.0; // Luminanceif (delta == 0.0)       //This is a gray, no chroma...{hsl.x = 0.0;    // Huehsl.y = 0.0;    // Saturation}else                                    //Chromatic data...{if (hsl.z < 0.5)hsl.y = delta / (fmax + fmin); // Saturationelsehsl.y = delta / (2.0 - fmax - fmin); // Saturationfloat deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta;float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta;float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta;if (color.r == fmax )hsl.x = deltaB - deltaG; // Hueelse if (color.g == fmax)hsl.x = (1.0 / 3.0) + deltaR - deltaB; // Hueelse if (color.b == fmax)hsl.x = (2.0 / 3.0) + deltaG - deltaR; // Hueif (hsl.x < 0.0)hsl.x += 1.0; // Hueelse if (hsl.x > 1.0)hsl.x -= 1.0; // Hue}return hsl;
}float HueToRGB(float f1, float f2, float hue)
{if (hue < 0.0)hue += 1.0;else if (hue > 1.0)hue -= 1.0;float res;if ((6.0 * hue) < 1.0)res = f1 + (f2 - f1) * 6.0 * hue;else if ((2.0 * hue) < 1.0)res = f2;else if ((3.0 * hue) < 2.0)res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;elseres = f1;return res;
}vec3 HSLToRGB(vec3 hsl)
{vec3 rgb;if (hsl.y == 0.0)rgb = vec3(hsl.z); // Luminanceelse{float f2;if (hsl.z < 0.5)f2 = hsl.z * (1.0 + hsl.y);elsef2 = (hsl.z + hsl.y) - (hsl.y * hsl.z);float f1 = 2.0 * hsl.z - f2;rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0));rgb.g = HueToRGB(f1, f2, hsl.x);rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0));}return rgb;
}/*
** Contrast, saturation, brightness
** Code of this function is from TGM's shader pack
** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057
*/// For all settings: 1.0 = 100% 0.5=50% 1.5 = 150%
vec3 ContrastSaturationBrightness(vec3 color, float brt, float sat, float con)
{// Increase or decrease theese values to adjust r, g and b color channels seperatelyconst float AvgLumR = 0.5;const float AvgLumG = 0.5;const float AvgLumB = 0.5;const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721);vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB);vec3 brtColor = color * brt;vec3 intensity = vec3(dot(brtColor, LumCoeff));vec3 satColor = mix(intensity, brtColor, sat);vec3 conColor = mix(AvgLumin, satColor, con);return conColor;
}/*
** Float blending modes
** Adapted from here: http://www.nathanm.com/photoshop-blending-math/
** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results
*/#define BlendLinearDodgef           BlendAddf
#define BlendLinearBurnf            BlendSubstractf
#define BlendAddf(base, blend)      min(base + blend, 1.0)
#define BlendSubstractf(base, blend)    max(base + blend - 1.0, 0.0)
#define BlendLightenf(base, blend)      max(blend, base)
#define BlendDarkenf(base, blend)       min(blend, base)
#define BlendLinearLightf(base, blend)  (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5))))
#define BlendScreenf(base, blend)       (1.0 - ((1.0 - base) * (1.0 - blend)))
#define BlendOverlayf(base, blend)  (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)))
#define BlendSoftLightf(base, blend)    ((blend < 0.5) ? (2.0 * base * blend + base * base * (1.0 - 2.0 * blend)) : (sqrt(base) * (2.0 * blend - 1.0) + 2.0 * base * (1.0 - blend)))
#define BlendColorDodgef(base, blend)   ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0))
#define BlendColorBurnf(base, blend)    ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0))
#define BlendVividLightf(base, blend)   ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5))))
#define BlendPinLightf(base, blend)     ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5))))
#define BlendHardMixf(base, blend)  ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0)
#define BlendReflectf(base, blend)      ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0))/*
** Vector3 blending modes
*/// Component wise blending
#define Blend(base, blend, funcf)       vec3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b))#define BlendNormal(base, blend)        (blend)
#define BlendLighten                BlendLightenf
#define BlendDarken             BlendDarkenf
#define BlendMultiply(base, blend)      (base * blend)
#define BlendAverage(base, blend)       ((base + blend) / 2.0)
#define BlendAdd(base, blend)       min(base + blend, vec3(1.0))
#define BlendSubstract(base, blend)     max(base + blend - vec3(1.0), vec3(0.0))
#define BlendDifference(base, blend)    abs(base - blend)
#define BlendNegation(base, blend)  (vec3(1.0) - abs(vec3(1.0) - base - blend))
#define BlendExclusion(base, blend)     (base + blend - 2.0 * base * blend)
#define BlendScreen(base, blend)        Blend(base, blend, BlendScreenf)
#define BlendOverlay(base, blend)       Blend(base, blend, BlendOverlayf)
#define BlendSoftLight(base, blend)     Blend(base, blend, BlendSoftLightf)
#define BlendHardLight(base, blend)     BlendOverlay(blend, base)
#define BlendColorDodge(base, blend)    Blend(base, blend, BlendColorDodgef)
#define BlendColorBurn(base, blend)     Blend(base, blend, BlendColorBurnf)
#define BlendLinearDodge            BlendAdd
#define BlendLinearBurn         BlendSubstract
// Linear Light is another contrast-increasing mode
// If the blend color is darker than midgray, Linear Light darkens the image by decreasing the brightness. If the blend color is lighter than midgray, the result is a brighter image due to increased brightness.
#define BlendLinearLight(base, blend)   Blend(base, blend, BlendLinearLightf)
#define BlendVividLight(base, blend)    Blend(base, blend, BlendVividLightf)
#define BlendPinLight(base, blend)      Blend(base, blend, BlendPinLightf)
#define BlendHardMix(base, blend)       Blend(base, blend, BlendHardMixf)
#define BlendReflect(base, blend)       Blend(base, blend, BlendReflectf)
#define BlendGlow(base, blend)      BlendReflect(blend, base)
#define BlendPhoenix(base, blend)       (min(base, blend) - max(base, blend) + vec3(1.0))
#define BlendOpacity(base, blend, F, O)     (F(base, blend) * O + blend * (1.0 - O))// Hue Blend mode creates the result color by combining the luminance and saturation of the base color with the hue of the blend color.
vec3 BlendHue(vec3 base, vec3 blend)
{vec3 baseHSL = RGBToHSL(base);return HSLToRGB(vec3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b));
}// Saturation Blend mode creates the result color by combining the luminance and hue of the base color with the saturation of the blend color.
vec3 BlendSaturation(vec3 base, vec3 blend)
{vec3 baseHSL = RGBToHSL(base);return HSLToRGB(vec3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b));
}// Color Mode keeps the brightness of the base color and applies both the hue and saturation of the blend color.
vec3 BlendColor(vec3 base, vec3 blend)
{vec3 blendHSL = RGBToHSL(blend);return HSLToRGB(vec3(blendHSL.r, blendHSL.g, RGBToHSL(base).b));
}// Luminosity Blend mode creates the result color by combining the hue and saturation of the base color with the luminance of the blend color.
vec3 BlendLuminosity(vec3 base, vec3 blend)
{vec3 baseHSL = RGBToHSL(base);return HSLToRGB(vec3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b));
}/*
** Gamma correction
** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/
*/#define GammaCorrection(color, gamma)                               pow(color, 1.0 / gamma)/*
** Levels control (input (+gamma), output)
** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/
*/#define LevelsControlInputRange(color, minInput, maxInput)              min(max(color - vec3(minInput), vec3(0.0)) / (vec3(maxInput) - vec3(minInput)), vec3(1.0))
#define LevelsControlInput(color, minInput, gamma, maxInput)                GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma)
#define LevelsControlOutputRange(color, minOutput, maxOutput)           mix(vec3(minOutput), vec3(maxOutput), color)
#define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput)   LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput)

Over,道路曲折希望遇到该问题的人少走弯路。


参考链接:
1. Secrets_of_Photoshops_Colour_Blend
2. photoshops-color-blend-mode
3. 我的小屋

这篇关于实现photoshop 颜色混合的shader的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/573996

相关文章

将java程序打包成可执行文件的实现方式

《将java程序打包成可执行文件的实现方式》本文介绍了将Java程序打包成可执行文件的三种方法:手动打包(将编译后的代码及JRE运行环境一起打包),使用第三方打包工具(如Launch4j)和JDK自带... 目录1.问题提出2.如何将Java程序打包成可执行文件2.1将编译后的代码及jre运行环境一起打包2

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

MySQL8.0设置redo缓存大小的实现

《MySQL8.0设置redo缓存大小的实现》本文主要在MySQL8.0.30及之后版本中使用innodb_redo_log_capacity参数在线更改redo缓存文件大小,下面就来介绍一下,具有一... mysql 8.0.30及之后版本可以使用innodb_redo_log_capacity参数来更改

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque