MCPcopy
hub / github.com/tailwindlabs/tailwindcss / withAlpha

Function withAlpha

packages/tailwindcss/src/utilities.ts:176–193  ·  view source on GitHub ↗
(value: string, alpha: string)

Source from the content-addressed store, hash-verified

174 * Apply opacity to a color using `color-mix`.
175 */
176export 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.

Callers 8

alphaFunction · 0.90
resolveThemeValueFunction · 0.90
compileFnFunction · 0.90
createThemeFnFunction · 0.90
asColorFunction · 0.85
createUtilitiesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected