What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

A Photoshop substitute process for GIMP's "Color to Alpha" function?


Weikarczaena

New Member
Messages
3
Likes
0
Hi all you gurus and devious geniuses of the interwebs... I have a question for you. This is gonna be a long one so get comfy.

I just recently got CS5, and having been on GIMP since I started my amateur graphics designing I quickly noticed that a feature was missing from Photoshop: The Color to Alpha function. I found that there is no official Adobe or third party version of this plugin (that seems stable anyway), and since I don't feel like learning C just to code my own plugin for Photoshop, I was wondering if someone might be able to tell me how to do what I'm going to describe.

A background for those who don't know what Color-to-Alpha does: The most basic description is the plugin looks at each pixel and replaces a user defined color in that pixel with a corresponding alpha value. It essentially turns a color into a transparency value, but it's a little more complicated than that. It would be easy if this were a simple "pick a color and make each pixel with that color transparent" operation, but it's not. The overall effect is something along the lines of: If you were to have a solid color underneath your now-slightly-transparent layer, the resulting image you see is the same as when you had started.

Let me give you an example. I found a picture of a volcano that I like, but say I wanted to make the sky a different color.

1.png

To me, this would be a difficult venture using color selection tools that photoshop has because of those red color gradients in the eruption, but after applying a simple Color to Alpha function choosing black (#000000 in html color) you can clearly see how what is left is the absence of the black pixels.

2.png

Furthermore, adding a solid black layer just below it, the image returns to what it was before I performed the Color to Alpha.

4.png

And I can simply replace this black layer with a sky blue to get a nice sky that works well because all the pixels are what is left over from that black background.

3.png

And alright, to be fair, this is a terrible job with the center being all pink and everything, but I did this in literally 30 seconds and with a few tweaks it could look way better.

Anyway, that's what the function does, and I took it upon myself to get to the bottom of how this operation works. After a lot of digging around on GIMP's source I found the code that does the math (and I'll post it if anyone wants it), and it all revolves around this formula:

wiki.png
Source: Wikipedia's article for "Alpha Compositing"

In a nutshell, this formula calculates the color value for a pixel that is resultant from two semi-transparent layers with different colors in normal blending mode. There are a lot of symbols here, and I'll explain them. "C[SUB]o[/SUB]" is the color value of the resulting pixel, "C[SUB]a[/SUB]" is the color value of the above layer and "C[SUB]b[/SUB]" is the color value of the bottom layer. The Alpha symbols are exactly that: the alpha values for the Above layer and Bottom layer.

Whew, take a breather.

Now the meat of the question. I'm going to outline the mathematical process that the code goes through to determine the alpha values and the RGB values for each pixel, and I want you guys to tell me how to do this in Photoshop. I'm honestly not sure why this method works, but I'm going to ignore that and just tell you what it does. The general idea is you give it "C[SUB]b[/SUB]" and you know "C[SUB]o[/SUB]", and it's job is to find "C[SUB]a[/SUB]" and "Alpha_a". This process is done per pixel:

Step 1: It determines what the largest possible alpha value is for each channel based on the above equation. How it does that is it takes your supplied color and the corresponding source color value for the channel it's focusing on, and it checks which is larger. Then it sets "C[SUB]a[/SUB]" to either max or min depending on which of those numbers is bigger and carries out the calculation from the above equation to solve for Alpha. It does this for each channel

Step 1.5: Next it compares the resulting Alpha values from the 3 different channels and picks the largest one to be used for the remaining calculations.

Step 2: Now it takes that largest Alpha value and plugs it back into the original equation to solve for the actual color value for each channel. The author's notes said something about a max-min relationship and optimizing code or something, and this is the part that confuses me. The moral of the story is now we have Color Values for each channel and an Alpha value for the whole pixel.

Step 3: It sets that pixel's new channel color values based on its previous calculations and then it gives that pixel the transparency it used throughout all that process, and then it moves on to the next pixel.

So there you have it. Again, it takes a color you specify, does some math stuff, and spits out an altered color value with transparency for each pixel. It does this in such a way that if you were to place a layer beneath the altered one filled with the color you just turned to alpha, the original image would return with no alterations. I want to know how to do this in Photoshop.

I'm not even sure if this is possible, but I figured this would be the right place to ask. And I do realize that I could just load my image into GIMP, do the operation and save it as a GIF or something, and then load it into Photoshop, but that's lame! I want to beat the system and not just have a workaround lol.

Anyway, thanks for reading, and if you need explanations on anything or if you need more information let me know. I'm really curious about whether I can do this in Photoshop!
 
I don't think it has the EXACT feature you talk of, however it is fairly simple. If you go to select in the top menu, then color range, and use the dropper tool to select a color, turn the fuzziness up a bit, and press okay it will give you a selection based on the color you picked, you can then use a layer mask to mask out the unwanted color and adjust it accordingly, if it masks out the opposite to what you want to keep, then just select the mask and invert the colors.

par example, this is a quick one minute job using the select by color range tool and the layer mask. I had to add the rock of the volcano back in on the layer mask, but it wasn't that difficult.
example.jpg
 
Hi Zeealex,

Yeah I've looked into the color range tool and it is effective enough for solid blocks of color, but the difference is that real life semi-transparent elements can't get that transparent look to them. What I mean is if instead you were to drop a picture behind the volcano, you would be able to see the picture through the spray because the it's slightly see-through. Here's an example:

This one is using the color picker technique (and it's a bad job because I can't use Photoshop >.<)
5.png

And this is GIMP's Color to Alpha:

6.png

Is there a way to get an effect like the second one?
 
I was able to get similar effect. I turned off the Blue channel, enabled Blend If at the 0 end and split it off to 0/55, while leaving the other end at 255.
 
I got this using two duplicate layers, one using the blue channel as a layer mask, and the other using the green channel

Green Blue channel layer mask.jpg

obviously it would require careful touch ups.
 
One of my favorite, but oldest plugins for PS (still works fine in CS6 and CC) is "Asiva Selection". It does everything the OP requested (plus other things such as selecting on ranges of saturation, selecting on multiple color ranges, etc.). Unfortunately, I think the software company that produced it has gone out of business, but I just did a quick search and it seems to still be available from many sources.

First here's what the interface looks like:
selecting_greens.jpg


Next, as a background layer, I used a shot of a kitchen of a renovated house in South Carolina. This was a particular HDR rendering of that scene that I didn't like because it's too uniformly bright, but, because it's so bright and warm, it contrasts nicely with the exterior shot, so should work well as a background for this demo:

interior_shot-as_background.jpg

As a foreground layer, I used a shot of the exterior of the same house. From the outside, you would have almost no clue how much work the owner did to the interior:

D7B_3830-SC_house_by_TJM-01-as_foreground.jpg


I then used Asiva Select to select on a range of hues around the greens on the exterior shot. It produced the following mask:

mask_produced_by_asiva.jpg

Here's the final result that shows the transparency being smoothly controlled by the proximity of the hue to green:

color_to_transparency_by_asiva_select-ps01a-01_result.jpg

HTH,

Tom

PS - If one enjoys a complete DIY approach based on the math that described, it can certainly be exactly reproduced in Matlab, and almost certainly also in PS, but to do it exactly would likely be tedious (and I don't feel like doing it since I have a method that works - LOL).
 
Thanks for responding.

Crotale: I did not know about the Blend If feature, but it seems like a really cool tool. Could you elaborate a bit more on exactly what process you used? I'm a little confused.

Zeealex: This technique did indeed work well. You're right in that it requires a lot of time to find how much of each channel to use as the mask, but this has a lot of promise. I'll play around with this technique.

Tom Mann: I'll have to check this tool out before I buy it. Thank you for the suggestion.
 

Back
Top