Skip to content

Commit dba50fd

Browse files
committed
rgb_match when regex not matched
1 parent 1df239b commit dba50fd

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib_src/misc/colors.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ function rgb2hex(rgb: string): string {
3535
if (rgb.search("rgb") === -1) {
3636
return rgb
3737
} else {
38-
rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/)
39-
return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
38+
const rgb_match = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/)
39+
40+
if (rgb_match) {
41+
return hex(rgb_match[1]) + hex(rgb_match[2]) + hex(rgb_match[3])
42+
} else {
43+
// TODO should we check for this error?
44+
console.error(rgb.concat(" isn't a rgb string!"))
45+
return "#000000" // black
46+
}
4047
}
4148
}
4249

0 commit comments

Comments
 (0)