像油漆一样添加颜色(颜色)(蓝色+黄色=绿色等)颜色、油漆、黄色、蓝色

2023-09-06 09:26:15 作者:亮瞎你的小眼睛

我正在使用 cocos2d 库制作 iOS 游戏.

I'm making an iOS game using cocos2d libraries.

假设您有两个具有两种不同颜色的对象 - 在 RGB 中定义为

Lets say you have two objects that have two separate colours - defined in RGB as

Blue:    0,0,255
Yellow:  255,255,0

我想加蓝色和黄色变成绿色.

I want to add blue and yellow to make green.

为了使事情过于复杂,假设蓝色对象比黄色对象大(为了论证,假设比例是 2:1),我添加的蓝色是黄色的两倍 - 如何我正确计算了这个新的(浅绿色)颜色.

To over complicate things, let's say that the Blue object is bigger than the Yellow object (for the sake of argument let's say that the ratio is 2:1), I'm adding twice as much blue as yellow - how to I calculate this new (light green) colour correctly.

我了解 LAB * 颜色空间对于这种自然颜色"类型的东西很有用,但我不知道如何使用它——尤其是在 cocos2d 对象的上下文中(AFAIK)仅限于使用RGB 的配色方案.

I understand LAB * Color Space is useful for this sort of 'natural colour' kind of thing, but I'm not sure how to use it - especially in the context of a cocos2d object which (AFAIK) is limited to using RGB in its colour schemes.

非常感谢有关如何实现此功能的实际帮助.多谢!

I'd really appreciate practical help on how to implement this. Thanks heaps!

21/4 更新:所以在 LAB* 中蓝色+黄色 ≠ 绿色(当您看到 他们在同一个频道的两端).小 bit 关于 SO 的讨论.似乎最终的答案是使用一个名为 Krita 的开源软件使用的 Kubelka-Munk 方法.我在任何地方都找不到(公式或代码本身).

21/4 Update: So in LAB* blue+yellow ≠ green (which makes sense when you see they're at opposite ends of the same channel). It's actually quite a tricky problem with a little bit of discussion on SO. It seems that the ultimate answer is to use the Kubelka-Munk method that a piece of open source software called Krita uses. I can't find that anywhere (either the formula or the code itself).

这个问题有一个链接,它使用 HSL 以类似于绘画的方法工作.我会尝试看看它是否有效,我会在这里反馈结果.

This question has a link which uses HSL to work in a similar method to paint. I'm going to try to see if it works, and I'll feed back the result here.

同时如果有人知道如何实现 Kubelka-Munk 或者我可以在哪里找到代码来做到这一点,或者其他解决方案,我会非常非常激动!

In the meantime if anyone knows how to implement Kubelka-Munk or where I can find code to do this, or another solution, I would be very, very stoked!

推荐答案

没有将蓝色和黄色混合成绿色的颜色模型.自己试试水粉画,唯一有效的方法是青色和黄色.这就是为什么您应该尝试从 RGB 切换到 CMYK,并在需要时返回.这是它的完成方式

There is no color model where mixing blue and yellow makes green. Try it yourself with gouache, the only way it works is cyan and yellow. This is why you should try switching from RGB to CMYK, and back if you need. Here is how it's done

void toCMYK(float red, float green, float blue, float* cmyk)
{
  float k = MIN(255-red,MIN(255-green,255-blue));
  float c = 255*(255-red-k)/(255-k); 
  float m = 255*(255-green-k)/(255-k); 
  float y = 255*(255-blue-k)/(255-k); 

  cmyk[0] = c;
  cmyk[1] = m;
  cmyk[2] = y;
  cmyk[3] = k;
}

void toRGB(float c, float m, float y, float k, float *rgb)
{
  rgb[0] = -((c * (255-k)) / 255 + k - 255);
  rgb[1] = -((m * (255-k)) / 255 + k - 255);
  rgb[2] = -((y * (255-k)) / 255 + k - 255);
}

然后在你的代码中,混合青色和黄色

And then in your code, mix the cyan and yellow

float cmyk1[4];
toCMYK(255, 255, 0, cmyk1);  // yellow

float cmyk2[4];
toCMYK(0, 255, 255, cmyk2);  // cyan

// Mixing colors is as simple as adding
float cmykMix[] = { cmyk1[0] + cmyk2[0], cmyk1[1] + cmyk2[1], cmyk1[2] + cmyk2[2], cmyk1[3] + cmyk2[3] };

float rgb[3];
toRGB(cmykMix[0], cmykMix[1], cmykMix[2], cmykMix[3], rgb);  

NSLog(@"RGB mix = (%f, %f, %f)", rgb[0], rgb[1], rgb[2]);

运行代码将产生:RGB mix = (0.000000, 255.000000, 0.000000)