how to calculate Brightness of RGB color?

Tomasz Szulc picture Tomasz Szulc · Feb 27, 2012 · Viewed 8.2k times · Source

i've got my function:

-(void)rgbToHSBWithR:(float)red G:(float)green B:(float)blue {

float brightness = red * 0.3 + green * 0.59 + blue * 0.11; // found in stackoverflow
NSLog(@"%f",brightness);
}

and it isn't work for me.

for example: r:84 g:67 b:73. function return 72.760002. In Photoshop brightness for this color is 33%. What's wrong?

Thanks.

Answer

jscs picture jscs · Feb 27, 2012

Your RGB values range from 0 to 255, not 0 to 99 -- you need to divide them first if you want to end up with a percentage:

float brightness = (red / 255.0) * 0.3 + (green / 255.0) * 0.59 + (blue / 255.0) * 0.11;

Also, there is no single conversion between "brightness" and RGB values -- Photoshop may be using a different formula than you are. If you want to know more, I recommend the "Gamma FAQ" and "Color FAQ" by Charles Poynton.