Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/2053866 to your computer and use it in GitHub Desktop.
Save jonathantneal/2053866 to your computer and use it in GitHub Desktop.
Canvas Prototype: Contrast
HTMLCanvasElement.prototype.contrast = function (value) {
var
canvas = this,
context = canvas.getContext('2d'),
imageData = context.getImageData(0, 0, canvas.width, canvas.height),
canvasPixelArray = imageData.data,
canvasPixelArrayLength = canvasPixelArray.length,
i = 0;
value = (parseFloat(value) || 0) + 1;
for (; i < canvasPixelArrayLength; i += 4) {
canvasPixelArray[i] = ((((canvasPixelArray[i] / 255) - 0.5) * value) + 0.5) * 255;
canvasPixelArray[i + 1] = ((((canvasPixelArray[i + 1] / 255) - 0.5) * value) + 0.5) * 255;
canvasPixelArray[i + 2] = ((((canvasPixelArray[i + 2] / 255) - 0.5) * value) + 0.5) * 255;
}
context.putImageData(imageData, 0, 0);
return canvas;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment