## Formula to determine brightness of RGB color

### Question

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.

### Accepted Answer

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]

### Popular Answer

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
```

Read more... Read less...

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.

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++.

```
// sRGB luminance(Y) values
const double rY = 0.212655;
const double gY = 0.715158;
const double bY = 0.072187;
// Inverse of sRGB "gamma" function. (approx 2.2)
double inv_gam_sRGB(int ic) {
double c = ic/255.0;
if ( c <= 0.04045 )
return c/12.92;
else
return pow(((c+0.055)/(1.055)),2.4);
}
// sRGB "gamma" function (approx 2.2)
int gam_sRGB(double v) {
if(v<=0.0031308)
v *= 12.92;
else
v = 1.055*pow(v,1.0/2.4)-0.055;
return int(v*255+0.5); // This is correct in C++. Other languages may not
// require +0.5
}
// GRAY VALUE ("brightness")
int gray(int r, int g, int b) {
return gam_sRGB(
rY*inv_gam_sRGB(r) +
gY*inv_gam_sRGB(g) +
bY*inv_gam_sRGB(b)
);
}
```

*The "Accepted" Answer is Incorrect and Incomplete*

The only answers that are accurate are the @jive-dadson and @EddingtonsMonkey answers, and in support @nils-pipenbrinck. The other answers *(including the accepted)* are linking to or citing sources that are either wrong, irrelevant, obsolete, or broken.

### Briefly:

- sRGB must be
*LINEARIZED*before applying the coefficients. - Luminance (L or Y) is linear as is light.
- Perceived lightness (L*) is nonlinear as is human perception.
- HSV and HSL are not even remotely accurate in terms of perception.
- The IEC standard for sRGB specifies a threshold of 0.04045 it is
*NOT*0.03928 (that was from an obsolete early draft). - The be useful
*(i.e. relative to perception)*, Euclidian distances require a perceptually uniform Cartesian vector space such as CIELAB.*sRGB is not one.*

## What follows is a correct and complete answer:

Because this thread appears highly in search engines, I am adding this answer to clarify the various misconceptions on the subject.

**Brightness** is a perceptual attribute, it does not have a direct measure.

**Perceived lightness** is measured by some vision models such as CIELAB, here L* (Lstar) is a measure of *perceptual lightness*, and is non-linear to approximate the human vision non-linear response curve.

**Luminance** is a linear measure of light, spectrally weighted for normal vision but not adjusted for non-linear perception of lightness.

**Luma** (*Y´* prime) is a gamma encoded, weighted signal used in some video encodings. It is not to be confused with linear luminance.

**Gamma** or transfer curve (TRC) is a curve that is often similar to the perceptual curve, and is commonly applied to image data for storage or broadcast to reduce perceived noise and/or improve data utilization (and related reasons).

**To determine perceived lightness**, first convert gamma encoded R´G´B´ image values to linear luminance (** L** or

**) and then to non-linear perceived lightness (**

`Y`

**)**

`L*`

## TO FIND LUMINANCE:

*...Because apparently it was lost somewhere...*

### Step One:

Convert all sRGB 8 bit integer values to decimal 0.0-1.0

```
vR = sR / 255;
vG = sG / 255;
vB = sB / 255;
```

### Step Two:

Convert a gamma encoded RGB to a linear value. sRGB (computer standard) for instance requires a power curve of approximately V^2.2, though the "accurate" transform is:

*Where V´ is the gamma-encoded R, G, or B channel of sRGB.*

Pseudocode:

```
function sRGBtoLin(colorChannel) {
// Send this function a decimal sRGB gamma encoded color value
// between 0.0 and 1.0, and it returns a linearized value.
if ( colorChannel <= 0.04045 ) {
return colorChannel / 12.92;
} else {
return pow((( colorChannel + 0.055)/1.055),2.4));
}
}
```

### Step Three:

To find Luminance (Y) apply the standard coefficients for sRGB:

Pseudocode using above functions:

```
Y = (0.2126 * sRGBtoLin(vR) + 0.7152 * sRGBtoLin(vG) + 0.0722 * sRGBtoLin(vB))
```

## TO FIND PERCEIVED LIGHTNESS:

### Step Four:

Take luminance Y from above, and transform to L*

```
function YtoLstar(Y) {
// Send this function a luminance value between 0.0 and 1.0,
// and it returns L* which is "perceptual lightness"
if ( Y <= (216/24389) { // The CIE standard states 0.008856 but 216/24389 is the intent for 0.008856451679036
return Y * (24389/27); // The CIE standard states 903.3, but 24389/27 is the intent, making 903.296296296296296
} else {
return pow(Y,(1/3)) * 116 - 16;
}
}
```

L* is a value from 0 (black) to 100 (white) where 50 is the perceptual "middle grey". L* = 50 is the equivalent of Y = 18.4, or in other words an 18% grey card, representing the middle of a photographic exposure (Ansel Adams zone V).

### References:

`IEC 61966-2-1:1999 Standard`

`Wikipedia sRGB`

`Wikipedia CIELAB`

`Wikipedia CIEXYZ`

Charles Poynton's Gamma FAQ

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.

```
/**
* Calculate relative luminance in sRGB colour space for use in WCAG 2.0 compliance
* @link http://www.w3.org/TR/WCAG20/#relativeluminancedef
* @param string $col A 3 or 6-digit hex colour string
* @return float
* @author Marcus Bointon <[email protected]>
*/
function relativeluminance($col) {
//Remove any leading #
$col = trim($col, '#');
//Convert 3-digit to 6-digit
if (strlen($col) == 3) {
$col = $col[0] . $col[0] . $col[1] . $col[1] . $col[2] . $col[2];
}
//Convert hex to 0-1 scale
$components = array(
'r' => hexdec(substr($col, 0, 2)) / 255,
'g' => hexdec(substr($col, 2, 2)) / 255,
'b' => hexdec(substr($col, 4, 2)) / 255
);
//Correct for sRGB
foreach($components as $c => $v) {
if ($v <= 0.04045) {
$components[$c] = $v / 12.92;
} else {
$components[$c] = pow((($v + 0.055) / 1.055), 2.4);
}
}
//Calculate relative luminance using ITU-R BT. 709 coefficients
return ($components['r'] * 0.2126) + ($components['g'] * 0.7152) + ($components['b'] * 0.0722);
}
/**
* Calculate contrast ratio acording to WCAG 2.0 formula
* Will return a value between 1 (no contrast) and 21 (max contrast)
* @link http://www.w3.org/TR/WCAG20/#contrast-ratiodef
* @param string $c1 A 3 or 6-digit hex colour string
* @param string $c2 A 3 or 6-digit hex colour string
* @return float
* @author Marcus Bointon <[email protected]>
*/
function contrastratio($c1, $c2) {
$y1 = relativeluminance($c1);
$y2 = relativeluminance($c2);
//Arrange so $y1 is lightest
if ($y1 < $y2) {
$y3 = $y1;
$y1 = $y2;
$y2 = $y3;
}
return ($y1 + 0.05) / ($y2 + 0.05);
}
```