Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What algorithm is behind the Gimp's "Color to Alpha" feature?

For those who aren't familiar with the Gimp's "Color to Alpha" feature yet, here is the page on it from the Gimp's documentation: Color to Alpha. It does a really good job, and I wonder much how exactly Gimp does it in terms of color manipulation, whichever color space the colors might be in. Thanks a bunch for any hints.

EDIT 1: Generating transparency information for a pixel based on its similarity to the key color (the one you select in the "Color to Alpha" dialog), like some folk suggested before removing his answer for some reason, would sound like a good insight, but I suppose it's more intricate than that. Let's assume we estimate color similarity in the unit range from 0.0 to 1.0 and we've got a pixel whose color is, for example, 0.4 similar to, say, the color of white (like you would have selected white in the "Color to Alpha" dialog) and therefore the pixel gets an alpha value of 0.6, then how would you alter the pixel's actual color to compensate the loose of brightness/luminosity/saturation when the resulting pixel is displayed against a white background with the alpha of 0.6?

EDIT 2: Actually an update: The sub-question related to the first edit has been answered in How to change the alpha of a pixel without changing the resulting color? but it's probably not the full story because what is going on in the Gimp's source for the "Color to Alpha" feature is not that simple and seems to be based on a specific algorithm rather than a formula.

like image 337
Desmond Hume Avatar asked Feb 14 '12 16:02

Desmond Hume


2 Answers

I took a look at the source code, and the meat of it is the colortoalpha function. The parameters *a1 to *a4 are the input/output red, green, blue and alpha, respectively, and c1 to c3 is the color to make alpha.

When you're combining two colors c1 and c2 with a specific alpha a (0 ≤ a ≤ 1), the result is

y = a * c1 + (1-a) * c2

Here we're doing the reverse operation: We know the end result y and the background color c2, and want to figure out c1 and a. Since this is an under-specified equation, there's an infinite amount of solutions. However, the ranges 0 ≤ c1 ≤ 255 and 0 ≤ a ≤ 1 adds bounds to the solution.

The way the Gimp plugin works is that for each pixel it minimizes the alpha value (i.e. maximizes transparency). Conversely, this means that for each resulting pixel that isn't completely transparent (i.e. was not exactly the background color), one of the RGB components is either 0 or 255.

This produces an image that when overlayed on top of the specified color will produce the original image (in absence of rounding errors) and has maximum transparency for each pixel.

It's worth noting that the whole process is done in the RGB color space, but could be performed in others as well, as long as the combining operation is done in the same color space.

like image 86
Sampo Avatar answered Sep 21 '22 11:09

Sampo


I implemented this filter into my editor www.Photopea.com under Filter - Other - Color to Alpha. The results are 100% identical to GIMP. The algorithm is extremely simple.

The idea: a background color B was combined with a foreground color F using the transparency value A, to get a new color N:

N = A * F  +  (1 - A) * B;

You know N (the actual color in the image) and B (the parameter of the filter), and you want to recover the foreground color F and its transparency A.

Do it like this:

A = max( abs(N.r - B.r), abs(N.g - B.g), abs(N.b - B.b)  )  

Now, you know N, B, A. Just use the formula above to compute the F.

like image 33
Ivan Kuckir Avatar answered Sep 22 '22 11:09

Ivan Kuckir