tags:

views:

4639

answers:

8

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 together and having higher sums be brighter, but I'm kind of at a loss as to where to start.

+2  A: 

Please define brightness. If you're looking for how close to white the color is you can use Euclidean Distance from (255, 255, 255)

Ben S
+16  A: 

I think what you are looking for is the RGB -> Luma conversion formula.

Photometric/digital ITU-R:

Y = 0.2126 R + 0.7152 G + 0.0722 B

Digital CCIR601 (gives more weight to the R and B components):

Y = 0.299 R + 0.587 G + 0.114 B

If you are willing to trade accuracy for perfomance, there are two approximation formulas for this one:

Y = 0.33 R + 0.5 G + 0.16 B

Y = 0.375 R + 0.5 G + 0.125 B

These can be calculated quickly as

Y = (R+R+B+G+G+G)/6

Y = (R+R+R+B+G+G+G+G)>>3
Franci Penov
I like that you put in precise values, but also included a quick "close enough" type shortcut. +1.
Beska
+16  A: 

Do you mean brightness? Perceived brightness? Luminance?

  • Luminance (standard, objective): (0.2126*R) + (0.7152*G) + (0.0722*B)
  • Luminance (perceived option 1): (0.299*R + 0.587*G + 0.114*B)
  • Luminance (perceived option 2, slower to calculate): sqrt( 0.241*R^2 + 0.691*G^2 + 0.068*B^2 )
Anonymous
Note that both of these emphasize the physiological aspects: the human eyeball is most sensitive to green light, less to red and least to blue.
Bob Cross
Yes, it all depends on the application. All these models including human subjective perception...
Anonymous
Note also that all of these are probably for linear 0-1 RGB, and you probably have gamma-corrected 0-255 RGB. They are not converted like you think they are.
alex strange
Where'd ya get those formulas?
bobobobo
For the first two the source is in the other answers. As for the final one - I think it was from the lectures on television or graphics...
Anonymous
I think your last formula is incorrect. I get it returning black for a dark blue color.
Nemi
+2  A: 

The HSV colorspace should do the trick, see the wikipedia article depending on the language you're working in you may get a library conversion .

H is hue which is a numerical value for the color (i.e. red, green...)

S is the saturation of the color, i.e. how 'intense' it is

V is the 'brightness' of the color.

Ian Hopkinson
Problem with the HSV color space is that you can have the same saturation and value, but different hue's, for **blue** and **yellow**. Yellow is **much** brighter than blue. Same goes for HSL.
Ian Boyd
+1  A: 

RGB Luminance value = 0.3 R + 0.59 G + 0.11 B

http://www.scantips.com/lumin.html

If you're looking for how close to white the color is you can use Euclidean Distance from (255, 255, 255)

I think RGB color space is perceptively non-uniform with respect to the L2 euclidian distance. Uniform spaces include CIE LAB and LUV.

+1  A: 

The 'V' of HSV is probably what you're looking for. MATLAB has an rgb2hsv function and the previously cited wikipedia article is full of pseudocode. If an RGB2HSV conversion is not feasible, a less accurate model would be the grayscale version of the image.

Jacob
A: 

To add what all the others said:

All these equations work kinda well in practice, but if you need to be very precise you have to first convert the color to linear color space (apply inverse image-gamma), do the weight average of the primary colors and - if you want to display the color - take the luminance back into the monitor gamma.

The luminance difference between ingnoring gamma and doing proper gamma is up to 20% in the dark grays.

Nils Pipenbrinck
A: 

I found this script (written in C#) that does an excellent job of calculating the "brightness" of a color. In this scenario, the script is trying to determine whether to put white or black text over the color. http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx

sitesbyjoe