Skip to content

Instantly share code, notes, and snippets.

@konsultaner
Created April 2, 2015 06:30
Show Gist options
  • Save konsultaner/5b89c77363e8434b133c to your computer and use it in GitHub Desktop.
Save konsultaner/5b89c77363e8434b133c to your computer and use it in GitHub Desktop.
Color filtering known from Photoshop in JavaScript
var colorize = {
blendColorValue : function (color1,color2,method){
if(typeof color1 == 'undefined'){
color1 = 0;
}
if(typeof color2 == 'undefined'){
color2 = 0;
}
if(typeof this[method] == 'undefined'){
return color1;
}
var r1 = color1 >> 16,
g1 = color1 >> 8 & 0x00FF,
b1 = color1 & 0x0000FF,
r2 = color2 >> 16,
g2 = color2 >> 8 & 0x00FF,
b2 = color2 & 0x0000FF;
return parseInt((this[method](r1,r2) << 16) + (this[method](g1,g2) << 8) + (this[method](b1,b2)));
},
multiply: function(channel1,channel2){
return (channel1*channel2)/0xFF;
},
screen : function(channel1,channel2){
return (channel1+channel2-(channel1 * channel2))/0xFF;
},
overlay : function (channel1,channel2){
return (channel2 < 0x80) ? (2 * channel1 * channel2 / 0xFF):(0xFF - 2 * (0xFF - channel1) * (0xFF - channel2) / 0xFF);
},
softlight : function(channel1,channel2){
if (channel1 > 0x7F){
return channel2 + ((0xFF - channel2) * ((channel1 - 0x7F) / 0x7F) * (Math.abs(channel2-0x7F)/0xFF));
}else{
return channel2 - (channel2 * ((0x7F - channel1) / 0x7F) * (Math.abs(channel2-0x7F)/0xFF));
}
},
hardlight : function(channel1,channel2){
if (channel1 > 0x7F){
return channel2 + ((0xFF - channel2) * ((channel1 - 0x7F) / 0x7F));
}else{
return channel2 * channel1 / 0x7F;
}
},
colorDodge : function(channel1,channel2){
return (channel1 === 0xFF)?channel1:Math.min(0xFF,((channel2 << 8 ) / (0xFF - channel1)));
},
colorBurn : function(channel1,channel2){
return (channel1 === 0)?channel1:Math.max(0,(0xFF - ((0xFF - channel2) << 8 ) / channel1));
},
linearcolordodge : function(channel1,channel2){
return min(channel1 + channel2, 0xFF);
},
linearcolorburn : function(channel1,channel2){
return ((channel1 + channel2) < 0xFF)?0:(channel1 + channel2 - 0xFF);
},
darken : function(channel1,channel2){
return Math.min(channel1,channel2);
},
lighten : function(channel1,channel2){
return Math.max(channel1,channel2);
},
difference : function(channel1,channel2){
return Math.abs(channel1-channel2);
},
exclusion : function(channel1,channel2){
return channel1 + channel2 - (channel1 * channel2 / 0x7F);
},
reflex : function(channel1,channel2){
return ((channel1 === 0xFF) ? channel1:Math.min(0xFF, (channel2 * channel2 / (0xFF - channel1))));
},
linearlight : function(channel1,channel2){
return (channel1 < 0x7F) ? this.linearcolorburn(channel2,(2 * channel1)) : this.linearcolordodge(channel2,(2 * (channel1 - 0x7F)));
},
pinlight : function(channel1,channel2){
return (channel1 < 0x7F) ? this.darken(channel2,(2 * channel1)) : this.lighten(channel2,(2 * (channel1 - 0x7F)));
},
vividlight : function(channel1,channel2){
return (channel1 < 0x7F) ? this.colorBurn(channel2,(2 * channel1)) : this.colorDodge(channel2,(2 * (channel1 - 0x7F)));
},
hardmix : function(channel1,channel2){
return this.vividlight(channel1,channel2) > 0x7F ? 0 : 255;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment