@@ -81,42 +81,33 @@ abstract class Color implements IColor {
81
81
}
82
82
abstract getColorChannels ( ) : [ ColorChannel , ColorChannel , ColorChannel ]
83
83
}
84
-
85
- const HEX_REGEX = / ^ # (?: ( [ 0 - 9 a - f ] { 3 } ) | ( [ 0 - 9 a - f ] { 6 } ) ) $ / i;
86
-
87
- // X = <negative/positive number with/without decimal places>
88
- // before/after a comma, 0 or more whitespaces are allowed
89
- // - rgb(X, X, X)
90
- // - rgba(X, X, X, X)
91
- const RGB_REGEX = / r g b \( ( [ - + ] ? \d + (?: .\d + ) ? \s * , \s * [ - + ] ? \d + (?: .\d + ) ? \s * , \s * [ - + ] ? \d + (?: .\d + ) ? ) \) | r g b a \( ( [ - + ] ? \d + (?: .\d + ) ? \s * , \s * [ - + ] ? \d + (?: .\d + ) ? \s * , \s * [ - + ] ? \d + (?: .\d + ) ? \s * , \s * [ - + ] ? \d ( .\d + ) ? ) \) / ;
92
-
93
84
class RGBColor extends Color {
94
85
constructor ( private red : number , private green : number , private blue : number , private alpha : number ) {
95
86
super ( ) ;
96
87
}
97
88
98
- static parse ( value : string ) : RGBColor | void {
99
- let m ;
100
- if ( ( m = value . match ( HEX_REGEX ) ) ) {
101
- if ( m [ 1 ] ) {
102
- let r = parseInt ( m [ 1 ] [ 0 ] + m [ 1 ] [ 0 ] , 16 ) ;
103
- let g = parseInt ( m [ 1 ] [ 1 ] + m [ 1 ] [ 1 ] , 16 ) ;
104
- let b = parseInt ( m [ 1 ] [ 2 ] + m [ 1 ] [ 2 ] , 16 ) ;
105
- return new RGBColor ( r , g , b , 1 ) ;
106
- } else if ( m [ 2 ] ) {
107
- let r = parseInt ( m [ 2 ] [ 0 ] + m [ 2 ] [ 1 ] , 16 ) ;
108
- let g = parseInt ( m [ 2 ] [ 2 ] + m [ 2 ] [ 3 ] , 16 ) ;
109
- let b = parseInt ( m [ 2 ] [ 4 ] + m [ 2 ] [ 5 ] , 16 ) ;
110
- return new RGBColor ( r , g , b , 1 ) ;
89
+ static parse ( value : string ) {
90
+ let colors = [ ] ;
91
+ // matching #rgb, #rgba, #rrggbb, #rrggbbaa
92
+ if ( / ^ # [ \d a - f ] + $ / i. test ( value ) && [ 4 , 5 , 7 , 9 ] . includes ( value . length ) ) {
93
+ const values = ( value . length < 6 ? value . replace ( / [ ^ # ] / gi, '$&$&' ) : value ) . slice ( 1 ) . split ( '' ) ;
94
+ while ( values . length > 0 ) {
95
+ colors . push ( parseInt ( values . splice ( 0 , 2 ) . join ( '' ) , 16 ) ) ;
111
96
}
97
+ colors [ 3 ] = colors [ 3 ] !== undefined ? colors [ 3 ] / 255 : undefined ;
112
98
}
113
99
114
- if ( ( m = value . match ( RGB_REGEX ) ) ) {
115
- const [ r , g , b , a ] = ( m [ 1 ] ?? m [ 2 ] ) . split ( ',' ) . map ( n => Number ( n . trim ( ) ) ) ;
116
- return new RGBColor ( clamp ( r , 0 , 255 ) , clamp ( g , 0 , 255 ) , clamp ( b , 0 , 255 ) , clamp ( a ?? 1 , 0 , 1 ) ) ;
100
+ // matching rgb(rrr, ggg, bbb), rgba(rrr, ggg, bbb, 0.a)
101
+ const match = value . match ( / ^ r g b a ? \( ( .* ) \) $ / ) ;
102
+ if ( match ?. [ 1 ] ) {
103
+ colors = match [ 1 ] . split ( ',' ) . map ( value => Number ( value . trim ( ) ) ) ;
104
+ colors = colors . map ( ( num , i ) => clamp ( num , 0 , i < 3 ? 255 : 1 ) ) ;
117
105
}
106
+
107
+ return colors . length < 3 ? undefined : new RGBColor ( colors [ 0 ] , colors [ 1 ] , colors [ 2 ] , colors [ 3 ] ?? 1 ) ;
118
108
}
119
109
110
+
120
111
toString ( format : ColorFormat | 'css' ) {
121
112
switch ( format ) {
122
113
case 'hex' :
0 commit comments