@@ -154,26 +154,32 @@ export function rgbToHsv ({ r, g, b, a }) {
154154 }
155155}
156156
157- const reRGBA = / ^ \s * r g b ( a ) ? \s * \ (( \s * ( \d + ) \s * , \s * ? ) { 2 } ( \d + ) \s * , ? \s * ( [ 0 1 ] ? \. ? \d * ?) ? \s * \) \s * $ /
157+ const reRGBA = / ^ r g b ( a ) ? \( ( \d { 1 , 3 } ) , ( \d { 1 , 3 } ) , ( \d { 1 , 3 } ) , ? ( [ 0 1 ] ? \. ? \d * ?) ? \) $ /
158158
159- export function textToRgb ( color ) {
160- if ( typeof color !== 'string' ) {
159+ export function textToRgb ( str ) {
160+ if ( typeof str !== 'string' ) {
161161 throw new TypeError ( 'Expected a string' )
162162 }
163163
164+ const color = str . replace ( / / g, '' )
165+
164166 const m = reRGBA . exec ( color )
165- if ( m ) {
166- const rgb = {
167- r : Math . min ( 255 , parseInt ( m [ 2 ] , 10 ) ) ,
168- g : Math . min ( 255 , parseInt ( m [ 3 ] , 10 ) ) ,
169- b : Math . min ( 255 , parseInt ( m [ 4 ] , 10 ) )
170- }
171- if ( m [ 1 ] ) {
172- rgb . a = Math . min ( 1 , parseFloat ( m [ 5 ] ) )
173- }
174- return rgb
167+
168+ if ( m === null ) {
169+ return hexToRgb ( color )
175170 }
176- return hexToRgb ( color )
171+
172+ const rgb = {
173+ r : Math . min ( 255 , parseInt ( m [ 2 ] , 10 ) ) ,
174+ g : Math . min ( 255 , parseInt ( m [ 3 ] , 10 ) ) ,
175+ b : Math . min ( 255 , parseInt ( m [ 4 ] , 10 ) )
176+ }
177+
178+ if ( m [ 1 ] ) {
179+ rgb . a = Math . min ( 1 , parseFloat ( m [ 5 ] ) || 0 ) * 100
180+ }
181+
182+ return rgb
177183}
178184
179185/* works as darken if percent < 0 */
0 commit comments