How to calculate (a physical) ratio of colors to achieve a target color?

Sorry in advance about my way of expressing this - I am not really a math person to be fair. However, I have been pretty obsessed with an idea I got and that idea requires me to understand something about color mixing in the real world. It turns out that what I want to do is not trivial (to me at least).

Some context: Imagine you're an artist and you want to paint your own Mona Lisa. To do this you have a reference photo of the real Mona Lisa. You have sketched an outline of the sections of where the colors will go and it is time to color it. There's only one problem - You're a noob and you don't know how to achieve the target colors. The target color is a brown nuance based on a shadow near her eye: R:114 G:78 B:42

You have several factory-mixed and known industry colors:

Cadmium Red: R:227 G:0 B:34, Cadmium Yellow: R:255 G:246 B:0, Titanium White: R:243 G:244 B:247, Ivory Black: R:35 G:31 B:32, Burnt Umber: R:127 G:50 B:36, Cobolt Blue: R:0 G:71 B:171, Phtalo Blue: R:0 G:15 B:137, and a few others..

If you take an amount of Cadmium Yellow and mix it with an equal amount of Burnt Umber then you end up changing the RGB. It [originally cadmium yellow] now has a new RGB after being mixed with Burnt Umber. relative to the first amount of cadmium yellow, R is reduced, G is reduced even more and B is increased slightly.

I can visually approximate a target color by mixing it intuitively. With different colors, there is some mix-ratio of those colors that will produce the target color. I want to know, how to know that ratio!

I know that there are several things in play here, as the amount of resin/color pigments, etc., but let's assume that I can figure out to compensate for that myself at some later point if I get that far. My problem seems to be, to figure out how some physical color with a given RGB interacts with another physical color with another RGB and how that interaction manifests in the RGB of the mixture.

It doesn't have to be RGB by the way if some other system makes this easier.

Question: How can I figure out what ratio to mix colors to achieve a target color (or visually approximating it at least)?


Solution 1:

I don’t know how mixing of paints works in the real world, but let’s assume it follows the obvious mathematical rules.

Suppose you have four ingredient colors $(r1, g1, b1)$, $(r2, g2, b2)$, $(r3, g3, b3)$, and $(r4,g4,b4)$, and you want to produce a target mix color $(r0, g0, b0)$.

Then you need to find four proportions $p1$, $p2$, $p3$, $p4$ such that $p1 + p2 + p3 + p4 = 1$ and $$ p1*(r1, g1, b1) + p2*(r2, g2, b2) + p3*(r3, g3, b3) +p4*(r4,g4,b4) = (r0, g0, b0) $$ This gives you four linear equations that you will (perhaps) be able to solve to get $p1$, $p2$, $p3$, $p4$.

I say “perhaps” because the four equations might not have a solution. You’ll need to read about systems of linear equations to understand the when and why. It boils down to whether or not your ingredient colors are sufficiently rich and varied to “span” the entire $(r,g,b)$ color space.

If you only have three or fewer ingredient colors, there’s only a small chance that you’ll be able to get the target color you want. You might be lucky, but probably not.

Another way to think of this: your four ingredient colors are the vertices of a tetrahedron in a three-dimensional $rgb$ space. What we’re looking for are the so-called “barycentric coordinates” of the target color within this tetrahedron. This will work only if the tetrahedron has non-zero volume (i.e. it’s not squashed to a plane, or worse). The non-zero volume corresponds to the four ingredients being “rich and varied”, as I mentioned above. You probably don’t know what I’m talking about, but you can look up barycentric coordinates, at least.

Solution 2:

Very similar to @bubba's answer

This assume linear mixing rules for the colors (I have no idea about it).

Suppose that you select $n$ colors $(r_n,g_n,b_n)$ that you want to mix to obtain, at least as close as possible, a color $(R,G,B)$ we can write $n$ equations
$$f_{r}= \sum_{i=1}^n p_i\, r_i -R $$ $$f_{g}= \sum_{i=1}^n p_i\, g_i -G $$ $$f_{b}= \sum_{i=1}^n p_i\, r_i -B $$

Now, define a norm $$\Phi(p_1,p_2,\cdots,p_n)=\big[f_{r}\big]^2+\big[f_{b}\big]^2+\big[f_{b}\big]^2$$ Now, differentiating the norm (that we want to minimize) with respect to each $p_i$ gives $n$ linear equations (as @bubba already wrote them).

So, we can use Lagrange multipliers, the constraints being possibly $p_i^{\text{min}} \leq p_i \leq p_i ^{\text{max}}$ and, for sure, $\sum_{i=1}^n p_1=1$ or some bound constrained optimizer.

For illustration purposes, I took the following colors (the ones you gave) $$\left( \begin{array}{cccc} i & r_i & g_i & b_i \\ 1 & 227 & 0 & 34 \\ 2 & 255 & 246 & 0 \\ 3 & 243 & 244 & 247 \\ 4 & 35 & 31 & 32 \\ 5 & 127 & 50 & 36 \\ 6 & 0 & 71 & 171 \\ 7 & 0 & 15 & 137 \end{array} \right)$$ and we want the color $(114,78,42)$.

Using $p_i^{\text{min}}=0 ~~\forall i$, the minimum of $\Phi$ (very close to $0$ - more precisely $1.58\times 10^{-13}$ ) was attained for the following set of $p_i$ $$\{0.0921245,0.180839,0.0154541,0.545447,0.189983,0.0412615,0.0270162\}$$ and this combination gives exactly the desired color.

If, for the same problem, I impose $p_i^{\text{min}}=0.1 ~~\forall i$ the minimum value is quite far away from $0$ (more precisely $1322$ ) and the minimum is attained for the following set of $p_i$ $$\{0.1,0.137977,0.1,0.462023,0.1,0.1,0.1\}$$and this combination gives the color $(118,93,76)$.

Warning

If the minumum is not very close to $0$, the result would depend on the manner the equations would write; in such a case, I should rescale then as, for example, $$f_{r}= \frac 1 R \sum_{i=1}^n p_i\, r_i -1 $$ $$f_{g}= \frac 1 G \sum_{i=1}^n p_i\, g_i -1 $$ $$f_{b}= \frac 1 B \sum_{i=1}^n p_i\, r_i -1 $$

Used for the bad case in the worked example would lead to

$$\{0.1,0.171347,0.1,0.428653,0.1,0.1,0.1\}$$and this combination gives the color $(111,86,77)$.