Color algebra: How do I pick a color F will make known color C against known background B at opacity N?
February 18, 2008 4:39 PM   Subscribe

Color algebra: Given a color C, how do I pick a color F that will be color C when against background color B at opacity N? How would I do the same thing for a color multiplication?

I'm guessing one can start by treating each color as an RGB or CMYK vector, but I'm more than a little fuzzy on exactly what these operations mean and therefore how to invert them to pick F.

If context matters in defining the operations, practically I'm concerned with whatever Illustrator CS and Fireworks 8 mean by the terms, but I'm interested in general observations as well.
posted by weston to Computers & Internet (18 answers total) 5 users marked this as a favorite
 
Assuming opacity is defined in terms of a linear blending of the two layers, you can just use algebra. For some values of C, B, and N, F might be outside the color space.

C = B * (1 - N) + F * N
F * N = B * (1 - N) - C
F = (B * (1 - N) - C) / N
posted by 0xFCAF at 5:01 PM on February 18, 2008


0xFCAF: Obviously a computer isn't going to produce colors outside of the color space by any means.

The method used by computers to determine the output color of two colors with an alpha channel is Alpha Blending. The color values used in that formula are normalized from 0 - 1.
posted by delmoi at 5:20 PM on February 18, 2008


I'm reasonably sure this can't be done. Certainly not universally for any opacity and any background/foreground color combination, though you might be able to make it work for very slight color differences.

Even if opacity didn't enter into the mix, determining what the color will "be" is tricky. Two of the classics of color theory, The Elements of Color and Interaction of Color describe perceptual color illusions. The effect is better in print, but surrounding colors influence our perception. That's why the two identical blue squares look different and the two identical brown squares look different, even though they're not.

But putting aside perceptual problems, you're still dealing with aspects of Hue, Saturation and Brightness. Once you throw opacity into the mix any background color that differs noticably along more than one of those axes is going to be practically impossible to compensate for.
posted by Jeff Howard at 5:32 PM on February 18, 2008


Normalization isn't relevant - it's still possible that F is not in the range of a given color space. Set N = 0.00001, B = black and C = white --> F will be 'brighter' than any color that is normally expressed.
posted by 0xFCAF at 5:42 PM on February 18, 2008


0xFCAF: eponysterical answerer?
posted by jpdoane at 7:07 PM on February 18, 2008 [1 favorite]


Disregarding transparency: Find F such that F on background color B looks like color C.

This is underdetermined, because color F also depends on the size and shape of the region (and also on the size and shape of the surround) and also on whether the F region is moving.
posted by hexatron at 7:07 PM on February 18, 2008


Response by poster: F = (B * (1 - N) - C) / N


So what's the multiplication (and associated division) operation here? Is it multiplication inside each position of an RGB or CMYK tuple?


But putting aside perceptual problems, you're still dealing with aspects of Hue, Saturation and Brightness. Once you throw opacity into the mix any background color that differs noticably along more than one of those axes is going to be practically impossible to compensate for....
This is underdetermined, because color F also depends on the size and shape of the region (and also on the size and shape of the surround) and also on whether the F region is moving.

I think it's safe to put aside perceptual issues for the practical application I'm interested in. But if there is a mechanic involved in the interaction between an arbitrary background and a transparent foreground that makes combining the two to yield a given color difficult or impossible, I'm interested.
posted by weston at 8:28 PM on February 18, 2008


I'm not knowlegable enough with the workings of CMYK tuples to opine on them, but if you work in RGB space, then it's a vector equation that works for each color element independently. e.g.
F_red = [B_red * (1 - N) - C_red] / N

and the same equation works for the green and blue components respectively.
And as 0xFCAF said, there are some values for this equation that don't work, because no color combined with B with opacity N couldn't possibly result in C. If you try to use this equation in such a circumstance, it will give you a color F that is nonsensical (i.e. negative or impossibly bright).
posted by Humanzee at 8:52 PM on February 18, 2008


For theoretical applications, the existance of an equation is interesting, but for practical applications it seems less helpful. Humanzee's illustration of negative or impossibly bright colors is a good example. Those colors don't actually exist in a way that Fireworks or Illustrator can utilize.

There are a narrow range of circumstances where what you're suggesting is possible. If the background and the color to match differ only in saturation then F is pretty easy to find, especially if the color to match is in the middle of the spectrum. Likewise for two colors that differ only in brightness.

If you want to do this with colors that differ in hue, C needs to be a hue that incorporates B as a component. So if B is yellow, then this only works if C happens to be either green or orange otherwise there's no practical F that can mix with B to get C. (And if B isn't a primary color you're out of luck.)

Once you add variations in saturation and brightness to variations in hue, this becomes almost impossible as a practical exercise.
posted by Jeff Howard at 10:43 PM on February 18, 2008


