Skip to content

Commit

Permalink
fix: better alpha-handling in parseRgbColor
Browse files Browse the repository at this point in the history
  • Loading branch information
usefulthink committed Jan 5, 2023
1 parent a9af5fe commit 5224ed2
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/lib/color.ts
Original file line number Diff line number Diff line change
@@ -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];
Expand Down Expand Up @@ -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];
}
Expand Down

0 comments on commit 5224ed2

Please sign in to comment.