Adding Colours (Colors) Together like Paint (Blue + Yellow = Green, etc)

前端 未结 5 1715
挽巷
挽巷 2021-01-05 14:32

I\'m making an iOS game using cocos2d libraries.

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

Blue:    0,0,255
Ye         


        
相关标签:
5条回答
  • 2021-01-05 15:09

    I think, it is worth to try HSL color space. When adding colors we interpolate their Hue values (even taking in account objects weights). If the colors are 100% saturated, Luminance and Saturation values will be equal.

    0 讨论(0)
  • 2021-01-05 15:15

    Actually it seems that converting RGB->XYZ->LAB does exatly the same thing as RGB->LAB

    0 讨论(0)
  • 2021-01-05 15:18

    Dyes don't work in the real world quite like subtractive-color models suggests. The dyes used for CYMK printing are pretty close, since they're formulated for that purpose, but many dyes made from naturally-occurring substances can behave somewhat oddly. The difficulty is that while white light is perceived as a combination of red, green, and blue, it actually consists of many different wavelengths--literally "all the colors of the rainbow"--each of which will stimulate the red, green, and blue receptors in the eye by different amounts. It is possible for two colors which appear identical to in fact contain different combinations of wavelengths; likewise, two dyes may appear identical when viewed in white light, but absorb different combinations of wavelengths. Such dyes may look identical to each other when used alone, but may yield very different-seeming results when combined with something else.

    Although dyes can sometimes be tricky, however, paints are even worse. Paints contain reflective particles, and some of the light which hits a painted surface will be reflected back off the surface by the first particle it hits; in that regard, they mix like additive colors. For example, if the paint contains 20% green particles, then a significant amount of green light will be reflected, regardless of what other colors it might contain. On the other hand, some of the light which hits a painted surface will bounce around and hit multiple particles. If any of those particles absorbs a photon of some color, that photon won't be reflected. In that regard, paints behave more like subtractive colors. In practice, paints behave somewhat like additive colors, somewhat like subtractive colors, and sometimes like something weird and wacky and totally unlike either.

    0 讨论(0)
  • 2021-01-05 15:19

    Check the formulas on this site: http://www.easyrgb.com/index.php?X=MATH I've been doing similar thing, and it can be achieved by converting RGB->XYZ->Lab. However the computation is quite expensive(if you doing it for a lot of pixels).

    And forget about RGB math when trying to mix colors if you want to obtain results similar to human eye

    0 讨论(0)
  • 2021-01-05 15:29

    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]);
    

    Running the code will yield: RGB mix = (0.000000, 255.000000, 0.000000)

    0 讨论(0)
提交回复
热议问题