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.
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- CV2 Image Error: error: (-215:Assertion failed) !s
- DBGrid - How to set an individual background color
- Replace image attributes for lazyload plugin on im
相关文章
- Use savefig in Python with string and iterative in
- Where does this quality loss on Images come from?
- Specifying image dimensions in HTML vs CSS for pag
- How to insert pictures into each individual bar in
- Emacs/xterm color annoyance on Linux
- How do I append metadata to an image in Matlab?
- Img url to dataurl using JavaScript
- Click an image, get coordinates
Do you mean brightness? Perceived brightness? Luminance?
(0.2126*R + 0.7152*G + 0.0722*B)
[1](0.299*R + 0.587*G + 0.114*B)
[2]→sqrt( 0.241*R^2 + 0.691*G^2 + 0.068*B^2 )
sqrt( 0.299*R^2 + 0.587*G^2 + 0.114*B^2 )
(thanks to @MatthewHerbst) [3]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 converting sRGB images, as used in browsers etc., to grayscale.
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++.
Please define brightness. If you're looking for how close to white the color is you can use Euclidean Distance from (255, 255, 255)
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)
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
rgb2hsv {grDevices}
. The documentation is here.Here is this part of my code: