-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbytes.js
53 lines (49 loc) · 1.29 KB
/
bytes.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
/**
* @typedef {number[]} bytes An array of 3 (RGB) or 4 (A) values in bytes.
*
* All components in the range 0 <= x <= 255
*/
/**
* Updates a color based on byte values.
* @alias module:pex-color.fromBytes
* @param {import("./color.js").color} color
* @param {bytes} bytes
* @returns {import("./color.js").color}
*/
export function fromBytes(color, [r, g, b, a]) {
color[0] = r / 255;
color[1] = g / 255;
color[2] = b / 255;
if (a !== undefined) color[3] = a / 255;
return color;
}
/**
* Get RGB[A] color components as bytes array.
* @alias module:pex-color.toBytes
* @param {import("./color.js").color} color
* @param {Array} out
* @returns {bytes}
*/
export function toBytes(color, out = []) {
out[0] = Math.round(color[0] * 255);
out[1] = Math.round(color[1] * 255);
out[2] = Math.round(color[2] * 255);
if (color[3] !== undefined) out[3] = Math.round(color[3] * 255);
return out;
}
/**
* @deprecated Use "fromBytes()".
* @ignore
*/
export function fromRGBBytes(color, bytes) {
console.error(`"fromRGBBytes()" deprecated. Use "fromBytes()".`);
return fromBytes(color, bytes);
}
/**
* @deprecated Use "toBytes()".
* @ignore
*/
export function toRGBBytes(color, out) {
console.error(`"toRGBBytes()" deprecated. Use "toBytes()".`);
return toBytes(color, out);
}