| 1 | import type { Rgba } from './ir' |
| 2 | |
| 3 | /** |
| 4 | * Somewhere to record a color string no parser understood. A parameter rather |
| 5 | * than module state, so calls made outside an export run cannot accumulate |
| 6 | * into the next one. |
| 7 | */ |
| 8 | export type UnparsedColors = Set<string> | undefined |
| 9 | |
| 10 | function numbers(body: string): number[] { |
| 11 | return body.split(/[\s,/]+/).filter(Boolean).map((token) => { |
| 12 | const n = Number.parseFloat(token) |
| 13 | return token.endsWith('%') |
| 14 | ? n / 100 |
| 15 | : n |
| 16 | }) |
| 17 | } |
| 18 | |
| 19 | function srgbChannel(v: number): number { |
| 20 | // The sRGB transfer function, for converting linear light back to 0-255. |
| 21 | const c = v <= 0.0031308 |
| 22 | ? v * 12.92 |
| 23 | : 1.055 * v ** (1 / 2.4) - 0.055 |
| 24 | return Math.max(0, Math.min(255, Math.round(c * 255))) |
| 25 | } |
| 26 | |
| 27 | /** Oklab to sRGB, per the CSS Color 4 conversion. */ |
| 28 | function oklabToRgb(L: number, a: number, b: number): [number, number, number] { |
| 29 | const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3 |
| 30 | const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3 |
| 31 | const s = (L - 0.0894841775 * a - 1.2914855480 * b) ** 3 |
| 32 | return [ |
| 33 | srgbChannel(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s), |
| 34 | srgbChannel(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s), |
| 35 | srgbChannel(-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s), |
| 36 | ] |
| 37 | } |
| 38 | |
| 39 | function hueToRgb(h: number, s: number, l: number): [number, number, number] { |
| 40 | const c = (1 - Math.abs(2 * l - 1)) * s |
| 41 | const hp = (((h % 360) + 360) % 360) / 60 |
| 42 | const x = c * (1 - Math.abs((hp % 2) - 1)) |
| 43 | const [r, g, b] |
| 44 | = hp < 1 |
| 45 | ? [c, x, 0] |
| 46 | : hp < 2 |
| 47 | ? [x, c, 0] |
| 48 | : hp < 3 |
| 49 | ? [0, c, x] |
| 50 | : hp < 4 |
| 51 | ? [0, x, c] |
| 52 | : hp < 5 |
| 53 | ? [x, 0, c] |
| 54 | : [c, 0, x] |
| 55 | const m = l - c / 2 |
| 56 | const to255 = (v: number) => Math.max(0, Math.min(255, Math.round((v + m) * 255))) |
| 57 | return [ |
| 58 | to255(r), |
| 59 | to255(g), |
| 60 | to255(b), |
| 61 | ] |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * A computed CSS color, in any syntax Chromium actually serializes: a theme |
| 66 | * authored in modern syntax keeps `oklch()`, `color()` or `hsl()` in the |
| 67 | * computed value, and dropping those loses every fill on the slide. |
| 68 | */ |
| 69 | export function parseColor(value: string | undefined, unparsed?: UnparsedColors): Rgba | undefined { |
| 70 | if (!value) |
| 71 | return undefined |
| 72 | const text = value.trim() |
| 73 | if (text === 'transparent') |
| 74 | return { r: 0, g: 0, b: 0, a: 0 } |
| 75 | |
| 76 | const fn = text.match(/^([a-z-]+)\(([^)]+)\)$/i) |
| 77 | if (!fn) { |
| 78 | unparsed?.add(text) |
| 79 | return undefined |
| 80 | } |
| 81 | const name = fn[1].toLowerCase() |
| 82 | |
| 83 | if (name === 'color') { |
| 84 | // Before the numeric guard: the first token is a color space name, so |
| 85 | // parsing the whole body as numbers would reject every `color()` value. |
| 86 | const tokens = fn[2].trim().split(/[\s,/]+/).filter(Boolean) |
| 87 | const channels = numbers(tokens.slice(1).join(' ')) |
| 88 | if (tokens[0] === 'srgb' && channels.length >= 3 && !channels.slice(0, 3).some(Number.isNaN)) { |
| 89 | return { |
| 90 | r: Math.round(channels[0] * 255), |
| 91 | g: Math.round(channels[1] * 255), |
| 92 | b: Math.round(channels[2] * 255), |
| 93 | a: channels.length > 3 && !Number.isNaN(channels[3]) |
| 94 | ? channels[3] |
| 95 | : 1, |
| 96 | } |
| 97 | } |
| 98 | unparsed?.add(text) |
| 99 | return undefined |
| 100 | } |
| 101 | |
| 102 | const parts = numbers(fn[2]) |
| 103 | if (parts.some(Number.isNaN)) { |
| 104 | unparsed?.add(text) |
| 105 | return undefined |
| 106 | } |
| 107 | const alpha = (index: number) => (parts.length > index |
| 108 | ? parts[index] |
| 109 | : 1) |
| 110 | |
| 111 | if (name === 'rgb' || name === 'rgba') { |
| 112 | if (parts.length < 3) |
| 113 | return undefined |
| 114 | // Percentage channels were divided by 100 above, so scale them back. |
| 115 | const channel = (v: number, raw: string) => (raw.includes('%') |
| 116 | ? v * 255 |
| 117 | : v) |
| 118 | const raw = fn[2].split(/[\s,/]+/).filter(Boolean) |
| 119 | return { |
| 120 | r: channel(parts[0], raw[0] ?? ''), |
| 121 | g: channel(parts[1], raw[1] ?? ''), |
| 122 | b: channel(parts[2], raw[2] ?? ''), |
| 123 | a: alpha(3), |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (name === 'hsl' || name === 'hsla') { |
| 128 | if (parts.length < 3) |
| 129 | return undefined |
| 130 | const [r, g, b] = hueToRgb(parts[0], parts[1], parts[2]) |
| 131 | return { |
| 132 | r, |
| 133 | g, |
| 134 | b, |
| 135 | a: alpha(3), |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (name === 'oklch' || name === 'oklab') { |
| 140 | if (parts.length < 3) |
| 141 | return undefined |
| 142 | const L = parts[0] |
| 143 | const [a, b] = name === 'oklch' |
| 144 | ? [parts[1] * Math.cos((parts[2] * Math.PI) / 180), parts[1] * Math.sin((parts[2] * Math.PI) / 180)] |
| 145 | : [parts[1], parts[2]] |
| 146 | const [r, g, bb] = oklabToRgb(L, a, b) |
| 147 | return { |
| 148 | r, |
| 149 | g, |
| 150 | b: bb, |
| 151 | a: alpha(3), |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | unparsed?.add(text) |
| 156 | return undefined |
| 157 | } |
| 158 | |
| 159 | /** Whether a color contributes any ink at all. */ |
| 160 | export function isVisible(color: Rgba | undefined): boolean { |
| 161 | return !!color && color.a > 0 |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Fold an element's effective opacity into a color's own alpha: DrawingML has |
| 166 | * no element-level or group opacity, only per-fill alpha. The walker compounds |
| 167 | * the value down the tree, since CSS `opacity` does not inherit. |
| 168 | */ |
| 169 | export function withOpacity(color: Rgba | undefined, opacity: number | undefined): Rgba | undefined { |
| 170 | if (!color) |
| 171 | return undefined |
| 172 | if (opacity === undefined || !Number.isFinite(opacity) || opacity >= 1) |
| 173 | return color |
| 174 | return { ...color, a: color.a * Math.max(0, opacity) } |
| 175 | } |
| 176 |