-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
hsv.ts
69 lines (58 loc) 路 1.58 KB
/
hsv.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { InputObject, RgbaColor, HsvaColor } from "../types";
import { ALPHA_PRECISION } from "../constants";
import { clamp, clampHue, isPresent, round } from "../helpers";
export const clampHsva = (hsva: HsvaColor): HsvaColor => ({
h: clampHue(hsva.h),
s: clamp(hsva.s, 0, 100),
v: clamp(hsva.v, 0, 100),
a: clamp(hsva.a),
});
export const roundHsva = (hsva: HsvaColor): HsvaColor => ({
h: round(hsva.h),
s: round(hsva.s),
v: round(hsva.v),
a: round(hsva.a, ALPHA_PRECISION),
});
export const parseHsva = ({ h, s, v, a = 1 }: InputObject): RgbaColor | null => {
if (!isPresent(h) || !isPresent(s) || !isPresent(v)) return null;
const hsva = clampHsva({
h: Number(h),
s: Number(s),
v: Number(v),
a: Number(a),
});
return hsvaToRgba(hsva);
};
export const rgbaToHsva = ({ r, g, b, a }: RgbaColor): HsvaColor => {
const max = Math.max(r, g, b);
const delta = max - Math.min(r, g, b);
const hh = delta
? max === r
? (g - b) / delta
: max === g
? 2 + (b - r) / delta
: 4 + (r - g) / delta
: 0;
return {
h: 60 * (hh < 0 ? hh + 6 : hh),
s: max ? (delta / max) * 100 : 0,
v: (max / 255) * 100,
a,
};
};
export const hsvaToRgba = ({ h, s, v, a }: HsvaColor): RgbaColor => {
h = (h / 360) * 6;
s = s / 100;
v = v / 100;
const hh = Math.floor(h),
b = v * (1 - s),
c = v * (1 - (h - hh) * s),
d = v * (1 - (1 - h + hh) * s),
module = hh % 6;
return {
r: [v, c, b, b, d, v][module] * 255,
g: [d, v, v, c, b, b][module] * 255,
b: [b, b, d, v, v, c][module] * 255,
a: a,
};
};