Normalization isn't relevant - it's still possible that F is not in the range of a given color space. Set N = 0.00001, B = black and C = white --> F will be 'brighter' than any color that is normally expressed.

Um, no, according to your own formula "F = (B * (1 - N) - C) / N."

Then F = (0 - 1) / 10-5 = -10-5, which is actually 'darker' then any color expressed.

Unfortunately, your formula is wrong. The actual formula (for Ca 'over' Cb), as per wikipedia would be

Co = Caαa + Cb(1-αa)

Doing variable substitution, that works out too:

F = C*N + B*1

Doing numerical substitution we get

F = 1*10-5+ 0*1

Which of course works out too

10-5, a positive number between 0 and 1
posted by delmoi at 10:08 AM on February 19, 2008


I'm reasonably sure this can't be done. Certainly not universally for any opacity and any background/foreground color combination, though you might be able to make it work for very slight color differences.

This is totally absurd. If it were not possible, then there would be no way to do layer opacity in photoshop, for example. Alpha blending is a mathematical formula applied to binary data. The result is always the same, and weston's goal here is to produce the same output as various art programs.
posted by delmoi at 10:12 AM on February 19, 2008


For theoretical applications, the existance of an equation is interesting, but for practical applications it seems less helpful. Humanzee's illustration of negative or impossibly bright colors is a good example.

No, it's a fucking mistake.
posted by delmoi at 10:13 AM on February 19, 2008


(sorry for getting upset, it bugs me when someone will just say something in the beginning of a thread and everyone just goes along with it. I posted a link to the correct formulas in the second comment!)
posted by delmoi at 10:19 AM on February 19, 2008


Delmoi, I think you're misreading my post. I'm not arguing that layer blending is impossible. I'm only arguing that there are limits to the range of colors that are possible to mimic using layer blending. As I understand it, the OP's goal is to match colors precisely using layer blending techniques common to Illustrator and Fireworks. He essentially wants to "game" the blending to arrive at a pre-determined color. But it only works under a narrow range of circumstances.

Say you want to place a 50% transparent color (F) over bright green (B). You're trying to produce bright red (C). There's no color that will mix with bright green at 50% and get a bright red. You can certainly blend any two colors, but not to arrive at any arbitrary result.
posted by Jeff Howard at 10:32 AM on February 19, 2008


I think you're misreading my post. I'm not arguing that layer blending is impossible. I'm only arguing that there are limits to the range of colors that are possible to mimic using layer blending.

Ugh, crap your right. I somehow managed to read this post wrong today. Ugh.
posted by delmoi at 10:37 AM on February 19, 2008


Yes, it turns out I am retarded. Sorry.
posted by delmoi at 10:39 AM on February 19, 2008


Say you want to place a 50% transparent color (F) over bright green (B). You're trying to produce bright red (C). There's no color that will mix with bright green at 50% and get a bright red. You can certainly blend any two colors, but not to arrive at any arbitrary result.

Right of course. However you could still store the 'impossible color' in memory, and display a clipped version on the screen (so 'superbright' colors would appear as 100%, and 'superdark' colors would appear as 0%). The impossible color needed in your example could be a color that would always have a maxed out red channel, but use the normal alpha blending function on the green and blue channels.

But you would need to write the composing function yourself, and I'm not sure if you can do things like that in Illustrator or Fireworks.

Also, I'm still a total moron. Apologies to everyone :P
posted by delmoi at 11:01 AM on February 19, 2008


A day late and a dollar short--

I misunderstood the problem in my first comment. But in fact, the first solution
F = (B * (1 - N) - C) / N
posted by 0xFCAF at 8:01 PM on February 18
is correct, except that it disregards gamma

When arithmetic operations are performed on color values, the values are assumed to be linear wrt brightness, so that if A=2*B, then A emits twice as many photons/second as B). But the numbers given for RGB or CMYK are not linear wrt brightness--they are roughly exponential. And so we assume they are actually exponential, with exponent gamma(γ) (the letter long used in photographic science, and taken over into computer graphics. For most monitors, γ is between 2 & 3.

And the formula above turns into the following mess:

Let f=(pow(B,γ)*(1-N) - pow(C,γ)) / N (applying gamma)

Then if f>0

F= pow( f , 1/γ) (converting from linear back to pixel values)

where pow(a,b) is the power function a to the b-th power.

Note that f can be negative. This means that F is not just out-of-gamut, it is actually an unrealizable color. As noted above, there is NO 50% transparent color you can put over a pure green source that will result in pure red. The green source may have absolutely no red in it. Since the filter is not luminous itself, where would the red photons come from?

And this has nothing to do with the limitations of a CRT or color space. It's fizzicks.
posted by hexatron at 5:50 PM on February 20, 2008


« Older Do I still have Anxiety?   |   This secret is... Newer »
This thread is closed to new comments.