添加颜色(颜色)在一起像油漆(蓝色+黄色=绿色等) [英] Adding Colours (Colors) Together like Paint (Blue + Yellow = Green, etc)

查看:215
本文介绍了添加颜色(颜色)在一起像油漆(蓝色+黄色=绿色等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

过于复杂的事情,让我们说Blue对象大于黄色对象(为了参数的缘故,我们说比例是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 * Color Space对于这种自然色彩的东西很有用,但我不知道如何使用它 - 特别是在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 *蓝色+黄色≠绿色(这是有意义的,当你看到它们位于同一个频道的两端。这实际上是一个棘手的问题与 关于SO的讨论。似乎最终的答案是使用Kubelka-Munk方法,一个名为Krita的开源软件。我找不到任何地方(公式或代码本身)。

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 )

这篇关于添加颜色(颜色)在一起像油漆(蓝色+黄色=绿色等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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