Improved filters.

This commit is contained in:
Devine Lu Linvega 2016-11-15 10:38:13 -08:00
parent 16d3575fc0
commit b473437de5

View File

@ -12,7 +12,7 @@ function Filter(element)
switch(filter_name) { switch(filter_name) {
case "saturation": case "saturation":
this.filter_saturation(p); this.filter_saturation(this.pixels(),p);
break; break;
} }
} }
@ -28,20 +28,20 @@ function Filter(element)
// Filters // Filters
this.filter_saturation = function() this.filter_saturation = function(pixels = this.pixels(),p = null)
{ {
var imgPixels = this.pixels(); var d = pixels.data;
for(var y = 0; y < imgPixels.height; y++){ for (var i=0; i<d.length; i+=4) {
for(var x = 0; x < imgPixels.width; x++){ var r = d[i];
var i = (y * 4) * imgPixels.width + x * 4; var g = d[i+1];
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3; var b = d[i+2];
imgPixels.data[i] = avg; // CIE luminance for the RGB
imgPixels.data[i + 1] = avg; // The human eye is bad at seeing red and blue, so we de-emphasize them.
imgPixels.data[i + 2] = avg; var v = 0.2126*r + 0.7152*g + 0.0722*b;
} d[i] = d[i+1] = d[i+2] = v
} }
ronin.canvas.clear(); ronin.canvas.clear();
ronin.canvas.context().putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height); ronin.canvas.context().putImageData(pixels, 0, 0, 0, 0, pixels.width, pixels.height);
} }
// //