-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlitchRothko.js
62 lines (50 loc) · 1.38 KB
/
GlitchRothko.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
import Artifier from './Artifier.js';
export default class GlitchRothko extends Artifier {
constructor() {
super();
}
artFunction(ctx, width, height) {
const imageData = ctx.getImageData(0, 0, width, height).data;
let reds = [];
let greens = [];
let blues = [];
let grayscales = [];
for (let h = 0; h < height; h++) {
for (let w = 0; w < width; w++) {
let current = this.getPixelAt(h, w, width, imageData);
let winnerType = this.getHighestOfThree(current);
if (winnerType === 'red') {
this.mergeArrays(reds, current);
}
if (winnerType === 'green') {
this.mergeArrays(greens, current);
}
if (winnerType === 'blue') {
this.mergeArrays(blues, current);
}
if (winnerType === 'grayscale') {
this.mergeArrays(grayscales, current);
}
}
}
return new Uint8ClampedArray([...reds, ...greens, ...blues, ...grayscales]);
}
getHighestOfThree(arr) {
let winnerType, winnerIndex;
if (arr[0] === arr[1] && arr[0] === arr[2]) {
return 'grayscale';
}
if (arr[0] > arr[1]) {
winnerType = 'red';
winnerIndex = 0;
} else {
winnerType = 'green';
winnerIndex = 1;
}
if (arr[2] > arr[winnerIndex]) {
winnerType = 'blue';
}
return winnerType;
}
}
const gr = new GlitchRothko()