Last active
August 29, 2015 14:04
-
-
Save jahan-addison/0f0fe61fd70d806109bf to your computer and use it in GitHub Desktop.
A generator of infinite contiguously-good-looking colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function(global, undefined) { | |
'use strict'; | |
var Colouri = function() { | |
this.color = '1d9caf'; | |
}; | |
global.Colouri = Colouri; | |
Colouri.prototype = (function() { | |
/** | |
* Private | |
*/ | |
var hue = function(color) { | |
color = (color.indexOf(0) == '#') ? color.substr(1) : color; | |
var rgb = color.match(/([A-Za-z0-9]{2})/g) | |
.map(function(e) { return parseInt(e, 16) }), | |
r = rgb[0] || 0, | |
g = rgb[1] || 0, | |
b = rgb[2] || 0, | |
$r = r / 255, | |
$g = g / 255, | |
$b = b / 255, | |
max = [$r, $g, $b].sort(function(a, b) { | |
return b - a; | |
}).shift(), | |
min = [$r, $g, $b].sort(function(a, b) { | |
return a - b; | |
}).shift(), | |
delta = max - min; | |
switch(max) { | |
case $r: | |
return 60 * ((($g - $b) / delta) % 6); | |
break; | |
case $g: | |
return 60 * ((($b - $r) / delta) + 2); | |
break; | |
case $b: | |
return 60 * ((($r - $g) / delta) + 4); | |
break; | |
} | |
}; | |
/** | |
* Public | |
*/ | |
return { | |
Constructor: Colouri, | |
generate: function () { | |
var currentHue = hue(this.color), | |
origHue = currentHue, | |
queue = []; | |
return function() { | |
if (currentHue + 20 >= 360) { | |
currentHue = ((currentHue + 20) - 360) + 0; | |
} else { | |
currentHue += 20; | |
} | |
if (!(queue.length % 3)) { | |
queue.push((currentHue - 180 < 0) ? (360 - currentHue - 180) : currentHue - 180); | |
} | |
queue.push(currentHue); | |
if (!(queue.length % 4)) { | |
currentHue = origHue = queue.shift(); | |
} | |
return currentHue; | |
}; | |
} | |
}; | |
})(); | |
})(this); | |
/* | |
var color = new Colouri(), | |
generator = color.generate(), | |
map = Array.prototype.map; | |
map.call(document.getElementsByTagName('div'), function(e) { | |
e.style.background = 'hsl(' + Math.floor ( generator() ) + ', 70%, ' + Math.floor((Math.random() * (50 - 20)) + 20) + '% )'; | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment