-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
177 lines (158 loc) · 5.14 KB
/
script.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var canvas = document.getElementById('canvas'),
originalImage = null,
imageGrey = null,
imageRed = null,
imageRainbow = null;
function loadImage() {
var file = document.getElementById('file');
originalImage = new SimpleImage(file);
imageGrey = new SimpleImage(file);
imageRed = new SimpleImage(file);
imageRainbow = new SimpleImage(file);
originalImage.drawTo(canvas);
}
function filterGrey() {
// Reset Image
for (var pixel of imageGrey.values()) {
var originalPixel = originalImage.getPixel(pixel.getX(), pixel.getY());
imageGrey.setPixel(pixel.getX(), pixel.getY(), originalPixel);
}
// Grey Filter
for (var pixel of imageGrey.values()) {
var avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3;
pixel.setRed(avg);
pixel.setGreen(avg);
pixel.setBlue(avg);
}
imageGrey.drawTo(canvas);
}
function printOriginal(img) {
img.drawTo(canvas);
}
function makeRed() {
// Reset Image
for (var pixel of imageRed.values()) {
var originalPixel = originalImage.getPixel(pixel.getX(), pixel.getY());
imageRed.setPixel(pixel.getX(), pixel.getY(), originalPixel);
}
// Red Filter
for (var pixel of imageRed.values()) {
var avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3;
if (avg < 128) {
pixel.setRed(avg * 2 - 255);
pixel.setBlue(avg * 2 - 255);
} else {
pixel.setRed(255);
pixel.setGreen(avg * 2 - 255);
pixel.setBlue(avg * 2 - 255);
}
}
imageRed.drawTo(canvas);
}
function makeRainbow() {
// Reset Image
for (var pixel of imageRainbow.values()) {
var originalPixel = originalImage.getPixel(pixel.getX(), pixel.getY());
imageRainbow.setPixel(pixel.getX(), pixel.getY(), originalPixel);
}
// Rainbow Filter
for (var pixel of imageRainbow.values()) {
var avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3;
/* ---- Red Strip ----- */
if (pixel.getY() <= imageRainbow.getHeight() * (1 / 7)) {
if (avg < 128) {
pixel.setRed(2 * avg);
pixel.setGreen(0);
pixel.setBlue(0);
} else {
pixel.setRed(255);
pixel.setGreen(avg * 2 - 255);
pixel.setBlue(avg * 2 - 255);
}
}
/* ----- Orange Stripe ----- */
if (pixel.getY() > imageRainbow.getHeight() * (1 / 7) && pixel.getY() <= imageRainbow.getHeight() * (2 / 7)) {
if (avg < 128) {
pixel.setRed(2 * avg);
pixel.setGreen(0.8 * avg);
pixel.setBlue(0);
} else {
pixel.setRed(255);
pixel.setGreen(1.2 * avg - 51);
pixel.setBlue(avg * 2 - 255);
}
}
/* ----- Yellow Strip ----- */
if (pixel.getY() > imageRainbow.getHeight() * (2 / 7) && pixel.getY() <= imageRainbow.getHeight() * (3 / 7)) {
if (avg < 128) {
pixel.setRed(2 * avg);
pixel.setGreen(2 * avg);
pixel.setBlue(0);
} else {
pixel.setRed(255);
pixel.setGreen(255);
pixel.setBlue(avg * 2 - 255);
}
}
/* ----- Green Strip ----- */
if (pixel.getY() > imageRainbow.getHeight() * (3 / 7) && pixel.getY() <= imageRainbow.getHeight() * (4 / 7)) {
if (avg < 128) {
pixel.setRed(0);
pixel.setGreen(2 * avg);
pixel.setBlue(0);
} else {
pixel.setRed(avg * 2 - 255);
pixel.setGreen(255);
pixel.setBlue(avg * 2 - 255);
}
}
/* ----- Blue Strip ----- */
if (pixel.getY() > imageRainbow.getHeight() * (4 / 7) && pixel.getY() <= imageRainbow.getHeight() * (5 / 7)) {
if (avg < 128) {
pixel.setRed(0);
pixel.setGreen(0);
pixel.setBlue(2 * avg);
} else {
pixel.setRed(avg * 2 - 255);
pixel.setGreen(avg * 2 - 255);
pixel.setBlue(255);
}
}
/* ----- Indigo Strip ----- */
if (pixel.getY() > imageRainbow.getHeight() * (5 / 7) && pixel.getY() <= imageRainbow.getHeight() * (6 / 7)) {
if (avg < 128) {
pixel.setRed(0.8 * avg);
pixel.setGreen(0);
pixel.setBlue(2 * avg);
} else {
pixel.setRed(1.2 * avg - 51);
pixel.setGreen(avg * 2 - 255);
pixel.setBlue(255);
}
}
/* ----- Violet Strip ----- */
if (pixel.getY() > imageRainbow.getHeight() * (6 / 7) && pixel.getY() <= imageRainbow.getHeight() * (7 / 7)) {
if (avg < 128) {
pixel.setRed(1.6 * avg);
pixel.setGreen(0);
pixel.setBlue(1.6 * avg);
} else {
pixel.setRed(0.4 * avg + 153);
pixel.setGreen(avg * 2 - 255);
pixel.setBlue(0.4 * avg + 153);
}
}
}
imageRainbow.drawTo(canvas);
}
function makeGrey() {
if (imageGrey != null) {
filterGrey();
}
}
function resetImage() {
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
printOriginal(originalImage);
}
// Developed by Mohammad Kiaei