getLuminance
Calculates the relative luminance of an RGB color according to WCAG 2.0 specifications.Parameters
The RGB color to calculate luminance for. Must have
red, green, and blue properties (0-255).Returns
The relative luminance value in the range [0, 1], where 0 represents the darkest possible color (black) and 1 represents the lightest (white).
Example
Technical details
The luminance calculation follows the WCAG 2.0 specification:- RGB values are normalized to the range [0, 1]
- Each channel is linearized using the sRGB transfer function
- The final luminance is calculated as:
0.2126 * R + 0.7152 * G + 0.0722 * B
getContrastRatio
Calculates the contrast ratio between two RGB colors according to WCAG 2.0 specifications.Parameters
The first RGB color. Must have
red, green, and blue properties (0-255).The second RGB color. Must have
red, green, and blue properties (0-255).Returns
The contrast ratio in the range [1, 21], where 1 represents no contrast (same colors) and 21 represents maximum contrast (black and white).
Example
WCAG contrast requirements
The WCAG 2.0 defines minimum contrast ratios for text accessibility: Level AA (minimum)- Normal text: 4.5:1
- Large text (18pt+ or 14pt+ bold): 3:1
- Normal text: 7:1
- Large text: 4.5:1
Practical usage
Technical details
The contrast ratio is calculated as:- L1 is the relative luminance of the lighter color
- L2 is the relative luminance of the darker color
- The 0.05 offset accounts for ambient light