-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgradient.js
More file actions
78 lines (58 loc) · 1.79 KB
/
Copy pathgradient.js
File metadata and controls
78 lines (58 loc) · 1.79 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
export class Gradient {
#colors;
#iteration = 1;
constructor(from, to, steps) {
this.#colors = this.#generateColors(from, to, steps);
}
next() {
if (Math.floor((this.#iteration - 1) / 2) % 2 && this.#iteration % this.#colors.length === 0) {
this.#iteration = 1;
}
const index = Math.floor(this.#iteration / this.#colors.length) % 2 === 0
? this.#iteration % this.#colors.length
: this.#colors.length - (this.#iteration % this.#colors.length) - 1
this.#iteration++;
return this.#colors[index - 1];
}
get current() {
return this.#colors[this.#iteration % this.#colors.length]
}
#generateColors(from, to, steps) {
const start = this.#convertToRGB(from);
const end = this.#convertToRGB(to);
const saida = [];
for (let i = 0; i < steps; i++) {
const alpha = (1 / steps) * i;
const hex = this.#convertToHex([
start[0] * alpha + (1 - alpha) * end[0],
start[1] * alpha + (1 - alpha) * end[1],
start[2] * alpha + (1 - alpha) * end[2]
])
saida.push(hex);
}
console.log(saida);
return saida;
}
#convertToRGB(hex) {
return [
parseInt((this.#trim(hex)).substring(0, 2), 16),
parseInt((this.#trim(hex)).substring(2, 4), 16),
parseInt((this.#trim(hex)).substring(4, 6), 16),
]
}
#convertToHex([red, green, blue]) {
return '#' + this.#hex(red) + this.#hex(green) + this.#hex(blue);
}
#hex(rgb) {
const charset = "0123456789abcdef";
let color = parseInt(rgb);
if (color === 0) return "00";
color = Math.round(Math.min(Math.max(0, color), 255));
return charset.charAt((color - color % 16) / 16) + charset.charAt(color % 16);
}
#trim(color) {
return color.charAt(0) == '#'
? color.substring(1, 7)
: color
}
}