-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincrementColor.js
More file actions
14 lines (13 loc) · 868 Bytes
/
incrementColor.js
File metadata and controls
14 lines (13 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
module.exports = function(color, step){
var colorToInt = parseInt(color.substr(1), 16), // Convert HEX color to integer
nstep = parseInt(step); // Convert step to integer
if(!isNaN(colorToInt) && !isNaN(nstep)){ // Make sure that color has been converted to integer
colorToInt += nstep; // Increment integer with step
var ncolor = colorToInt.toString(16); // Convert back integer to HEX
ncolor = '#' + (new Array(7-ncolor.length).join(0)) + ncolor; // Left pad "0" to make HEX look like a color
if(/^#[0-9a-f]{6}$/i.test(ncolor)){ // Make sure that HEX is a valid color
return ncolor;
}
}
return color;
};