Color Analysis
Complete reference for color analysis and accessibility functions
The analyze API provides color analysis, accessibility checking, and validation functions. All functions are available via api.analyze.
Luminance & Brightness
relativeLuminance(rgb: RGB): number
Calculate WCAG relative luminance (0-1).
Returns: number (0-1)
brightness(rgb: RGB): number
Simple brightness calculation (0-255).
Formula: (r + g + b) / 3
perceivedBrightness(rgb: RGB): number
Perceptually-weighted brightness (0-255).
Formula: Uses weighted coefficients (0.299R + 0.587G + 0.114B)
isLight(rgb: RGB): boolean
Check if color is light (luminance > 0.5).
isDark(rgb: RGB): boolean
Check if color is dark (luminance ≤ 0.5).
lightOrDark(rgb: RGB): string
Returns "light" or "dark".
WCAG Contrast Analysis
contrastRatio(fg: RGB, bg: RGB): number
Calculate WCAG 2.1 contrast ratio (1-21).
WCAG Requirements:
- AA Normal Text: 4.5:1
- AA Large Text: 3:1
- AAA Normal Text: 7:1
- AAA Large Text: 4.5:1
checkContrast(fg: RGB, bg: RGB): WCAGContrastResult
Full WCAG compliance report.
Return Type:
meetsContrastAA(fg: RGB, bg: RGB): boolean
Check if meets AA normal text (4.5:1).
meetsContrastAAA(fg: RGB, bg: RGB): boolean
Check if meets AAA normal text (7:1).
meetsLargeTextAA(fg: RGB, bg: RGB): boolean
Check if meets AA large text (3:1).
meetsLargeTextAAA(fg: RGB, bg: RGB): boolean
Check if meets AAA large text (4.5:1).
contrastWithWhite(rgb: RGB): number
Shorthand for contrast against white.
contrastWithBlack(rgb: RGB): number
Shorthand for contrast against black.
suggestTextColor(bg: RGB): RGB
Get optimal text color (black or white) for background.
findAccessibleShade(color: RGB, bg: RGB, targetContrast: number): RGB
Find accessible variant of color that meets target contrast.
APCA Contrast (Modern)
APCA (Accessible Perceptual Contrast Algorithm) is a modern alternative to WCAG contrast.
apcaContrast(text: RGB, bg: RGB): number
Calculate APCA Lc value.
Returns: Lc value (-108 to +108)
- Positive: dark text on light background
- Negative: light text on dark background
checkApca(text: RGB, bg: RGB): APCAResult
Full APCA analysis.
meetsApcaBodyText(text: RGB, bg: RGB): boolean
Check if meets APCA body text requirement (Lc 75+).
meetsApcaLargeText(text: RGB, bg: RGB): boolean
Check if meets APCA large text requirement (Lc 60+).
meetsApcaNonText(text: RGB, bg: RGB): boolean
Check if meets APCA non-text/UI requirement (Lc 45+).
Color Difference (Delta-E)
deltaE2000(color1: RGB, color2: RGB): number
Calculate CIE Delta-E 2000 color difference.
Interpretation:
- < 1.0: Not perceptible
- 1.0-2.0: Perceptible on close observation
- 2.0-10.0: Perceptible at a glance
- 10.0-50.0: Different colors
- > 50.0: Opposite colors
analyzeColorDifference(c1: RGB, c2: RGB): DeltaEResult
Detailed color difference analysis.
areColorsSimilar(c1: RGB, c2: RGB): boolean
Check if colors are similar (ΔE < 10).
areColorsIdentical(c1: RGB, c2: RGB): boolean
Check if colors are identical (ΔE < 1).
Adaptive Colors
adaptiveTextColor(bg: RGB): RGB
Get optimal text color using APCA.
More sophisticated than suggestTextColor - uses APCA algorithm.
ensureContrast(fg: RGB, bg: RGB, minContrast: number): RGB
Adjust foreground to meet minimum contrast.
generateContrastingColors(bg: RGB, count: number, minContrast: number): RGB[]
Generate multiple colors with guaranteed contrast.
blendWithBackground(fg: RGB, bg: RGB, opacity: number): RGB
Blend foreground with background using alpha.
isSafeForColorBlind(c1: RGB, c2: RGB): boolean
Check if color pair is distinguishable for color blind users.
generateAccessiblePairs(base: RGB, count: number): AccessibleColorPair[]
Generate accessible foreground/background pairs.
Validation
isValidHex(hex: string): boolean
Quick hex validation.
Accepts: #RGB, #RRGGBB, #RRGGBBAA, with or without #
validateHex(hex: string): ValidationResult
Detailed hex validation.
validateColorString(str: string): ValidationResult
Validate any color string format.
normalizeHex(hex: string): string
Normalize hex to standard format.
isGrayscale(rgb: RGB): boolean
Check if color is grayscale.
isNearGrayscale(rgb: RGB, tolerance: number): boolean
Check if color is near grayscale within tolerance.
colorType(rgb: RGB): string
Classify color type.
Returns: "black", "white", "grayscale", or "chromatic"