Formula to determine brightness of RGB color

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.


Do you mean brightness? Perceived brightness? Luminance?

  • Luminance (standard for certain colour spaces): (0.2126*R + 0.7152*G + 0.0722*B) [1]
  • Luminance (perceived option 1): (0.299*R + 0.587*G + 0.114*B) [2]
  • Luminance (perceived option 2, slower to calculate): 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 think what you are looking for is the RGB -> Luma conversion formula.

    Photometric/digital ITU BT.709:

    Y = 0.2126 R + 0.7152 G + 0.0722 B
    

    Digital ITU BT.601 (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
    

    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)

    0.2126 * R + 0.7152 * G + 0.0722 * B
    

    2nd picture - http://www.w3.org/TR/AERT#color-contrast

    0.299 * R + 0.587 * G + 0.114 * B
    

    3rd picture - HSP Color Model

    sqrt(0.299 * R^2 + 0.587 * G^2 + 0.114 * B^2)
    

    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.

    感知亮度算法比较

    链接地址: http://www.djcxy.com/p/15260.html

    上一篇: 我如何着色Python日志输出?

    下一篇: 确定RGB颜色亮度的公式