3

As an input I have two colors, let's say red (RGB = 1,0,0) and magenta (RGB = 1,0,1).

Now I have an image which includes additive mixes between these two colors, for example purple (RGB = 0.5,0,1).

I want to calculate the mix amount between these two colors where 0 is 100% the first color (red) and 1 100% the second color (magenta). In the example with purple, the mix would be 0.5.

Of course input colors may be as complicated as possible, but it is always ensured that they are indeed mixable.

I know how to calculate this for grayscale colors, but not for arbitrary input colors.

A visualized input and output would be like this, when the output will be used as the alpha channel for the second color:

Example unmixing

Ray
  • 151
  • How do you define mix and how did you get 0.8? and how would that make yellow orange (RGB = 0.2,1,0)? – Satish Ramanathan Sep 29 '14 at 18:11
  • (0.2,1,0)= 1(1,1,0) - 0.8(1,0,0). What does it mean to have added negative color? Perhaps a mix number would be red amplitude / yellow amplitude = -0.8 / 1 = -0.8. – SDiv Sep 29 '14 at 18:19
  • In general, this looks like a linear algebra problem. Instead of the unit vectors (1,0,0), (0,1,0), and (0,0,1) it looks like you want to use some other basis. This is always possible but you will find that you may need to add "negative color." In the RGB basis, you always add some amount or R G and B whereas this may not be the case in some other basis. – SDiv Sep 29 '14 at 18:23
  • @satishramanathan: Oops, I messed up something, I fixed the color names now. – Ray Sep 29 '14 at 21:43
  • @SDiv: If I could somehow get a single value for both colors, the formula would be simple. But with 3 components for each color, I don't know how to get such a value. Maybe it's brightness / hue or saturation of a color? I can think of a case for each of these three values where it wouldn't work eventually. – Ray Sep 30 '14 at 19:10
  • I am still confused. Given two colors, a positive mix of the two can only create colors that lie along the line between the two on the chromaticity diagram. Otherwise you would have to add "negative" color (whatever that is). For instance, in your example, purple is impossible because it requires -0.5 red. The RGB basis was chosen because it spans a large area on the chromaticity diagram for strictly positive mixes. Check out http://dotcolordotcom.files.wordpress.com/2012/08/anatomy-of-a-cie-diagram2x.png – SDiv Sep 30 '14 at 21:54
  • I found the answer, posting my answer. Thinking of RGB as XYZ made it pretty easy. – Ray Sep 30 '14 at 22:21

1 Answers1

2

Things became much easier to me when I thought of the RGB components as XYZ coordinates of a 3-dimensional point, and that the lengths between those is the key to success!

To get the mix-amount between two colors, you require some basic vector maths:

  • We have the background color A and foreground color B. C is the mixed color. All 3 have R, G and B components.
  • Compute the square length between A and B. With the formula for retrieving the square length between two 3-dimensional vectors, do the following: $$ \left \| AB \right \|^2 = (A_{r}-B_{r})^2+(A_{g}-B_{g})^2+(A_{b}-B_{b})^2 $$
  • Calculate the square length between A and C (the mixed color). For C it is ensured that it is a point on the track between A and B (and if it isn't, we don't care, as the provided A and B is wrong and it's not our fault): $$ \left \| AC \right \|^2 = (A_{r}-C_{r})^2+(A_{g}-C_{g})^2+(A_{b}-C_{b})^2 $$
  • Divide the square length of A to B by the square length of A to C. If you take the root of the result, you'll get the "alpha denominator" (you'll see shortly why): $$ d = \sqrt{\frac{\left \| AB \right \|^2}{\left \| AC \right \|^2}} $$
  • The alpha value eventually is 1 divided by the denominator: $$ a = \frac{1}{d} $$

The resulting a is exactly 0 if the color is completely the background color A. It is 1 if the color is completely foreground color B. Anything between is background color A + foreground color B * a.

I'm sorry if any of my math formatting is incorrect, I'm new to LaTeX and not really a mathematician :)

Ray
  • 151
  • Keep in mind that the color "C" must lie along the line between "A" and "B" on the chromaticity diagram. You cant make some other arbitrary color by adding positive amounts of only two colors. – SDiv Oct 01 '14 at 05:23
  • @SDiv: Yes, this is ensured by the input, I also repeated that in step 3. – Ray Oct 01 '14 at 13:39
  • 1
    Correct, yet in the main question you have the example of using (1,0,0) and (1,0,1) to make (0.5,0,1) which is impossible. – SDiv Oct 01 '14 at 14:05
  • @SDiv ew, sorry, I'm not really good in making up examples :/ – Ray Oct 01 '14 at 14:20