I am looking for a library/algorithm/technique to take the 3 additive or subtractive colors and determine what combination of two results in some color x.
This is not a correct example (as I don't know much about color theory yet), but this is to illustrate the point. Say you have a color like greenish brown #45512b. The problem to solve is figure out -- for the two systems (additive and subtractive) -- the pair of colors that will generate greenish brown (the given color).
So you have the value "greenish brown" #45512b, and want to know what paints mix together to form this color. Maybe it is #fff123 and #bbb456 that combine subtractively to form greenish brown (just making this up). Then for additive mixing, figuring out the two shades of light of the 3 base colors of light used in computational models that combine to form greenish brown.
Basically just looking for this:
function additiveComponents(color) {
return [ a, b ]
}
function subtractiveComponents(color) {
return [ a, b ]
}
The input color doesn't need to be in hex format, any of the color spaces would work. But in the end I would like to convert the two output values back to a hex value.
One naive way I could imagine doing this is to take the reverse algorithm (algorithm to mix two colors into a third color), and try every combination of colors until you find the right one. But that would be super inefficient, wondering if there is a better way or standard technique.
use RGB color model. I am assuming you got 8 bit per channel color
c0=(r0,g0,b0)and want to knowc1,c2that mix back toc0.Additive mixing (light sources)
chose one color
c1to be able to mix up to
c0thec1must be less or equal toc0on per channel bases. So for example random lesser color:compute the missing color
c2simply use the additive logic:
so
Substractive mixing (filters, paint colors)
chose one color
c1this time
c1must bec1>=c0so again random such color example:compute the missing color
c2simply use the substractive logic:
so
[Edit1] example
I just encoded a simple C++/VCL app to test this. So I got 3 sscrollbars to chose RGB of target color
c0and then some panels to show thec1,c2and their mix to visually verify its working as should. Here the code:And here a screenshot:
The only important stuff in code is the
sb_rgbChangeevent which is called on any change of any of the 3 scrollbars. It compute thec1,c2colors for both additive and substractive logic and output the colors into pannels.It works without any problem... btw You where right even my
Random(x)generatesxso the limit should be 255 (I already repaired the answer).Here complete source code (BDS2006 C++/VCL/Win32) and Win32 binary: