-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradientify.js
More file actions
71 lines (58 loc) · 1.9 KB
/
gradientify.js
File metadata and controls
71 lines (58 loc) · 1.9 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
var MFLOSS = MFLOSS || {};
MFLOSS.gradientify = (function() {
var module = {};
function hexToRgb(hexValue) {
var
hexArray = [],
rgbArray = []
;
if (hexValue.length > 0) {
// Remove # from hex value
if (hexValue[0] == '#') {
hexValue = hexValue.substr(1, hexValue.length);
}
if (hexValue.length == 3) {
// Expand hex value with 3 characters to 6 characters
hexArray[0] = hexValue[0] + hexValue[0];
hexArray[1] = hexValue[1] + hexValue[1];
hexArray[2] = hexValue[2] + hexValue[2];
} else {
// Create an array that puts every 2 characters in an element of the array
hexArray = hexValue.match(/.{1,2}/g);
}
// Create RGB value as an array
for (var i = 0; i < hexArray.length; i++) {
rgbArray.push(parseInt(hexArray[i], 16));
}
return rgbArray;
}
}
module.paint = function(elements, color1, color2, styleProperty) {
// Assume 1 element is passed if it's a string
if (typeof elements == 'string') {
} else {
var
color1Rgb = hexToRgb(color1),
color2Rgb = hexToRgb(color2),
rIncrement = Math.ceil((color2Rgb[0] - color1Rgb[0]) / elements.length),
gIncrement = Math.ceil((color2Rgb[1] - color1Rgb[1]) / elements.length),
bIncrement = Math.ceil((color2Rgb[2] - color1Rgb[2]) / elements.length),
elementCount = 0,
tempColorArray = []
;
for (var i in elements) {
// Figures out the RGB values for the next element
// Gradient progression with RGB is linear
tempColorArray.push(color1Rgb[0] + (rIncrement * elementCount));
tempColorArray.push(color1Rgb[1] + (gIncrement * elementCount));
tempColorArray.push(color1Rgb[2] + (bIncrement * elementCount));
// Applies gradient color
elements[i].style[styleProperty] = 'rgb(' + tempColorArray.join(',') + ')';
// Resets array for next element
tempColorArray = [];
elementCount++;
}
}
}
return module.paint;
}());