Formula to determine brightness of RGB color

前端 未结 20 3141
猫巷女王i
猫巷女王i 2020-11-21 23:16

I\'m looking for some kind of formula or algorithm to determine the brightness of a color given the RGB values. I know it can\'t be as simple as adding the RGB values toget

20条回答
  •  梦谈多话
    2020-11-21 23:39

    Here's a bit of C code that should properly calculate perceived luminance.

    // reverses the rgb gamma
    #define inverseGamma(t) (((t) <= 0.0404482362771076) ? ((t)/12.92) : pow(((t) + 0.055)/1.055, 2.4))
    
    //CIE L*a*b* f function (used to convert XYZ to L*a*b*)  http://en.wikipedia.org/wiki/Lab_color_space
    #define LABF(t) ((t >= 8.85645167903563082e-3) ? powf(t,0.333333333333333) : (841.0/108.0)*(t) + (4.0/29.0))
    
    
    float
    rgbToCIEL(PIXEL p)
    {
       float y;
       float r=p.r/255.0;
       float g=p.g/255.0;
       float b=p.b/255.0;
    
       r=inverseGamma(r);
       g=inverseGamma(g);
       b=inverseGamma(b);
    
       //Observer = 2°, Illuminant = D65 
       y = 0.2125862307855955516*r + 0.7151703037034108499*g + 0.07220049864333622685*b;
    
       // At this point we've done RGBtoXYZ now do XYZ to Lab
    
       // y /= WHITEPOINT_Y; The white point for y in D65 is 1.0
    
        y = LABF(y);
    
       /* This is the "normal conversion which produces values scaled to 100
        Lab.L = 116.0*y - 16.0;
       */
       return(1.16*y - 0.16); // return values for 0.0 >=L <=1.0
    }
    

提交回复
热议问题