@@ -4,59 +4,3 @@ import { twMerge } from 'tailwind-merge';
44export function cn ( ...inputs : ClassValue [ ] ) {
55 return twMerge ( clsx ( inputs ) ) ;
66}
7-
8- /**
9- * Converts OKLCH color format to RGB hex code
10- * @param oklchString - OKLCH color string (e.g., "oklch(38% 0.189 293.745)")
11- * @returns RGB hex code (e.g., "#1a1a1a")
12- */
13- export function oklchToRgb ( oklchString : string ) : string {
14- // Parse OKLCH values from string like "oklch(38% 0.189 293.745)"
15- const match = oklchString . match ( / o k l c h \( ( [ ^ ) ] + ) \) / ) ;
16- if ( ! match ) {
17- throw new Error ( `Invalid OKLCH format: ${ oklchString } ` ) ;
18- }
19-
20- const parts = match [ 1 ] . trim ( ) . split ( / \s + / ) ;
21- const l = parseFloat ( parts [ 0 ] . replace ( '%' , '' ) ) / 100 ; // Convert percentage to 0-1
22- const c = parseFloat ( parts [ 1 ] ) ;
23- const h = parseFloat ( parts [ 2 ] ) * ( Math . PI / 180 ) ; // Convert degrees to radians
24-
25- // Convert OKLCH to OKLab
26- const a = c * Math . cos ( h ) ;
27- const bLab = c * Math . sin ( h ) ;
28-
29- // Convert OKLab to linear RGB
30- // OKLab to linear sRGB matrix (inverse of sRGB to OKLab)
31- const l_ = l + 0.3963377774 * a + 0.2158037573 * bLab ;
32- const m_ = l - 0.1055613458 * a - 0.0638541728 * bLab ;
33- const s_ = l - 0.0894841775 * a - 1.291485548 * bLab ;
34-
35- const l3 = l_ * l_ * l_ ;
36- const m3 = m_ * m_ * m_ ;
37- const s3 = s_ * s_ * s_ ;
38-
39- const r_ = 4.0767416621 * l3 - 3.3077115913 * m3 + 0.2309699292 * s3 ;
40- const g_ = - 1.2684380046 * l3 + 2.6097574011 * m3 - 0.3413193965 * s3 ;
41- const b_ = - 0.0041960863 * l3 - 0.7034186147 * m3 + 1.707614701 * s3 ;
42-
43- // Convert linear RGB to sRGB (gamma correction)
44- const gamma = ( c : number ) => {
45- if ( c <= 0.0031308 ) {
46- return 12.92 * c ;
47- }
48- return 1.055 * Math . pow ( c , 1 / 2.4 ) - 0.055 ;
49- } ;
50-
51- const r = Math . max ( 0 , Math . min ( 1 , gamma ( r_ ) ) ) ;
52- const g = Math . max ( 0 , Math . min ( 1 , gamma ( g_ ) ) ) ;
53- const b = Math . max ( 0 , Math . min ( 1 , gamma ( b_ ) ) ) ;
54-
55- // Convert to 0-255 range and then to hex
56- const toHex = ( n : number ) => {
57- const hex = Math . round ( n * 255 ) . toString ( 16 ) ;
58- return hex . length === 1 ? '0' + hex : hex ;
59- } ;
60-
61- return `#${ toHex ( r ) } ${ toHex ( g ) } ${ toHex ( b ) } ` ;
62- }
0 commit comments