Formula to determine brightness of RGB color

前端 未结 20 3487
猫巷女王i
猫巷女王i 2020-11-21 23:16

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 toget

20条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 23:37

    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 here)

    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.

    Perceived brightness algorithm comparison

提交回复
热议问题