/
is-gray.ts
31 lines (27 loc) · 936 Bytes
/
is-gray.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
import { DEFAULT, defmulti } from "@thi.ng/defmulti/defmulti";
import { eqDelta } from "@thi.ng/math/eqdelta";
import type { ReadonlyColor, TypedColor } from "./api";
import { EPS } from "./api/constants";
import { rgb } from "./rgb/rgb";
const isGrayHsv = (x: ReadonlyColor, eps = EPS) => x[1] <= eps;
const isGrayRgb = (x: ReadonlyColor, eps = EPS) =>
eqDelta(x[0], x[1], eps) && eqDelta(x[0], x[2], eps);
const isGrayLab = (x: ReadonlyColor, eps = EPS) =>
eqDelta(x[1], 0, eps) && eqDelta(x[2], 0, eps);
export const isGray = defmulti<TypedColor<any>, number | undefined, boolean>(
(x) => x.mode,
{},
{
hcy: isGrayHsv,
hsi: isGrayHsv,
hsl: isGrayHsv,
hsv: isGrayHsv,
labD50: isGrayLab,
labD65: isGrayLab,
lch: isGrayHsv,
rgb: isGrayRgb,
srgb: isGrayRgb,
ycc: isGrayLab,
[DEFAULT]: (x: any) => isGrayRgb(rgb(x)),
}
);