@@ -13,7 +13,7 @@ const HEX_COLOR_REGEX = /^([0-9A-Fa-f]{3}|[0-9A-Fa-f]{4}|[0-9A-Fa-f]{6}|[0-9A-Fa
1313 */
1414export function isValidHex ( color ?: string ) : boolean {
1515 if ( ! color ) return false ;
16- const cleanColor = color . replace ( '#' , '' ) ;
16+ const cleanColor = color . replace ( / ^ # + / , '' ) ;
1717 return HEX_COLOR_REGEX . test ( cleanColor ) ;
1818}
1919
@@ -26,27 +26,27 @@ export function isValidHex(color?: string): boolean {
2626 * For user-supplied input, use `sanitizeHexColor` instead.
2727 */
2828export function hexColor ( value : string , fallback = '000000' ) : HexColor {
29- const cleaned = value . replace ( '#' , '' ) ;
29+ const cleaned = value . replace ( / ^ # + / , '' ) ;
3030 if ( HEX_COLOR_REGEX . test ( cleaned ) ) {
3131 return cleaned as HexColor ;
3232 }
33- return fallback . replace ( '#' , '' ) as HexColor ;
33+ return fallback . replace ( / ^ # + / , '' ) as HexColor ;
3434}
3535
3636/**
3737 * Sanitizes a color input, ensuring it's a valid hex or falls back to a safe value.
3838 * Always returns a hex string WITHOUT the leading #.
3939 */
4040export function sanitizeHexColor ( input : string | undefined | null , fallback : string ) : HexColor {
41- if ( ! input ) return fallback . replace ( '#' , '' ) as HexColor ;
41+ if ( ! input ) return fallback . replace ( / ^ # + / , '' ) as HexColor ;
4242
43- const cleanInput = input . trim ( ) . replace ( '#' , '' ) ;
43+ const cleanInput = input . trim ( ) . replace ( / ^ # + / , '' ) ;
4444
4545 if ( HEX_COLOR_REGEX . test ( cleanInput ) ) {
4646 return cleanInput as HexColor ;
4747 }
4848
49- return fallback . replace ( '#' , '' ) as HexColor ;
49+ return fallback . replace ( / ^ # + / , '' ) as HexColor ;
5050}
5151
5252/**
0 commit comments