File tree Expand file tree Collapse file tree 1 file changed +12
-8
lines changed
Expand file tree Collapse file tree 1 file changed +12
-8
lines changed Original file line number Diff line number Diff line change 66 * opacity utilities — colors must be fully resolved values.
77 */
88export function withAlpha ( color : string , alpha : number ) : string {
9- if ( color . startsWith ( 'oklch(' ) ) return color . replace ( ')' , ` / ${ alpha } )` )
10- if ( color . startsWith ( '#' ) )
11- return (
12- color +
13- Math . round ( alpha * 255 )
14- . toString ( 16 )
15- . padStart ( 2 , '0' )
16- )
9+ const clamped = Math . min ( Math . max ( alpha , 0 ) , 1 )
10+ if ( color . startsWith ( 'oklch(' ) ) {
11+ const withoutAlpha = color . replace ( / \s * \/ [ ^ ) ] * (? = \) ) / , '' )
12+ return withoutAlpha . replace ( ')' , ` / ${ clamped } )` )
13+ }
14+ if ( / ^ # ( [ a - f \d ] { 6 } | [ a - f \d ] { 8 } ) $ / i. test ( color ) ) {
15+ const base = color . slice ( 0 , 7 )
16+ const a = Math . round ( clamped * 255 )
17+ . toString ( 16 )
18+ . padStart ( 2 , '0' )
19+ return `${ base } ${ a } `
20+ }
1721 return color
1822}
1923
You can’t perform that action at this time.
0 commit comments