(
color: { r: number; g: number; b: number },
opacity: number = 1
)
| 204 | } |
| 205 | |
| 206 | export function formatHexAlpha( |
| 207 | color: { r: number; g: number; b: number }, |
| 208 | opacity: number = 1 |
| 209 | ): string { |
| 210 | const toHex = (n: number) => { |
| 211 | const i = Math.min(255, Math.max(0, Math.round(n * 255))) |
| 212 | return i.toString(16).padStart(2, '0').toUpperCase() |
| 213 | } |
| 214 | |
| 215 | const r = toHex(color.r) |
| 216 | const g = toHex(color.g) |
| 217 | const b = toHex(color.b) |
| 218 | |
| 219 | if (opacity >= 0.99) { |
| 220 | if (r[0] === r[1] && g[0] === g[1] && b[0] === b[1]) { |
| 221 | return `#${r[0]}${g[0]}${b[0]}` |
| 222 | } |
| 223 | return `#${r}${g}${b}` |
| 224 | } |
| 225 | |
| 226 | const a = toHex(opacity) |
| 227 | if (r[0] === r[1] && g[0] === g[1] && b[0] === b[1] && a[0] === a[1]) { |
| 228 | return `#${r[0]}${g[0]}${b[0]}${a[0]}` |
| 229 | } |
| 230 | |
| 231 | return `#${r}${g}${b}${a}` |
| 232 | } |
| 233 | |
| 234 | export function parseBackgroundShorthand(value: string) { |
| 235 | const result: { |
no test coverage detected