为什么我的混色功能无法按预期工作? [英] Why won't my color mixing function work as expected?

查看:72
本文介绍了为什么我的混色功能无法按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个参数的函数。它们是 [r,g,b] 格式的数组。

I have this function that takes two arguments. They are arrays in the [r, g, b] format.

function mix(color1, color2) 
{
    var r = Math.round((color1[0] + color2[0])/2);
    var g = Math.round((color1[1] + color2[1])/2);
    var b = Math.round((color1[2] + color2[2])/2);

    return [r, g, b];
}

如果我尝试混合红色(255、0、0)和蓝色( 0,0,255),tt给我 [128,0,128] ,它是紫色的。但是如果我尝试混合蓝色(0,0,255)和黄色(255,255,0)

If I try to mix red (255, 0, 0) and blue (0, 0, 255), tt gives me [128,0,128], which is purple. But if I try mixing blue (0, 0, 255) and yellow (255, 255, 0)

console.log(mix([255,0,0], [0,0,255]));
console.log(mix([255,255,0], [0,0,255]));

它给了我灰色 [128,128,128] ,而不是绿色。为什么会发生这种情况?

it gives me gray [128, 128, 128], instead of green. Why is this happening?

推荐答案

因为您要计算的结果颜色是两种基色的算术平均值。

Because you are calculating resulting color as the arythmetic average of two base colors.

根据混合的颜色,颜色以不同的方式工作。如果您混合使用许多不同颜色的涂料,则结果将是深色,接近黑色。但是,如果您混合使用不同颜色的灯光,结果将接近白色。首先定义您要模拟的过程-我敢打赌,这类似于混合涂料,对吗?

Colors work in a different way depending on what you mix. If you would mix paints of many different colors, the result would be dark, nearly black. But if you would mix lights of different colors, the result would be nearly white. First define the process you want to simulate - I bet it is the one that is similar to mixing paints, correct?

两种混合颜色的基本方法是:

Two basic methods of mixing colors are:


  • 减法混合,如下所示:

  • subtractive mixing, which looks like that:

添加剂混合,看起来像那样:

additive mixing, which looks like that:

您需要的是减色混色,其输出应为 RGB 符号。

What you need is subtractive color mixing, whose output should be in RGB notation.

这篇关于为什么我的混色功能无法按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