Last active
November 27, 2016 23:04
-
-
Save eymiha/10908799 to your computer and use it in GitHub Desktop.
Color Interpolator - when you need to find a color in a colorstop-defined gradient.
This file contains hidden or 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
// usage: prime with a set of color stops, then ask for colors in the range. | |
// | |
// colors = ['#37d78a','#40be8f','#49a594','#538d9a','#5c749f','#655ba4']; | |
// colorInterpolator = new ColorInterpolator(); | |
// colorInterpolator.prime([ | |
// { value: 0.0, color: colors[0] }, | |
// { value: 0.2, color: colors[1] }, | |
// { value: 0.4, color: colors[2] }, | |
// { value: 0.6, color: colors[3] }, | |
// { value: 0.8, color: colors[4] }, | |
// { value: 1.0, color: colors[5] }]) | |
// | |
// colorInterpolator.color(0.35) | |
function ColorInterpolator() { | |
this.interpolators = null | |
this.start = { value: 0, color: [0,0,0] } | |
this.end = { value: 1, color: [255,255,255] } | |
} | |
ColorInterpolator.prototype = { | |
prime: function(colorStops) { | |
var that = this | |
$.each(colorStops, function(i,colorStop) { | |
if (typeof colorStop.color === 'string') { | |
colorStop.color = that.rgb(colorStop.color) | |
} | |
}) | |
if (colorStops.length > 2) { | |
this.interpolators = [] | |
for (var i = 0; i < colorStops.length-1; i++) { | |
var newStops = [colorStops[i],colorStops[i+1]] | |
this.interpolators.push(new ColorInterpolator().prime(newStops)) | |
} | |
} else if (colorStops.length == 2) { | |
this.start = colorStops[0] | |
this.end = colorStops[1] | |
return this | |
} else if (colorStops.length == 1) { | |
var newStops = [[that.start,colorStops[0]], [colorStops[0],that.end]] | |
this.prime(newStops) | |
} | |
}, | |
rgbColor: function(value) { | |
var interpolators = this.interpolators | |
if (interpolators) { | |
if (value < interpolators[0].value) | |
value = interpolators[0].value | |
else if (value > interpolators[interpolators.length-1].value) | |
value = interpolators[interpolators.length-1].value | |
for (var i = 0; i < interpolators.length; i++) { | |
var interpolator = interpolators[i] | |
if ((interpolator.start.value <= value) && | |
(interpolator.end.value >= value)) { | |
return interpolator.rgbColor(value) | |
} | |
} | |
} else { | |
var start = this.start | |
var end = this.end | |
var fraction = (value-start.value)/(end.value-start.value) | |
var red = Math.floor(((end.color[0]-start.color[0])*fraction)+start.color[0]) | |
var green = Math.floor(((end.color[1]-start.color[1])*fraction)+start.color[1]) | |
var blue = Math.floor(((end.color[2]-start.color[2])*fraction)+start.color[2]) | |
return [red,green,blue] | |
} | |
}, | |
toHex: function(d) { | |
return ("0"+(Number(d).toString(16))).slice(-2) | |
}, | |
color: function(value) { | |
var rgb = this.rgbColor(value) | |
return "#"+this.toHex(rgb[0])+this.toHex(rgb[1])+this.toHex(rgb[2]) | |
}, | |
rgb: function(value) { | |
return [parseInt(value.substr(1,2),16), | |
parseInt(value.substr(3,2),16), | |
parseInt(value.substr(5,2),16)] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment