From 5224ed29c205a9c80bd30bbaee53f6fa4dd246ee Mon Sep 17 00:00:00 2001 From: Martin Schuhfuss Date: Thu, 5 Jan 2023 16:26:07 +0100 Subject: [PATCH] fix: better alpha-handling in parseRgbColor --- src/lib/color.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lib/color.ts b/src/lib/color.ts index 0742039..7bc5595 100644 --- a/src/lib/color.ts +++ b/src/lib/color.ts @@ -1,3 +1,5 @@ +import {warnOnce} from './util'; + export type RGBAColor = [number, number, number, number]; export type RGBColor = [number, number, number]; export type LABColor = [number, number, number]; @@ -73,14 +75,14 @@ function parseHexColor(color: string): RGBAColor { function parseRgbColor(color: string): RGBAColor { try { - const [, r, g, b, a] = color.match(rxRgbColor)!; - const c = [r, g, b, a]; + const [, r, g, b, a = '1'] = color.match(rxRgbColor)!; + const rgba = [r, g, b, a]; - return c - .filter(s => s !== undefined) // alpha might be undefined - .map(s => (s.endsWith('%') ? Number(s) * 2.55 : Number(s))) as RGBAColor; + return rgba.map(s => + s.endsWith('%') ? Number(s.slice(0, -1)) * 2.55 : Number(s) + ) as RGBAColor; } catch (err) { - console.error(`rgb-color parsing failed (parsing value: '${color}')`); + warnOnce(`rgb-color parsing failed (parsing value: '${color}')`); return [0, 0, 0, 1]; }