File Exchange

image thumbnail

functions for computing relative luminance in W3C standard

version (2.77 KB) by Alessandro Farini
two functions computing relative luminance and contrast ratio using the WCAG 2.0

1 Download

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

Comments and Ratings (0)

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