Saturday, August 23, 2014

Purity in RGB

We've just updated our Pic! app (update it if you have it!) to include a "turn it to 11" color mode.  This is sometimes called increasing color "purity" and more commonly "chroma".   There are lots of terms for this basic concept, and in graphics it is usually called "saturation" in the HSV/HSL models.   "Saturation" is used differently in different fields so I avoid it and use "purity".  Most of Pic! uses our own color model, but this seemed best to do in good old RGB.

Note that I am betting purity in RGB is discussed in 50 different places already.  But I couldn't find it anywhere and in case it simply is one of those topic most people think is so simple they never bother to write it down I will here.  I didn't find it that easy!

Reducing purity is easier than increasing it as I will discuss.  First compute the grey (luminance) level associated with the color c.

v = k_r*R + k_g*G + k_b*B

Where the color c is (R,G,B).   The constants are from whatever your favorite luminance formula is, and in my experience it doesn't matter much as long as you are consistent.  It wouldn't surprise me if (1/3, 1/3, 1/3) worked best in practice for most graphics applications and has nice computational advantages.   Now set up an RGB grey v = (v,v,v).    Our assumption that all colors are a combination of a "pure" color b and a grey v:

c = p*b + (1-p)*v

In RGB a "pure" color is one that is 0 in one of the components.  So we can keep subtracting off grey from the smallest component.

min = p*0 + (1-p)*v

so,

p = 1-min/v

Therefore

b = (c - (1-p)*v)/p

That may have components above 1, so pmax should be computed for better clamping:

1.0 = p_max*b_max + (1-p_max)*v
p_max(bmax-v) = 1-v
p_max = (1-v)/(bmax-v);

In glsl or other shading languages, min can be computed without figuring out which component:

float m = min(min(c.r, c.g), c.b);

Here's a diagram of what is going on.  When the color points toward one of the upper faces b will be outside the cibe (see clamping above).


A common use case of turning up the purity to 11 is food pictures.  I may have overdone it here, but you get the point:


5 comments:

Anonymous said...
This comment has been removed by the author.
Anonymous said...

This is what I have been searching in many websites and I finally found it here. Amazing article. I am so impressed. Could never think of such a thing is possible with it...I think you have a great knowledge especially while dealings with such subjects. clipping path

jhoney said...

Great place, great time. Getting strange experienced with orphanages. I learned how to hold an excelent progam for kids, hope more activities. I became a kid on that day in my thought.
Buy Facebook Fans

jhoney said...

Great place, great time. Getting strange experienced with orphanages. I learned how to hold an excelent progam for kids, hope more activities. I became a kid on that day in my thought.
Buy Facebook Fans

Unknown said...

I would also motivate just about every person to save this web page for any favorite assistance to assist posted the appearance
rock climbing holds