| FG Color | BG Color | Ratio | WCAG AA | WCAG AAA |
|---|
Analyze UI graphics and photos with k-means color quantization. Extracts dominant Hex, RGB, and HSL swatches with nearest Tailwind CSS color tokens, while computing an interactive relative luminance contrast matrix for legal WCAG compliance.
| FG Color | BG Color | Ratio | WCAG AA | WCAG AAA |
|---|
Technical guide on Euclidean clustering, sRGB gamma expansion, and legal accessibility compliance.
Design system engineering requires rigorous adherence to accessibility standards. The Web Content Accessibility Guidelines (WCAG 2.1 / 3.0) define strict contrast thresholds between foreground text and background surfaces. This utility extracts dominant image colors using k-means vector clustering and computes a complete relative luminance contrast matrix to guarantee ADA and Section 508 accessibility compliance.
To extract the most visually prominent palette from millions of pixels, the canvas pixel buffer is sampled into 3D RGB vectors. K-Means clustering iteratively groups neighboring colors around k centroids, minimizing intra-cluster variance until convergence. The resulting centroid coordinates represent the mathematically dominant swatches.
Human eyes perceive green, red, and blue with vastly different sensitivity. Relative luminance L is defined in WCAG 2.1 by first converting nonlinear sRGB color components into linear values: C_lin = C_srgb <= 0.04045 ? C_srgb / 12.92 : ((C_srgb + 0.055) / 1.055)^2.4. Luminance is computed as L = 0.2126 * R_lin + 0.7152 * G_lin + 0.0722 * B_lin.
Contrast ratio between two surfaces is calculated as (L1 + 0.05) / (L2 + 0.05), where L1 is the lighter surface. Level AA compliance mandates a minimum 4.5:1 ratio for normal body text and 3:1 for large text (>= 18pt or bold 14pt). Level AAA mandates a strict 7:1 ratio for body text.
| Parameter | Standard / Value | Engineering Scope |
|---|---|---|
| Clustering Algorithm | K-Means Color Quantization | Iterative 3D vector centroid convergence |
| Compliance Standard | W3C WCAG 2.1 Level AA & AAA | Relative luminance contrast verification |
| Gamma Decoding | sRGB IEC 61966-2-1 Transfer Curve | Nonlinear to linear photometric conversion |
| Export Tokens | Tailwind CSS v3/v4 Theme & CSS Vars | Ready-to-use design system tokens |
function getLuminance(r: number, g: number, b: number): number {
const [rs, gs, bs] = [r, g, b].map((c) => {
const s = c / 255;
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
});
return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;
}
export function getContrastRatio(rgb1: [number, number, number], rgb2: [number, number, number]): number {
const l1 = getLuminance(...rgb1);
const l2 = getLuminance(...rgb2);
const lighter = Math.max(l1, l2);
const darker = Math.min(l1, l2);
return (lighter + 0.05) / (darker + 0.05);
}Convert SVGs into clean React (JSX/TSX), Vue 3, Flutter, React Native, SwiftUI components and CSS Data URIs.
Generate complete iOS, Android Adaptive Icons, PWA, and Web Favicon asset packages with single-click ZIP download.
Transform code snippets, bash logs, and SQL into stunning social media cards with macOS window frames and gradients.