Skip to content

Instantly share code, notes, and snippets.

@mattneary
Created November 19, 2011 18:44
Show Gist options
  • Save mattneary/1379204 to your computer and use it in GitHub Desktop.
Save mattneary/1379204 to your computer and use it in GitHub Desktop.
Grayscale to Color
function toColor(grayscale) {
var hexes = [0, 64, 128, 192, 255],
answers = [[]];
for( var i = 0; i < 5; i++ ) {
//Cycle through first value
for( var j = 0; j < 5; j++ ) {
//Cycle through second value
for( var k = 0; k < 5; k++ ) {
//Cycle through third value
var coeffs = [hexes[i],hexes[j],hexes[k]],
color = .59*hexes[i] + .3*hexes[j] + .11*hexes[k],
delta = Math.abs(color-target),
bool = delta < 5;
answers.push( bool ? coeffs : null );
}
}
}
return (function(newA) { //Filter array
for( i in answers )
answers[i] ? newA[i] = answers[i] : '';
return newA;
})([[]]).pop(); //Return first item (Reddest)
}
function toColor(a){var b=[0,64,128,192,255],c=[[]];for(var d=0;d<5;d++){for(var e=0;e<5;e++){for(var f=0;f<5;f++){var g=[b[d],b[e],b[f]],h=.59*b[d]+.3*b[e]+.11*b[f],i=Math.abs(h-target),j=i<5;c.push(j?g:null)}}}return function(a){for(d in c)c[d]?a[d]=c[d]:"";return a}([[]]).pop()}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment