Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Jun 3, 2024
1 parent e1d72dc commit b5d57ab
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions color.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,15 +487,27 @@ export class Color {

if (Array.isArray(colorInput)) {
if (
colorInput.length === 3 &&
colorInput.every((n) => typeof n === "number" && n >= 0 && n <= 255)
) {
rgb = colorInput;
} else {
throw Error(`Invalid RGB color format: '${colorInput}'.`);
}
} else if (typeof colorInput === "string" && colorInput.startsWith("#")) {
rgb = Color.hex2rgba(colorInput).slice(0, 3);
} else if (typeof colorInput === "string") {
if (colorInput.startsWith("#")) {
rgb = Color.hex2rgba(colorInput).slice(0, 3);
} else if (colorInput.startsWith("rgb")) {
const match = colorInput.match(
/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*\d*\.?\d+)?\)/,
);
if (match) {
rgb = match.slice(1, 4).map(Number);
} else {
throw Error(`Invalid RGB/RGBA color format: '${colorInput}'.`);
}
} else {
throw Error(`Invalid color format: '${colorInput}'.`);
}
} else {
throw Error(`Invalid color format: '${colorInput}'.`);
}
Expand Down

0 comments on commit b5d57ab

Please sign in to comment.