|
| 1 | +import * as culori from 'culori' |
| 2 | +import { |
| 3 | + ComponentValue, |
| 4 | + isFunctionNode, |
| 5 | + isTokenNode, |
| 6 | + isWhitespaceNode, |
| 7 | + parseComponentValue, |
| 8 | +} from '@csstools/css-parser-algorithms' |
| 9 | +import { |
| 10 | + isTokenComma, |
| 11 | + isTokenHash, |
| 12 | + isTokenIdent, |
| 13 | + isTokenNumber, |
| 14 | + isTokenNumeric, |
| 15 | + isTokenPercentage, |
| 16 | + stringify, |
| 17 | + tokenize, |
| 18 | +} from '@csstools/css-tokenizer' |
| 19 | + |
| 20 | +const COLOR_FN = /^(rgba?|hsla?|hwb|(ok)?(lab|lch)|color)$/i |
| 21 | + |
| 22 | +export type KeywordColor = 'currentColor' |
| 23 | +export type ParsedColor = culori.Color | KeywordColor | null |
| 24 | + |
| 25 | +export function colorFromString(value: string): ParsedColor { |
| 26 | + let tokens = tokenize({ css: value }) |
| 27 | + let cv = parseComponentValue(tokens) |
| 28 | + let color = colorFromComponentValue(cv) |
| 29 | + |
| 30 | + return color |
| 31 | +} |
| 32 | + |
| 33 | +export function colorFromComponentValue(cv: ComponentValue): ParsedColor { |
| 34 | + if (isTokenNode(cv)) { |
| 35 | + if (isTokenIdent(cv.value)) { |
| 36 | + let str = cv.value[4].value.toLowerCase() |
| 37 | + |
| 38 | + if (str === 'currentcolor') return 'currentColor' |
| 39 | + |
| 40 | + if (str === 'transparent') { |
| 41 | + // We omit rgb channels instead of using transparent black because we |
| 42 | + // use `culori.interpolate` to mix colors and it handles `transparent` |
| 43 | + // differently from the spec (all channels are mixed, not just alpha) |
| 44 | + return culori.parse('rgb(none none none / 0.5)') |
| 45 | + } |
| 46 | + |
| 47 | + if (str in culori.colorsNamed) { |
| 48 | + return culori.parseNamed(str as keyof typeof culori.colorsNamed) ?? null |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // |
| 53 | + else if (isTokenHash(cv.value)) { |
| 54 | + let hex = cv.value[4].value.toLowerCase() |
| 55 | + |
| 56 | + return culori.parseHex(hex) ?? null |
| 57 | + } |
| 58 | + |
| 59 | + return null |
| 60 | + } |
| 61 | + |
| 62 | + // |
| 63 | + else if (isFunctionNode(cv)) { |
| 64 | + let fn = cv.getName() |
| 65 | + |
| 66 | + if (COLOR_FN.test(fn)) { |
| 67 | + return culori.parse(stringify(...cv.tokens())) ?? null |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return null |
| 72 | +} |
| 73 | + |
| 74 | +export function equivalentColorFromString(value: string): string { |
| 75 | + let color = colorFromString(value) |
| 76 | + let equivalent = computeEquivalentColor(color) |
| 77 | + |
| 78 | + return equivalent ?? value |
| 79 | +} |
| 80 | + |
| 81 | +function computeEquivalentColor(color: ParsedColor): string | null { |
| 82 | + if (!color) return null |
| 83 | + if (typeof color === 'string') return null |
| 84 | + if (!culori.inGamut('rgb')(color)) return null |
| 85 | + |
| 86 | + if (color.alpha === undefined || color.alpha === 1) { |
| 87 | + return culori.formatHex(color) |
| 88 | + } |
| 89 | + |
| 90 | + return culori.formatHex8(color) |
| 91 | +} |
| 92 | + |
| 93 | +export function colorMixFromString(value: string): ParsedColor { |
| 94 | + let tokens = tokenize({ css: value }) |
| 95 | + let cv = parseComponentValue(tokens) |
| 96 | + let color = colorMixFromComponentValue(cv) |
| 97 | + |
| 98 | + return color |
| 99 | +} |
| 100 | + |
| 101 | +export function colorMixFromComponentValue(cv: ComponentValue): ParsedColor { |
| 102 | + if (!isFunctionNode(cv)) return null |
| 103 | + if (cv.getName() !== 'color-mix') return null |
| 104 | + |
| 105 | + let state: 'in' | 'colorspace' | 'colors' = 'in' |
| 106 | + let colorspace: string = '' |
| 107 | + let colors: Array<culori.Color | number> = [] |
| 108 | + |
| 109 | + for (let i = 0; i < cv.value.length; ++i) { |
| 110 | + let value = cv.value[i] |
| 111 | + |
| 112 | + if (isWhitespaceNode(value)) continue |
| 113 | + |
| 114 | + if (state === 'in') { |
| 115 | + if (isTokenNode(value)) { |
| 116 | + if (isTokenIdent(value.value)) { |
| 117 | + if (value.value[4].value === 'in') { |
| 118 | + state = 'colorspace' |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } else if (state === 'colorspace') { |
| 123 | + if (isTokenNode(value)) { |
| 124 | + if (isTokenIdent(value.value)) { |
| 125 | + if (colorspace !== '') return null |
| 126 | + |
| 127 | + colorspace = value.value[4].value |
| 128 | + } else if (isTokenComma(value.value)) { |
| 129 | + state = 'colors' |
| 130 | + } |
| 131 | + } |
| 132 | + } else if (state === 'colors') { |
| 133 | + if (isTokenNode(value)) { |
| 134 | + if (isTokenPercentage(value.value)) { |
| 135 | + colors.push(value.value[4].value / 100) |
| 136 | + continue |
| 137 | + } else if (isTokenNumber(value.value)) { |
| 138 | + colors.push(value.value[4].value) |
| 139 | + continue |
| 140 | + } else if (isTokenComma(value.value)) { |
| 141 | + continue |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + let color = colorFromComponentValue(value) |
| 146 | + if (!color) return null |
| 147 | + if (typeof color === 'string') return null |
| 148 | + colors.push(color) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + let t = culori.interpolate(colors, colorspace as any) |
| 153 | + |
| 154 | + return t(0.5) ?? null |
| 155 | +} |
0 commit comments