This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unitransform.js
116 lines (99 loc) · 2.52 KB
/
unitransform.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var rgba = require("color-rgba");
var hsluv = require("hsluv");
function clamp100(num) {
return Math.min(Math.max(num, 0), 100);
}
function clampHue(hue) {
return (hue < 0 ? hue + 360 : hue) % 360;
}
function getHsluv(color) {
return hsluv.rgbToHsluv(
rgba(color)
.slice(0, 3)
.map(function(x) {
return x / 255;
})
);
}
function useProperties(modified, modifier, properties) {
properties = properties.toLowerCase();
if (!/^[hsl]{1,3}$/.test(properties)) {
throw new Error(
"Properties must be 3 chars and only can include h, s, or l"
);
}
var modifiedHsluv = getHsluv(modified);
var modifierHsluv = getHsluv(modifier);
for (var i = 0; i < properties.length; i++) {
var index = "hsl".indexOf(properties.charAt(i));
modifiedHsluv[index] = modifierHsluv[index];
}
return formatColor(modifiedHsluv);
}
function formatColor(color) {
return hsluv.hsluvToHex(color);
}
function lighten(color, adjustment) {
color = getHsluv(color);
color[2] = clamp100(color[2] + adjustment);
return formatColor(color);
}
function darken(color, adjustment) {
color = getHsluv(color);
color[2] = clamp100(color[2] - adjustment);
return formatColor(color);
}
function saturate(color, adjustment) {
color = getHsluv(color);
color[1] = clamp100(color[1] + adjustment);
return formatColor(color);
}
function desaturate(color, adjustment) {
color = getHsluv(color);
color[1] = clamp100(color[1] - adjustment);
return formatColor(color);
}
function rotate(color, adjustment) {
color = getHsluv(color);
color[0] = clampHue(color[0] + adjustment);
return formatColor(color);
}
function setHue(color, hue) {
color = getHsluv(color);
color[0] = clampHue(hue);
return formatColor(color);
}
function getHue(color) {
return getHsluv(color)[0];
}
function setSaturation(color, saturation) {
color = getHsluv(color);
color[1] = clamp100(saturation);
return formatColor(color);
}
function getSaturation(color) {
return getHsluv(color)[1];
}
function setLightness(color, lightness) {
color = getHsluv(color);
color[2] = clamp100(lightness);
return formatColor(color);
}
function getLightness(color) {
return getHsluv(color)[2];
}
module.exports = {
darken: darken,
desaturate: desaturate,
getHsluv: getHsluv,
getHue: getHue,
getLightness: getLightness,
getSaturation: getSaturation,
lighten: lighten,
rotate: rotate,
saturate: saturate,
setHue: setHue,
setLightness: setLightness,
setSaturation: setSaturation,
useProperties: useProperties
};