-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (63 loc) · 2.01 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
export function createGradient(startColor, endColor, steps = 10) {
if (isColor(startColor) && isColor(endColor)) {
// parse RGB values to each {Red, Green, Blue} keys
let startRGB = hexToRgb(startColor)
let endRGB = hexToRgb(endColor)
let gradients = []
// generate color for each steps provided then push the value to gradients array
for (let step = 1; step <= steps; step++) {
let colors = {}
for (let color of ['red', 'green', 'blue']) {
colors[color] = colorMaker(
startRGB[color],
endRGB[color],
steps,
step
)
}
let rgb = `rgb(${colors['red']}, ${colors['green']}, ${colors['blue']})`;
gradients.push(rgb)
}
return gradients
}
}
function isColor(strColor) {
var test = /^#[0-9A-F]{6}$/i.test(strColor);
if (test == true) {
return true;
} else {
return false;
}
}
function colorMaker(start, end, steps, step) {
// let redDiff = (start.red > end.red) ? start.red - end.red : end.red - start.red;
let val
if (start > end) {
let singleStep = (start - end) / steps
val = start - singleStep * step
} else if (end > start) {
let singleStep = (end - start) / steps
val = start + singleStep * step
} else {
val = start | end
}
return ~~val
}
// strng to object converson
function parseRGB(rgb) {
let baseSplit = rgb.split('(')[1].split(',')
let red = parseInt(baseSplit[0], 1)
let green = parseInt(baseSplit[1], 1)
let blue = parseInt(baseSplit[2].split(')')[0], 1)
return { red, green, blue }
}
function hexToRgb(hex) {
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? {
red: parseInt(result[1], 16),
green: parseInt(result[2], 16),
blue: parseInt(result[3], 16)
}
: null;
}