-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseNotation.js
More file actions
54 lines (42 loc) · 1.64 KB
/
parseNotation.js
File metadata and controls
54 lines (42 loc) · 1.64 KB
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
function parseHex(string) {
string = string.replace(/^#/, '').trim();
const hex = string.split('').map(l => l + l).join('');
if(hex.length !== 6 && hex.length !== 8) {
return null;
}
return hex;
}
function parseRGBA(string) {
const regex = /^\s*(?:rgba?\s*)?\(?\s*(\d+)\s*(?:\s+|,)\s*(\d+)\s*(?:\s+|,)\s*(\d+)\s*(?:\s*(?:\s+|,)\s*(\d*\.?\d+))?\)?\s*$/i;
const match = string.match(regex);
if (!match) return null;
return {
r: clamp(parseInt(match[1], 10), 0, 255),
g: clamp(parseInt(match[2], 10), 0, 255),
b: clamp(parseInt(match[3], 10), 0, 255),
a: clamp(parseFloat(match[4] ?? 1), 0, 1)
};
}
function parseHSLA(string) {
const regex = /^\s*(?:hsl[a]?\s*)?\(?\s*(\d+)\s*(?:,|\s)\s*(\d+)\s*(%)?\s*(?:,|\s)\s*(\d+)\s*(%)?\s*(?:,|\s)*(\d*\.?\d+)?\s*\)?\s*$/i;
const match = string.match(regex);
console.log(match)
if (!match) return null;
return {
h: Math.abs(parseInt(match[1], 10)) % 360,
s: clamp(parseInt(match[2], 10), 0, 100),
l: clamp(parseInt(match[4], 10), 0, 100),
a: clamp(parseFloat(match[6] ?? 1), 0, 1)
}
}
function parseHSVA(string) {
const regex = /^\s*(?:hsv[a]?\s*)?\(?\s*(\d+)\s*(?:,|\s)\s*(\d+)\s*(%)?\s*(?:,|\s)\s*(\d+)\s*(%)?\s*(?:,|\s)*(\d*\.?\d+)?\s*\)?\s*$/i;
const match = string.match(regex);
if (!match) return null;
return {
h: Math.abs(parseInt(match[1], 10)) % 360,
s: clamp(parseInt(match[2], 10), 0, 100),
v: clamp(parseInt(match[4], 10), 0, 100),
a: clamp(parseFloat(match[6] ?? 1), 0, 1)
}
}