/
ryb.ts
34 lines (29 loc) 路 1.26 KB
/
ryb.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { TInput, Irgba, TFormat, Iryba } from "../types";
import { adjustAlpha, clamp } from "../utils/numeric";
import { isRYBObject } from "../utils/typeGuards";
import { RYBtoRGB } from "../conversions/ryb";
/**
* color(ryb[a] <number | percentage>{3} [ / <alpha-value> ]? )
* @see https://www.w3.org/TR/css-color-4/#rgb-functions
*/
const RYBA_RE =
/color\s*\(\s*ryba?\s*(\d*\.?\d+%?)\s*,?\s*(\d*\.?\d+%?)\s*,?\s*(\d*\.?\d+%?)\s*,?\s*\/?\s*?(\d*\.?\d+%?)?\s*\)/gi;
function createReturnArr({ r, y, b, a }: Iryba): [Irgba, TFormat] {
return [RYBtoRGB({ r: clamp(0, r, 255), y: clamp(0, y, 255), b: clamp(0, b, 255), a: adjustAlpha(a) }), "ryb"];
}
export function rybaParser(color: TInput): [Irgba, TFormat] {
if (color.constructor.name.toLowerCase() === "object" && isRYBObject(color)) {
return createReturnArr(color as Iryba);
} else if (typeof color === "string") {
RYBA_RE.lastIndex = 0;
const matches = RYBA_RE.exec(color);
if (matches) {
const [r, y, b, a] = matches
.filter((val) => val !== undefined)
.slice(1)
.map((elem, i) => (elem.includes("%") ? +elem.slice(0, -1) * (i < 3 ? 2.55 : 0.01) : +elem));
return createReturnArr({ r, y, b, a });
}
}
return [{ r: 0, g: 0, b: 0, a: 1 }, "invalid"];
}