views:

227

answers:

3

Basically, I have a context where I can't programatically tint an image, though I can change it's alpha value. With some experimentation, I've found that I can layer a red, blue, and green version of the image, with specific alpha values, to produce a wide range of colors. However, I am wondering if it's possible to achieve a true RGB representation through this method? If so, what is the formula for converting an RGB value into different alpha values for the red, blue, and green layers.

A: 

I don't think that's possible, if I understand you correctly.

If, for a certain pixel, your image's red value is, say, 0.5, you can combine that with an alpha in the (typical) range [0,1] to form any value up to and including 0.5, but you can't go above, to get e.g. 0.6 or so as the output value.

unwind
+1  A: 

The basic "equation" of alpha combination is:

alpha * (R1,G1,B1) + (1-alpha) * (R2,G2,B2)

When you have three layers with alpha you are actually combining 4 layers (the 4th one is black) so the final color is:

alpha1 * (R1,G1,B1) + (1-alpha1) * ( 
   alpha2 * (R2,G2,B2) + (1-alpha2) * (
      alpha3 * (R3,G3,B3) + (1-alpha2) * (0,0,0) ) )

Provided you have the same image on each layer and layer1 is the red channel (G1=B1=0) and layer2 is green and layer3 is blue you get:

(alpha1 * R, (1-alpha1)*alpha2 * G, (1-alpha1)*(1-alpha2)*alpha3 * B)

For a white pixel you can do any possible color. For a black pixel you cannot do anything but black. For any other pixel, you are restricted by the values of R, G and B.

Say you wanted to achieve (Rd, Gd, Bd) at a pixel where the current color is (R, G, B) then you would have to choose:

alpha1 = Rd/R
alpha2 = Gd/(G*(1-alpha1))
alpha3 = Bd/(B*(1-alpha1)*(1-alpha2))

The problem is that alpha can typically only be between 0 and 1. So, for example, if Rd > R there is nothing you can do.

You can do better if you can control the blending function (for example, in Photoshop).

Philippe Beaudoin
So, using that formula, how would I derive alpha1, alpha2, and alpha3 from a single RGB value that I want to achieve?if R = 0xAA, then would alpha1 = 0xAA/0xFF?
Chris
I've edited my answer. Please consider upvoting/checking if you think its a good answer.
Philippe Beaudoin
I am experimenting in photoshop by layer 3 circles over each other and plugging in the values. The circles are colored r: 0xff0000g: 0x00ff00b: 0x0000ffIf I want to achieve color value 0xaa5588, your formula gives alpha values of .69, .32, .78, respectively. However, these layerings don't produce the proper color, whether over a black or white background. This is essentially the scenario I am trying to achieve. I also tried subtracting the lowest of the 3 target values (0x55) from each other, and using those new values over 0xFF, which gets closer, but not quite there. Any thoughts?
Chris
I made a mistake in my derivation, corrected now I hope. It also means that you've got a much reduced range in the color you can achieve. To achieve 0xaa5588, the values should be: 0.66, 1, infinity. In other words, you cannot do it.
Philippe Beaudoin
Note that this is order dependent. If you layer it in another order: green first, then blue, then red, the alpha you needs are: 0.33, 0.8, 5.0. Still you cannot do it, unfortunately.
Philippe Beaudoin
A: 

If you're looking to create 3 layers that blended together add up to the original image, it's quite possible. Here's a link to a Python script that calls functions in Paint Shop Pro to do the hard parts.

http://pixelnook.home.comcast.net/~pixelnook/SplitToRGB.htm

If you need more detail than that, leave a comment and I'll try to fill it in later.

Mark Ransom