我正在尝试编写一个函数来计算给定前景和背景颜色的柔和光线。 功能如下;
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。我不确定这是否正确实施。有什么想法吗?
答案 0 :(得分:1)
将公式应用于每个RGB值,而不是使用Hex。如果你需要使用Hex作为输入,你可能需要转换。
您需要规范化每个值(所以value / 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);