-
Notifications
You must be signed in to change notification settings - Fork 1
/
NiceColor.js
54 lines (43 loc) · 962 Bytes
/
NiceColor.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
class NiceColor {
constructor() {
this.hex = [
null, // R
null, // G
null // B
],
this.selected = this.randomDec(3)
}
// 8-bit
randomHex() {
return [...Array(2)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
}
randomDec(ceil) {
return Math.floor(Math.random() * ceil) + 0;
}
// Generate a 32-bit hexadecimal RGB string
generate() {
let binary = this.randomDec(2);
this.hex[this.selected] = this.randomHex();
this.hex.forEach((value,index) => {
// Continue if index is our special value
if(this.hex[index] !== null) {
return;
}
// Assign index and ignore if(binary) next run
if(binary) {
this.hex[index] = "16";
binary = 0;
return;
}
// Assign index and enter if(binary) next run
binary = 1;
this.hex[index] = "ff";
})
}
// Generate and return HEX-color (#RRGGBB)
get() {
this.generate();
return "#" + this.hex.join("");
}
}
// Victor Westerlund