我正在尝试编写一个函数来计算给定前景色和背景色的柔光。 函数如下;
var background = '0xFFFFFF';
var foreground = '0x47768C';
var calculateSoftlight = function (background, foreground) {
var intBackground = parseInt(background, 16);
var intForeground = parseInt(foreground, 16);
var softlight = (1 - (2 * intForeground)) * (intBackground*intBackground) + (2 * intForeground * intBackground);
return softlight.toString(16);
}
calculateSoftlight(background, foreground); //-8eed155338bb200000
我正在使用此处列出的 Pegtop 公式; http://en.wikipedia.org/wiki/Blend_modes 。我不确定这个的正确实现。有什么想法吗?
最佳答案
将公式应用于每个 RGB 值,而不是使用十六进制。如果您需要使用十六进制作为输入,您可能需要进行转换。
您需要标准化每个值(例如值/255
)并在公式中使用它。然后将结果乘以(并舍入)255 以转换回 8 位值。
这样的东西应该很接近,但我没有专门使用该公式,所以这还没有经过测试。
var top = top / 255,
bot = bot / 255;
top = ((1 - 2*bot)*Math.pow(top, 2)) + 2*bot*top,
top = Math.round(top * 255);
关于javascript - 柔光混合模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26684908/