Search code examples
algorithmcolorsrgbhsv

Algorithm to desaturate RGB color


I have a question. Is there some way or some algorithm that I can use to desaturate an RGB value without converting the color to HSV or HSL? My RGB input is going to be fully saturated, therefore I just need a way to desaturate my color.

I'm working on a color picker and I need a fast and efficient way of doing this. Any suggestions would be much appreciated:)


Solution

  • Simple approach (ignoring perceived lumunance like 0.3R+0.6G+0.11B and other formulas):

    Initial color (r,g,b)
    Gray color of the same intensity (i,i,i)
    where i = (r + g + b) / 3

    Deviations from gray components:

    dr = i - r
    dg = i - g
    db = i - b
    

    Let parameter t changes from 0 for initial saturated color to 1 for gray color. So

    r' = r + dr * t  
    g' = g + dg * t
    b' = b + db * t