@tangible/ui 0.2.20 → 0.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,6 +46,32 @@ function getRelativeLuminance(r, g, b) {
46
46
  0.7152 * gammaCorrect(g) +
47
47
  0.0722 * gammaCorrect(b));
48
48
  }
49
+ /**
50
+ * WCAG 2.x contrast ratio between two colours, 1 (identical) to 21
51
+ * (black on white). Order-independent — the lighter colour is always the
52
+ * numerator. Compare against 4.5 (AA text), 3 (AA large text / SC 1.4.11
53
+ * non-text), 7 (AAA text).
54
+ *
55
+ * @param hexA - Either colour in #rgb/#rrggbb (alpha digits ignored)
56
+ * @param hexB - The other colour
57
+ * @returns The ratio, or `NaN` when either input is not a hex colour — so a
58
+ * `>= 4.5` check on bad input fails rather than passes. No runtime throws,
59
+ * matching `getContrastTone`.
60
+ *
61
+ * Same scope rules as `getContrastTone`: for user-selected colours and
62
+ * consumer-side theme tooling (e.g. validating a generated ladder against a
63
+ * page background), not for choosing a component's own theming at runtime.
64
+ */
65
+ export function contrastRatio(hexA, hexB) {
66
+ const a = parseHex(hexA);
67
+ const b = parseHex(hexB);
68
+ if (!a || !b)
69
+ return NaN;
70
+ const la = getRelativeLuminance(...a);
71
+ const lb = getRelativeLuminance(...b);
72
+ const [lighter, darker] = la >= lb ? [la, lb] : [lb, la];
73
+ return (lighter + 0.05) / (darker + 0.05);
74
+ }
49
75
  /**
50
76
  * Determine whether light or dark foreground provides better contrast.
51
77
  * Uses WCAG relative luminance with 0.179 threshold.