-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (59 loc) · 1.48 KB
/
index.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
// Mock JSON data if this is running on a local file system
const colorJSON = `[
{"name": "red", "hsl": "hsl(0, 100%, 50%)"},
{"name": "blue", "hsl": "hsl(240, 100%, 50%)"},
{"name": "purple", "hsl": "hsl(270, 100%, 50%)"}
]`;
// Helper functions
const consonants = [
"b",
"c",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
"m",
"n",
"p",
"r",
"s",
"t",
"v",
"w",
"x",
"y",
"z"
];
const vowels = ["a", "e", "i", "o", "u"];
const getRandomNumber = cap => Math.floor(Math.random() * cap);
const getRandomItem = arr => arr[getRandomNumber(arr.length)];
const getRandomConsonant = () => getRandomItem(consonants);
const getRandomVowel = () => getRandomItem(vowels);
const getRandomBool = () => Math.random() >= 0.5;
const getRandomPair = () => getRandomVowel() + getRandomConsonant();
function generateRandomColorName() {
let name = Math.random() >= 0.3 ? getRandomConsonant() : getRandomPair();
name += getRandomPair();
if (getRandomBool()) {
name += getRandomPair();
}
if (getRandomBool()) {
name += getRandomPair();
}
return name;
}
function generateRandomColor() {
return {
name: generateRandomColorName(),
hsl: `hsl(${getRandomNumber(359)}, 100%, 50%)`
};
}
function changeSaturation(hsl) {
const regexp = /hsl\(\s*(\d+)\s*,\s*(\d+%)\s*,\s*(\d+%)\)/g;
const hslArray = regexp.exec(hsl).slice(1);
const randomSaturation = Math.floor(Math.random() * 75 + 25);
return `hsl(${hslArray[0]}, ${randomSaturation}%, ${hslArray[2]})`;
}