-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.ts
79 lines (72 loc) · 2.32 KB
/
utils.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
70
71
72
73
74
75
76
77
78
79
import { TinyColor, ColorInput } from "@ctrl/tinycolor";
/**
* Converts a hexadecimal string to a char-code composed string
* @param str the string to convert from a hex string representation to a charcode representation
* @returns the char-code string
*/
export function unhexlify(str: string) {
var result = '';
if (str.length % 2 !== 0) {
throw new Error('The string length is not a multiple of 2');
}
for (var i = 0, l = str.length; i < l; i += 2) {
const toHex = parseInt(str.substr(i, 2), 16);
if (isNaN(toHex)) {
throw new Error('str contains non hex character')
}
result += String.fromCharCode(toHex);
}
return result;
};
/**
* Converts a number to it's hexadecimal string representation in least significant byte first
* @param value the value to convert between 0 and 65535
* @returns the LSB First string reprensenting `value`
*/
export function int2hexlittle(value: number): string {
if (value > 65535 || value < 0) {
throw new TypeError('int2hexlittle only supports value between 0 and 65535');
};
const byte1 = (value & 0xFF).toString(16).padStart(2, "0");
const byte2 = ((value >> 8) & 0xFF).toString(16).padStart(2, "0");
return `${byte1}${byte2}`;
}
/**
* Converts an 8bit number to it's hexadecimal string representation
* @param int the number to convert
* @returns the hexadecimal string reprensetation
*/
export function number2HexString(int: number): string {
if (int > 255 || int < 0) {
throw new Error('number2HexString works only with number between 0 and 255')
}
return Math.round(int).toString(16).padStart(2, "0");
}
/**
* Convert a boolean to `00` or `01`
* @param bool a boolean
* @returns a string representing the boolean
*/
export function boolean2HexString(bool: boolean): string {
return bool ? "01" : "00";
}
/**
* Converts a color to an hexadecimal string representation
* @param color the color to convert
*/
export function color2HexString(color: ColorInput): string {
return new TinyColor(color).toHex();
}
/**
* Converts the brightness to a hex string
* @param brightness the brightness
* @returns the hex string
*/
export function brightness2HexString(brightness: number): string {
if (brightness > 100) {
brightness = 100;
} else if (brightness < 0) {
brightness = 0;
}
return number2HexString(Math.trunc(brightness));
}