Created
March 17, 2012 00:29
-
-
Save jonathantneal/2053866 to your computer and use it in GitHub Desktop.
Canvas Prototype: Contrast
This file contains 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
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