-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorLevels.js
66 lines (52 loc) · 1.92 KB
/
colorLevels.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Automatic color leveling for HTML images
* by AJ Farkas 2014
* http://www.afarkas.com
* Github: ajfarkas
*
* Auto Color Levels for HTML Images by AJ Farkas is licensed under
* The MIT License.
*/
$(document).ready(function(){
var imgList = document.getElementsByTagName('img');
$('img').after('<canvas></canvas>');
var colorLevel = function(imgNum){
var image = document.getElementsByTagName('img')[imgNum];
var canvas = document.getElementsByTagName('canvas')[imgNum];
image.onload = function(){
var ctx = canvas.getContext('2d');
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var pixelNum = imgData.data.length;
//initialize brightness for levels
var redMax = 0;
var redMin = 255;
var greenMax = 0;
var greenMin = 255;
var blueMax = 0;
var blueMin = 255;
for(var i = 0; i < pixelNum; i += 4){
//set min and max values for each color
if (imgData.data[i] > redMax) { redMax = imgData.data[i] };
if (imgData.data[i] < redMin) { redMin = imgData.data[i] };
if (imgData.data[i+1] > greenMax) { greenMax = imgData.data[i+1] };
if (imgData.data[i+1] < greenMin) { greenMin = imgData.data[i+1] };
if (imgData.data[i+2] > blueMax) { blueMax = imgData.data[i+2] };
if (imgData.data[i+2] < blueMin) { blueMin = imgData.data[i+2] };
}
for(var i = 0; i < pixelNum; i += 4){
//map colors to 0 - 255 range
imgData.data[i] = (imgData.data[i] - redMin) * (255 / (redMax - redMin));
imgData.data[i+1] = (imgData.data[i+1] - greenMin) * (255 / (greenMax - greenMin));
imgData.data[i+2] = (imgData.data[i+2] - blueMin) * (255 / (blueMax - blueMin));
}
ctx.putImageData(imgData, 0, 0);
//remove original img tag
$(image).remove();
};
};
for (var j = 0; j < imgList.length; j++){
colorLevel(j);
}
});