|
1 | | -import { useMode, modeOklch, modeRgb, formatHex, clampChroma } from 'culori/fn'; |
| 1 | +import { |
| 2 | + clampChroma, |
| 3 | + formatHex, |
| 4 | + modeLrgb, |
| 5 | + modeOklch, |
| 6 | + modeRgb, |
| 7 | + useMode, |
| 8 | + wcagContrast, |
| 9 | + type Oklch, |
| 10 | +} from 'culori/fn'; |
2 | 11 |
|
3 | 12 | const rgb = useMode(modeRgb); |
4 | 13 | export const oklch = useMode(modeOklch); |
| 14 | +// We need to initialise LRGB support for culori’s `wcagContrast()` method. |
| 15 | +useMode(modeLrgb); |
5 | 16 |
|
6 | | -/** Convert an OKLCH color to an RGB hex code. */ |
7 | | -export const oklchToHex = (l: number, c: number, h: number) => { |
8 | | - const okLchColor = oklch(`oklch(${l}% ${c} ${h})`)!; |
| 17 | +/** Convert a culori OKLCH color object to an RGB hex code. */ |
| 18 | +const oklchColorToHex = (okLchColor: Oklch) => { |
9 | 19 | const rgbColor = rgb(clampChroma(okLchColor, 'oklch')); |
10 | 20 | return formatHex(rgbColor); |
11 | 21 | }; |
| 22 | +/** Construct a culori OKLCH color object from LCH parameters. */ |
| 23 | +const oklchColorFromParts = (l: number, c: number, h: number) => oklch(`oklch(${l}% ${c} ${h})`)!; |
| 24 | +/** Convert OKLCH parameters to an RGB hex code. */ |
| 25 | +export const oklchToHex = (l: number, c: number, h: number) => |
| 26 | + oklchColorToHex(oklchColorFromParts(l, c, h)); |
| 27 | + |
| 28 | +/** |
| 29 | + * Ensure a text colour passes a contrast threshold against a specific background colour. |
| 30 | + * If necessary, colours will be darkened/lightened to increase contrast until the threshold is passed. |
| 31 | + * |
| 32 | + * @param text The text colour to adjust if necessary. |
| 33 | + * @param bg The background colour to test contrast against. |
| 34 | + * @param threshold The minimum contrast ratio required. Defaults to `4.5` to meet WCAG AA standards. |
| 35 | + * @returns The adjusted text colour as a culori OKLCH color object. |
| 36 | + */ |
| 37 | +const contrastColor = (text: Oklch, bg: Oklch, threshold = 4.5): Oklch => { |
| 38 | + /** Clone of the input foreground colour to mutate. */ |
| 39 | + const fgColor = { ...text }; |
| 40 | + // Brighten text in dark mode, darken text in light mode. |
| 41 | + const increment = fgColor.l > bg.l ? 0.005 : -0.005; |
| 42 | + while (wcagContrast(fgColor, bg) < threshold && fgColor.l < 100 && fgColor.l > 0) { |
| 43 | + fgColor.l += increment; |
| 44 | + } |
| 45 | + return fgColor; |
| 46 | +}; |
12 | 47 |
|
13 | 48 | /** Generate dark and light palettes based on user-selected hue and chroma values. */ |
14 | 49 | export function getPalettes(config: { |
15 | 50 | accent: { hue: number; chroma: number }; |
16 | 51 | gray: { hue: number; chroma: number }; |
| 52 | + minimumContrast?: number; |
17 | 53 | }) { |
18 | 54 | const { |
19 | 55 | accent: { hue: ah, chroma: ac }, |
20 | 56 | gray: { hue: gh, chroma: gc }, |
| 57 | + minimumContrast: mc, |
21 | 58 | } = config; |
22 | | - return { |
| 59 | + |
| 60 | + const palettes = { |
23 | 61 | dark: { |
24 | 62 | // Accents |
25 | | - 'accent-low': oklchToHex(25.94, ac / 3, ah), |
26 | | - accent: oklchToHex(52.28, ac, ah), |
27 | | - 'accent-high': oklchToHex(83.38, ac / 3, ah), |
| 63 | + 'accent-low': oklchColorFromParts(25.94, ac / 3, ah), |
| 64 | + accent: oklchColorFromParts(52.28, ac, ah), |
| 65 | + 'accent-high': oklchColorFromParts(83.38, ac / 3, ah), |
28 | 66 | // Grays |
29 | | - white: oklchToHex(100, 0, 0), |
30 | | - 'gray-1': oklchToHex(94.77, gc / 2.5, gh), |
31 | | - 'gray-2': oklchToHex(81.34, gc / 2, gh), |
32 | | - 'gray-3': oklchToHex(63.78, gc, gh), |
33 | | - 'gray-4': oklchToHex(46.01, gc, gh), |
34 | | - 'gray-5': oklchToHex(34.09, gc, gh), |
35 | | - 'gray-6': oklchToHex(27.14, gc, gh), |
36 | | - black: oklchToHex(20.94, gc / 2, gh), |
| 67 | + white: oklchColorFromParts(100, 0, 0), |
| 68 | + 'gray-1': oklchColorFromParts(94.77, gc / 2.5, gh), |
| 69 | + 'gray-2': oklchColorFromParts(81.34, gc / 2, gh), |
| 70 | + 'gray-3': oklchColorFromParts(63.78, gc, gh), |
| 71 | + 'gray-4': oklchColorFromParts(46.01, gc, gh), |
| 72 | + 'gray-5': oklchColorFromParts(34.09, gc, gh), |
| 73 | + 'gray-6': oklchColorFromParts(27.14, gc, gh), |
| 74 | + black: oklchColorFromParts(20.94, gc / 2, gh), |
37 | 75 | }, |
38 | 76 | light: { |
39 | 77 | // Accents |
40 | | - 'accent-low': oklchToHex(87.81, ac / 4, ah), |
41 | | - accent: oklchToHex(52.95, ac, ah), |
42 | | - 'accent-high': oklchToHex(31.77, ac / 2, ah), |
| 78 | + 'accent-low': oklchColorFromParts(87.81, ac / 4, ah), |
| 79 | + accent: oklchColorFromParts(52.95, ac, ah), |
| 80 | + 'accent-high': oklchColorFromParts(31.77, ac / 2, ah), |
43 | 81 | // Grays |
44 | | - white: oklchToHex(20.94, gc / 2, gh), |
45 | | - 'gray-1': oklchToHex(27.14, gc, gh), |
46 | | - 'gray-2': oklchToHex(34.09, gc, gh), |
47 | | - 'gray-3': oklchToHex(46.01, gc, gh), |
48 | | - 'gray-4': oklchToHex(63.78, gc, gh), |
49 | | - 'gray-5': oklchToHex(81.34, gc / 2, gh), |
50 | | - 'gray-6': oklchToHex(94.77, gc / 2.5, gh), |
51 | | - 'gray-7': oklchToHex(97.35, gc / 5, gh), |
52 | | - black: oklchToHex(100, 0, 0), |
| 82 | + white: oklchColorFromParts(20.94, gc / 2, gh), |
| 83 | + 'gray-1': oklchColorFromParts(27.14, gc, gh), |
| 84 | + 'gray-2': oklchColorFromParts(34.09, gc, gh), |
| 85 | + 'gray-3': oklchColorFromParts(46.01, gc, gh), |
| 86 | + 'gray-4': oklchColorFromParts(63.78, gc, gh), |
| 87 | + 'gray-5': oklchColorFromParts(81.34, gc / 2, gh), |
| 88 | + 'gray-6': oklchColorFromParts(94.77, gc / 2.5, gh), |
| 89 | + 'gray-7': oklchColorFromParts(97.35, gc / 5, gh), |
| 90 | + black: oklchColorFromParts(100, 0, 0), |
53 | 91 | }, |
54 | 92 | }; |
| 93 | + |
| 94 | + // Ensure text shades have sufficient contrast against common background colours. |
| 95 | + |
| 96 | + // Dark mode: |
| 97 | + // `gray-2` is used against `gray-5` in inline code snippets. |
| 98 | + palettes.dark['gray-2'] = contrastColor(palettes.dark['gray-2'], palettes.dark['gray-5'], mc); |
| 99 | + // `gray-3` is used in the table of contents. |
| 100 | + palettes.dark['gray-3'] = contrastColor(palettes.dark['gray-3'], palettes.dark.black, mc); |
| 101 | + |
| 102 | + // Light mode: |
| 103 | + // `accent` is used for links and link buttons and can be slightly under 7:1 for some hues. |
| 104 | + palettes.light.accent = contrastColor(palettes.light.accent, palettes.light['gray-6'], mc); |
| 105 | + // `gray-2` is used against `gray-6` in inline code snippets. |
| 106 | + palettes.light['gray-2'] = contrastColor(palettes.light['gray-2'], palettes.light['gray-6'], mc); |
| 107 | + // `gray-3` is used in the table of contents. |
| 108 | + palettes.light['gray-3'] = contrastColor(palettes.light['gray-3'], palettes.light.black, mc); |
| 109 | + |
| 110 | + // Convert the palette from OKLCH to RGB hex codes. |
| 111 | + return { |
| 112 | + dark: Object.fromEntries( |
| 113 | + Object.entries(palettes.dark).map(([key, color]) => [key, oklchColorToHex(color)]) |
| 114 | + ) as Record<keyof typeof palettes.dark, string>, |
| 115 | + light: Object.fromEntries( |
| 116 | + Object.entries(palettes.light).map(([key, color]) => [key, oklchColorToHex(color)]) |
| 117 | + ) as Record<keyof typeof palettes.light, string>, |
| 118 | + }; |
55 | 119 | } |
56 | 120 |
|
57 | 121 | /* |
|
0 commit comments