colormath: Desaturation

Started by
14 comments, last by S1CA 18 years, 2 months ago
Hi! I want to desaturate my colors in a shader... the question is: What's the most performant way to do desaturation? I thought about converting from RGB to HSV and then back to RGB but the formulas i found on wikipedia look a lil bit complex - so i don't think that this is the best way to do it... Every Hint is appreciated! Thx!
Advertisement
I'm not a maths guru but seems to me that you can do something like this:

Greyscale Intensity = 0.3 * red + 0.59 * green + 0.11 * blue

so to desaturate you would just lerp:

new_pixel.R = Intensity * K + old_pixel.R * (1 - K)
new_pixel.G = Intensity * K + old_pixel.G * (1 - K)
new_pixel.B = Intensity * K + old_pixel.B * (1 - K)

so K would range from 0.0 to 1.0 where 0 gives new pixel = old pixel and 1.0 for fully desaturated.

If I'm off the mark then sorry: I was just heading off to bed for a much needed sleep.

(Watch the debate that will follow about the constants 0.3, 0.59, 0.11)
hmmmm . . . 0.3, 0.59, 0.11 ? i don't understand...
Just multiply every color component with 0.3 for red, 0.59 for green, 0.11 for blue.

this are the values for color conversion to grayscale. search with google for color conversion and yo will find many other algorithms

here are some links:

http://www.easyrgb.com/math.html
http://astronomy.swin.edu.au/~pbourke/colour/
-----"Master! Apprentice! Heartborne, 7th Seeker Warrior! Disciple! In me the Wishmaster..." Wishmaster - Nightwish
sure? how can this be?? if you multiply plain white (1.0,1.0,1.0) with these values you get (0.3,0.59,0.11) which is green!?
Quote:Original post by genesys
sure? how can this be?? if you multiply plain white (1.0,1.0,1.0) with these values you get (0.3,0.59,0.11) which is green!?
No, you do a dot product of the color and (0.3, 0.59, 0.11) to get a grayscale intensity value. If you were converting to grayscale you would set each color channel to this intensity. What the AP was suggesting is to get this grayscale intensity and then linear interpolate between it and the original color to get your desaturation.
Quote:Original post by genesys
sure? how can this be?? if you multiply plain white (1.0,1.0,1.0) with these values you get (0.3,0.59,0.11) which is green!?


1 * .3 = .3
1 * .59 = .59
1 * .11 = .11
----------------+
intensity = .3 + .59 + .11 = 1

r = intensity
g = intensity
b = intensity
if you're just trying to change the image into grayscale, I believe that taking the average of the r,g,b channels and replacing them with it, should work.
m = .33333f*(r+g+b);r=g=b=m;
Using the different weights for the red, green, and blue values (.3, .59, .11) might provide better looking results than just averaging the values, and since it is a dot product it might even be possible to calculate it as quickly as the average. The different values for each component reflect the eye's sensitivity to each color. A fully saturated green appears brighter than a fully saturated blue.
I don't want to desaturate the colors completely! just a lil bit!

This topic is closed to new replies.

Advertisement