forked from ryan-endacott/javascript-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coolFilters.js
129 lines (113 loc) · 3.36 KB
/
coolFilters.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
// This file contains many filters that you can use with the JavaScript Photofilter demo!
// The demo is the index.html file in this folder.
// It is also currently hosted at http://mizzouacm.github.io/javascript-workshop/
// Past each filter
// NOTE: You may want to try to figure them out yourself! It can be fun!
// The current filters in this file are:
// 1. Unique color filter
// 2. Basic sepia filter
// 3. Black and white filter
// 4. Simple brightness filter
// 5. Stripes effect
// 6. Checkerboard effect
// 7. Grid effect
// See if you can implement them before checking the solution!
// The filters:
/*
* Unique Colors filter:
*
* This filter should hopefully (if I'm correct) scale
* the image down to have a maximum of numColors unique colors.
* The reason the image won't have exactly numColors unique colors (other than rounding)
* is because it scales each pixel individually, so it only guarantees
* a maximum of numColors uniques because each unique value may not be reached.
*
* Note: The inspiration for this filter comes from http://www.reddit.com/r/programming/comments/1bicsx/after_several_years_of_inactivity_the_underhanded/c96yx6w?context=2
* That filter can also be implemented exactly :) It's very interesting.
*
* Note 2: It may be a bit over for some values because of rounding.
* There is also probably a better way to do this.
*/
var numColors = 256;
var h = 255 / Math.pow(numColors, 1/3);
function filter(pixels) {
for (var i = 0; i < pixels.length; i++) {
var p = pixels[i];
p.r = Math.floor(p.r / h) * h;
p.g = Math.floor(p.g / h) * h;
p.b = Math.floor(p.b / h) * h;
}
}
// Basic sepia filter
function filter(pixels) {
for (var i = 0; i < pixels.length; i++) {
var p = pixels[i];
p.r = p.r * .8;
p.g = p.g * .5;
p.b = p.b * .2;
}
}
// Black and white filter
function filter(pixels) {
for (var i = 0; i < pixels.length; i++) {
var p = pixels[i];
// Average all of the colors so they are all equal to make a shade of gray.
p.r = p.g = p.b = (p.r + p.g + p.b)/3;
}
}
// Simple brightness filter
var delta = 20; // Change this to change bright/dim effect
function filter(pixels) {
for (var i = 0; i < pixels.length; i++) {
var p = pixels[i];
p.r += delta;
p.g += delta;
p.b += delta;
}
}
// Stripes effect
var stripeSize = 50;
var helper = stripeSize * 2;
function filter(pixels) {
for (var i = 0; i < pixels.length; i++) {
var p = pixels[i];
// Change to y to make horizontal stripes
if (p.x % helper > stripeSize) {
// Add your effect here
p.r = 255;
}
}
}
// Checkerboard effect
var squareSize = 50;
var helper = squareSize * 2;
function filter(pixels) {
for (var i = 0; i < pixels.length; i++) {
var p = pixels[i];
if ((p.x % helper > squareSize && p.y % helper > squareSize) ||
(p.x % helper < squareSize && p.y % helper < squareSize)) {
// Add your effect here
p.r = p.g = p.b = 255;
}
}
}
// Grid effect
var gridSize = 50;
function filter(pixels) {
for (var i = 0; i < pixels.length; i++) {
var p = pixels[i];
if (p.x % gridSize == 0 || p.y % gridSize == 0) {
// Add your effect here
p.r = p.g = p.b = 255;
}
}
}
// Negative filter
function filter(pixels) {
for (var i = 0; i < pixels.length; i++) {
var p = pixels[i];
p.r = 255 - p.r;
p.g = 255 - p.g;
p.b = 255 - p.b;
}
}