Skip to content

Commit b1c6a36

Browse files
committed
fix(colors/textToRgb): text is wrongly parsed quasarframework#6388
1 parent 16ae7dd commit b1c6a36

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

ui/src/utils/colors.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,32 @@ export function rgbToHsv ({ r, g, b, a }) {
154154
}
155155
}
156156

157-
const reRGBA = /^\s*rgb(a)?\s*\((\s*(\d+)\s*,\s*?){2}(\d+)\s*,?\s*([01]?\.?\d*?)?\s*\)\s*$/
157+
const reRGBA = /^rgb(a)?\((\d{1,3}),(\d{1,3}),(\d{1,3}),?([01]?\.?\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

Comments
 (0)