-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (85 loc) · 2.54 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var _ = require('underscore');
//start end
//step: the step of gradient
function getGradientIterator(start, end, step) {
var colors = genGradientColors.apply(null, arguments);
//iterator
return _iterate(colors);
}
function genGradientColors(startC, endC, step) {
step -= 2;
var startArr = _convertToRGB(startC);
var endArr = _convertToRGB(endC);
var alpha = 0.0;
var gradientColors = [];
for (var i = 0; i < step; i++) {
var c = [];
alpha += (1.0 / (step + 1));
var c = _.map(startArr, function(val, j) {
var val = endArr[j] * alpha + (1 - alpha) * startArr[j];
var c = parseInt(val);
if (isNaN(val)) {
return 0;
}
return Math.round(Math.min(Math.max(0, c), 255));
});
gradientColors.push(_convertToHex(c));
}
gradientColors.unshift(startC);
gradientColors.push(endC);
return gradientColors;
}
function _iterate(arr) {
var idx = 0;
return function() {
if (idx < arr.length) {
return arr[idx++];
} else {
return null;
}
};
}
//Convert Integer to Hex
//10 => 0A 254 => 255
function _hex(n) {
var str = n.toString(16).toUpperCase();
return str.length == 2 ? str : '0' + str;
}
//[254, 10, 9] => #FE0A09
function _convertToHex(rgbArr) {
if (rgbArr.toString() !== '[object Array]' && rgbArr.length != 3) {
console.error('rgbArr should be an array with length equals to 3.');
throw new Error('Invalid parameter');
}
var hex = _(rgbArr).map(_hex).reduce(function(prev, curr) {
return prev + curr;
});
return '#' + hex;
}
//#FCD => [15, 12, 13]
//#FF0C0D => [255, 12, 13]
function _convertToRGB(hex) {
if (_.isString(hex) == false || hex.charAt(0) !== '#') {
console.error('hex should be a string start with \'#\'.');
throw new Error('Invalid parameter');
}
var colors = null;
if (hex.length === 4) {
//abc => ['a','b','c'] => ['aa', 'bb', 'cc']
colors = hex.slice(1).match(/.{1,1}/g).map(colors, function(val) {
return val + val;
});
} else if (hex.length === 7) {
colors = hex.slice(1).match(/.{1,2}/g);
} else {
console.error('hex should be a string with length equals to 4 or 7');
throw new Error('Invalid parameter');
}
return colors.map(function(val) {
return parseInt(val, 16);
});
}
module.exports = {
genGradientColors: genGradientColors,
getGradientIterator: getGradientIterator
};