11import { colorKeywords as color_keywords , cssKeywords as css_keywords } from '@projectwallace/css-analyzer'
22import { type ColorValue , type ColorSpace as tColorSpace } from './types.js'
33import {
4- parse ,
4+ tryColor ,
55 ColorSpace ,
66 XYZ_D65 ,
77 XYZ_D50 ,
@@ -28,6 +28,8 @@ import {
2828} from 'colorjs.io/fn'
2929
3030// Register color spaces for parsing and converting
31+ // TODO: According to the changelog we should be able to import
32+ // and register all spaces in one go but it doesn't seem to work
3133ColorSpace . register ( sRGB ) // Parses keywords and hex colors
3234ColorSpace . register ( XYZ_D65 )
3335ColorSpace . register ( XYZ_D50 )
@@ -72,17 +74,14 @@ export function color_to_token(color: string): ColorValue | null {
7274 return null
7375 }
7476
75- try {
76- let parsed_color = parse ( color )
77- let [ component_a , component_b , component_c ] = parsed_color . coords
77+ let parsed_color = tryColor ( color )
7878
79- return {
80- colorSpace : parsed_color . spaceId as tColorSpace ,
81- components : [ component_a ?? 'none' , component_b ?? 'none' , component_c ?? 'none' ] ,
82- alpha : parsed_color . alpha ?? 1 ,
83- }
84- } catch ( error ) {
85- // A catch for edge cases that we don't support yet.
86- return null
79+ if ( parsed_color === null ) return null
80+ let [ component_a , component_b , component_c ] = parsed_color . coords
81+
82+ return {
83+ colorSpace : parsed_color . space . id as tColorSpace ,
84+ components : [ component_a ?? 'none' , component_b ?? 'none' , component_c ?? 'none' ] ,
85+ alpha : parsed_color . alpha ?? 1 ,
8786 }
8887}
0 commit comments