@@ -44,63 +44,67 @@ export function parseCssColor(input: string): RGBColor | undefined {
4444 }
4545
4646 // #f0c, #f0cf, #ff00cc, #ff00ccff
47- const hexRegexp = / ^ # (?: [ 0 - 9 a - f ] { 3 , 4 } | [ 0 - 9 a - f ] { 6 } | [ 0 - 9 a - f ] { 8 } ) $ / ;
48- if ( hexRegexp . test ( input ) ) {
49- const step = input . length < 6 ? 1 : 2 ;
50- let i = 1 ;
51- return [ // eslint-disable-line no-return-assign
52- parseHex ( input . slice ( i , i += step ) ) ,
53- parseHex ( input . slice ( i , i += step ) ) ,
54- parseHex ( input . slice ( i , i += step ) ) ,
55- parseHex ( input . slice ( i , i + step ) || 'ff' ) ,
56- ] ;
47+ if ( input . startsWith ( '#' ) ) {
48+ const hexRegexp = / ^ # (?: [ 0 - 9 a - f ] { 3 , 4 } | [ 0 - 9 a - f ] { 6 } | [ 0 - 9 a - f ] { 8 } ) $ / ;
49+ if ( hexRegexp . test ( input ) ) {
50+ const step = input . length < 6 ? 1 : 2 ;
51+ let i = 1 ;
52+ return [ // eslint-disable-line no-return-assign
53+ parseHex ( input . slice ( i , i += step ) ) ,
54+ parseHex ( input . slice ( i , i += step ) ) ,
55+ parseHex ( input . slice ( i , i += step ) ) ,
56+ parseHex ( input . slice ( i , i + step ) || 'ff' ) ,
57+ ] ;
58+ }
5759 }
5860
5961 // rgb(128 0 0), rgb(50% 0% 0%), rgba(255,0,255,0.6), rgb(255 0 255 / 60%), rgb(100% 0% 100% /.6)
60- const rgbRegExp = / ^ r g b a ? \( \s * ( [ \d e . + - ] + ) ( % ) ? (?: \s + | \s * ( , ) \s * ) ( [ \d e . + - ] + ) ( % ) ? (?: \s + | \s * ( , ) \s * ) ( [ \d e . + - ] + ) ( % ) ? (?: \s * ( [ , \/ ] ) \s * ( [ \d e . + - ] + ) ( % ) ? ) ? \s * \) $ / ;
61- const rgbMatch = input . match ( rgbRegExp ) ;
62- if ( rgbMatch ) {
63- const [
64- _ , // eslint-disable-line @typescript-eslint/no-unused-vars
65- r , // <numeric>
66- rp , // % (optional)
67- f1 , // , (optional)
68- g , // <numeric>
69- gp , // % (optional)
70- f2 , // , (optional)
71- b , // <numeric>
72- bp , // % (optional)
73- f3 , // ,|/ (optional)
74- a , // <numeric> (optional)
75- ap , // % (optional)
76- ] = rgbMatch ;
62+ if ( input . startsWith ( 'rgb' ) ) {
63+ const rgbRegExp = / ^ r g b a ? \( \s * ( [ \d e . + - ] + ) ( % ) ? (?: \s + | \s * ( , ) \s * ) ( [ \d e . + - ] + ) ( % ) ? (?: \s + | \s * ( , ) \s * ) ( [ \d e . + - ] + ) ( % ) ? (?: \s * ( [ , \/ ] ) \s * ( [ \d e . + - ] + ) ( % ) ? ) ? \s * \) $ / ;
64+ const rgbMatch = input . match ( rgbRegExp ) ;
65+ if ( rgbMatch ) {
66+ const [
67+ _ , // eslint-disable-line @typescript-eslint/no-unused-vars
68+ r , // <numeric>
69+ rp , // % (optional)
70+ f1 , // , (optional)
71+ g , // <numeric>
72+ gp , // % (optional)
73+ f2 , // , (optional)
74+ b , // <numeric>
75+ bp , // % (optional)
76+ f3 , // ,|/ (optional)
77+ a , // <numeric> (optional)
78+ ap , // % (optional)
79+ ] = rgbMatch ;
7780
78- const argFormat = [ f1 || ' ' , f2 || ' ' , f3 ] . join ( '' ) ;
79- if (
80- argFormat === ' ' ||
81- argFormat === ' /' ||
82- argFormat === ',,' ||
83- argFormat === ',,,'
84- ) {
85- const valFormat = [ rp , gp , bp ] . join ( '' ) ;
86- const maxValue =
87- ( valFormat === '%%%' ) ? 100 :
88- ( valFormat === '' ) ? 255 : 0 ;
89- if ( maxValue ) {
90- const rgba : RGBColor = [
91- clamp ( + r / maxValue , 0 , 1 ) ,
92- clamp ( + g / maxValue , 0 , 1 ) ,
93- clamp ( + b / maxValue , 0 , 1 ) ,
94- a ? parseAlpha ( + a , ap ) : 1 ,
95- ] ;
96- if ( validateNumbers ( rgba ) ) {
97- return rgba ;
81+ const argFormat = [ f1 || ' ' , f2 || ' ' , f3 ] . join ( '' ) ;
82+ if (
83+ argFormat === ' ' ||
84+ argFormat === ' /' ||
85+ argFormat === ',,' ||
86+ argFormat === ',,,'
87+ ) {
88+ const valFormat = [ rp , gp , bp ] . join ( '' ) ;
89+ const maxValue =
90+ ( valFormat === '%%%' ) ? 100 :
91+ ( valFormat === '' ) ? 255 : 0 ;
92+ if ( maxValue ) {
93+ const rgba : RGBColor = [
94+ clamp ( + r / maxValue , 0 , 1 ) ,
95+ clamp ( + g / maxValue , 0 , 1 ) ,
96+ clamp ( + b / maxValue , 0 , 1 ) ,
97+ a ? parseAlpha ( + a , ap ) : 1 ,
98+ ] ;
99+ if ( validateNumbers ( rgba ) ) {
100+ return rgba ;
101+ }
102+ // invalid numbers
98103 }
99- // invalid numbers
104+ // values must be all numbers or all percentages
100105 }
101- // values must be all numbers or all percentages
106+ return ; // comma optional syntax requires no commas at all
102107 }
103- return ; // comma optional syntax requires no commas at all
104108 }
105109
106110 // hsl(120 50% 80%), hsla(120deg,50%,80%,.9), hsl(12e1 50% 80% / 90%)
0 commit comments