-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathutils.js
More file actions
31 lines (28 loc) · 811 Bytes
/
utils.js
File metadata and controls
31 lines (28 loc) · 811 Bytes
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
export function multiplyAlpha(pixel) {
return pixel.map((channel, i) => {
// Pass the alpha channel through unchanged
if (i === 3) return channel;
// Otherwise, multiply by alpha
return channel * pixel[3];
});
}
export function unmultiplyAlpha(pixel) {
return pixel.map((channel, i) => {
// Pass the alpha channel through unchanged
if (i === 3) return channel;
// Avoid divide-by-zero
if (pixel[3] === 0) return channel;
// Divide by alpha
return channel / pixel[3];
});
}
export function clamp01(value) {
if (value < 0) return 0;
if (value > 1) return 1;
return value;
}
const toPercent = (num) => `${num * 100}%`;
export const toCSSColor = (pixel) =>
`rgb(${toPercent(pixel[0])} ${toPercent(pixel[1])} ${toPercent(pixel[2])} / ${
pixel[3]
})`;