functions for computing relative luminance in W3C standard

two functions computing relative luminance and contrast ratio using the WCAG 2.0
459 Downloads
Updated 11 Aug 2009

View License

Relative Luminance is the relative brightness of any point in a colorspace, normalized to 0 for darkest black and 1 for lightest white. For the sRGB colorspace, the relative luminance of a color is defined as

L = 0.2126 * R + 0.7152 * G + 0.0722 * B

where R, G and B are defined as:

if RsRGB <= 0.03928 then R = RsRGB/12.92 else R = ((RsRGB+0.055)/1.055) ^ 2.4
if GsRGB <= 0.03928 then G = GsRGB/12.92 else G = ((GsRGB+0.055)/1.055) ^ 2.4
if BsRGB <= 0.03928 then B = BsRGB/12.92 else B = ((BsRGB+0.055)/1.055) ^ 2.4

RsRGB, GsRGB, and BsRGB are defined as:
RsRGB = R8bit/255
GsRGB = G8bit/255
BsRGB = B8bit/255

Contrast Ratio= (L1 + 0.05) / (L2 + 0.05), where
L1 is the relative luminance of the lighter of the colors, and
L2 is the relative luminance of the darker of the colors.
Note Contrast ratios can range from 1 to 21 (commonly written 1:1 to 21:1). the input is the rgb value (from 0 to 255) of the ligheter and the darker colors.
The WCAG guidelines indicate that contrast ratio should be at least 4.5:1

Cite As

Alessandro Farini (2024). functions for computing relative luminance in W3C standard (https://www.mathworks.com/matlabcentral/fileexchange/24995-functions-for-computing-relative-luminance-in-w3c-standard), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0