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.
Join them; it only takes a minute:
|
|
Do you mean brightness? Perceived brightness? Luminance?
|
|||||||||||||||||||||
|
|
I think what you are looking for is the RGB -> Luma conversion formula. Photometric/digital ITU BT.709:
Digital ITU BT.601 (gives more weight to the R and B components):
If you are willing to trade accuracy for perfomance, there are two approximation formulas for this one:
These can be calculated quickly as
|
|||||||||||||||||||||
|
|
I have made comparison of the three algorithms in the accepted answer. I generated colors in cycle where only about every 400th color was used. Each color is represented by 2x2 pixels, colors are sorted from darkest to lightest (left to right, top to bottom). 1st picture - Luminance (relative)
2nd picture - http://www.w3.org/TR/AERT#color-contrast
3rd picture - HSP Color Model
4th picture - WCAG 2.0 SC 1.4.3 relative luminance and contrast ratio formula (see @Synchro's answer) Pattern can be sometimes spotted on 1st and 2nd picture depending on the number of colors in one row. I never spotted any pattern on picture from 3rd or 4th algorithm. If i had to choose i would go with algorithm number 3 since its much easier to implement and its about 33% faster than the 4th.
|
|||||
|
|
Below is the only CORRECT algorithm for sRGB images, as used in browsers etc.. It is necessary to apply an inverse of the gamma function for the color space before calculating the inner product. Then you apply the gamma function to the reduced value. Failure to incorporate the gamma function can result in errors of up to 20%. For typical computer stuff, the color space is sRGB. The right numbers for sRGB are approx. 0.21, 0.72, 0.07. Gamma for sRGB is a composite function that approximates exponentiation by 1/(2.2). Here is the whole thing in C++.
|
|||||||||||||||||||||
|
|
Interestingly, this formulation for RGB=>HSV just uses v=MAX3(r,g,b). In other words, you can use the maximum of (r,g,b) as the V in HSV. I checked and on page 575 of Hearn & Baker this is how they compute "Value" as well.
|
|||
|
|
|
I found this code (written in C#) that does an excellent job of calculating the "brightness" of a color. In this scenario, the code is trying to determine whether to put white or black text over the color. |
|||||
|
|
Rather than getting lost amongst the random selection of formulae mentioned here, I suggest you go for the formula recommended by W3C standards. Here's a straightforward but exact PHP implementation of the WCAG 2.0 SC 1.4.3 relative luminance and contrast ratio formulae. It produces values that are appropriate for evaluating the ratios required for WCAG compliance, as on this page, and as such is suitable and appropriate for any web app. This is trivial to port to other languages.
|
|||||||||
|
|
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. |
|||
|
|
|
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. |
|||||||||
|
|
RGB Luminance value = 0.3 R + 0.59 G + 0.11 B http://www.scantips.com/lumin.html
I think RGB color space is perceptively non-uniform with respect to the L2 euclidian distance. Uniform spaces include CIE LAB and LUV. |
|||
|
|
|
This link explains everything in depth, including why those multiplier constants exist before the R, G and B values. Edit: It has an explanation to one of the answers here too (0.299*R + 0.587*G + 0.114*B) |
||||
|
|
|
The inverse-gamma formula by Jive Dadson needs to have the half-adjust removed when implemented in Javascript, i.e. the return from function gam_sRGB needs to be return int(v*255); not return int(v*255+.5); Half-adjust rounds up, and this can cause a value one too high on a R=G=B i.e. grey colour triad. Greyscale conversion on a R=G=B triad should produce a value equal to R; it's one proof that the formula is valid. See Nine Shades of Greyscale for the formula in action (without the half-adjust). |
|||
|
|
|
Here's a bit of C code that should properly calculate perceived luminance.
|
|||
|
|
|
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. |
|||
|
|
|
For clarity, the formulas that use a square root need to be
not
The proof of this lies in the conversion of a R=G=B triad to greyscale R. That will only be true if you square the colour value, not the colour value times coefficient. See Nine Shades of Greyscale |
|||||||||
|
|
I wonder how those rgb coefficients were determined. I did an experiment myself and I ended up with the following:
Close but but obviously different than the long established ITU coefficients. I wonder if those coefficients could be different for each and every observer, because we all may have a different amount of cones and rods on the retina in our eyes, and especially the ratio between the different types of cones may differ. For reference: ITU BT.709:
ITU BT.601:
I did the test by quickly moving a small gray bar on a bright red, bright green and bright blue background, and adjusting the gray until it blended in just as much as possible. I also repeated that test with other shades. I repeated the test on different displays, even one with a fixed gamma factor of 3.0, but it all looks the same to me. More over, the ITU coefficients literally are wrong for my eyes. And yes, I presumably have a normal color vision. |
|||
|
|
|
To determine the brightness of a color with R, I convert the RGB system color in HSV system color. In my script, I use the HEX system code before for other reason, but you can start also with RGB system code with Here is this part of my code:
|
|||
|
|
|
Please define brightness. If you're looking for how close to white the color is you can use Euclidean Distance from (255, 255, 255) |
|||
|
|

