[英]How to multiply two colors in javascript?
The given parameters are r,g,b of two colors. 给定的参数是两种颜色的r,g,b。
How can I multiply them? 我怎样才能将它们相乘? (Like blending mode->multiply in Photoshop)
(像混合模式 - >在Photoshop中相乘)
Example: 例:
color1:0,255,255 颜色1:0,255,255
color2:255,255,0 COLOR2:255,255,0
multiplied:0,255,0 乘:0,255,0
Based on the simple multiply formula, here is a javascript function that should work for RGB: 基于简单的乘法公式,这里是一个适用于RGB的javascript函数:
function multiply(rgb1, rgb2) {
var result = [],
i = 0;
for( ; i < rgb1.length; i++ ) {
result.push(Math.floor(rgb1[i] * rgb2[i] / 255));
}
return result;
}
Using modern JavaScript: 使用现代JavaScript:
const multiply = (rgb1, rgb2) => rgb1.map((c, i) => Math.floor(c * rgb2[i] / 255))
Here is a fiddle using background colors you can play with: http://jsfiddle.net/unrLC/ 这是一个使用你可以玩的背景颜色的小提琴: http : //jsfiddle.net/unrLC/
Here's the formula for multiplying colors (as well as other blend mode formulas). 这是乘法颜色的公式 (以及其他混合模式公式)。
Formula: Result Color = (Top Color) * (Bottom Color) /255
公式:结果颜色=(顶部颜色)*(底部颜色)/ 255
You can implement this very easily in JavaScript. 您可以在JavaScript中轻松实现此功能。
var newRed = red1 * red2 / 255;
var newGreen = green1 * green2 / 255;
var newBlue = blue1 * blue2 / 255;
You translate the color components to a value between 0 and 1, then it's simple multiplication. 您将颜色分量转换为0到1之间的值,然后是简单的乘法。 Thanslate the result back to the range 0 to 255:
将结果分解回0到255的范围:
0, 255, 255 -> 0, 1, 1
255, 255, 0 -> 1, 1, 0
0*1 = 0, 1*1 = 1, 1*0 = 0
0, 1, 0 -> 0, 255, 0
I guess the aim is to multiply color ranges from 0 to 255. Reduce them to a range of [0, 1], multiply them, and expand them again: 我想目标是将颜色范围从0增加到255.将它们减小到[0,1]的范围,将它们相乘,然后再次展开它们:
color_new = ( (color_1 / 255) * (color_2 / 255) ) * 255
color_new = color_1 * color_2 / 255
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.