(value: string, alpha: string)
| 174 | * Apply opacity to a color using `color-mix`. |
| 175 | */ |
| 176 | export function withAlpha(value: string, alpha: string): string { |
| 177 | if (alpha === null) return value |
| 178 | |
| 179 | // Convert numeric values (like `0.5`) to percentages (like `50%`) so they |
| 180 | // work properly with `color-mix`. Assume anything that isn't a number is |
| 181 | // safe to pass through as-is, like `var(--my-opacity)`. |
| 182 | let alphaAsNumber = Number(alpha) |
| 183 | if (!Number.isNaN(alphaAsNumber)) { |
| 184 | alpha = `${alphaAsNumber * 100}%` |
| 185 | } |
| 186 | |
| 187 | // No need for `color-mix` if the alpha is `100%` |
| 188 | if (alpha === '100%') { |
| 189 | return value |
| 190 | } |
| 191 | |
| 192 | return `color-mix(in oklab, ${value} ${alpha}, transparent)` |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Replace the opacity in a color using relative color syntax. |
no outgoing calls
no test coverage detected