-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicColorTerminalLab.js
73 lines (66 loc) · 1.65 KB
/
BasicColorTerminalLab.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
import {
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
PURPLE,
PINK,
WHITE,
BLACK,
GRAY,
BROWN
} from "./constants.js";
import Artifier from "./Artifier.js";
class BasicColorTerminalLab extends Artifier {
constructor() {
super();
this.colorsForCanvas = [
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
PURPLE,
PINK,
WHITE,
BLACK,
GRAY,
BROWN
];
this.colors = this.colorsForCanvas.map(color =>
this.xyzToLab(this.rgbToXyz(color))
);
}
artFunction(ctx, width, height) {
const imageData = ctx.getImageData(0, 0, width, height).data;
const colors = this.colors;
let newImageArray = [];
for (let h = 0; h < height; h++) {
for (let w = 0; w < width; w++) {
let currentRgb = this.getPixelAt(h, w, width, imageData);
if (currentRgb[3] !== 255) {
currentRgb = this.rgbaToRgb(currentRgb);
}
const current = this.xyzToLab(this.rgbToXyz(currentRgb));
let closest;
let minDist = 1000000000; // temporary
for (let i = 0; i < colors.length; i++) {
let color = colors[i];
let redDiff = color[0] - current[0];
let greenDiff = color[1] - current[1];
let blueDiff = color[2] - current[2];
let dist =
redDiff * redDiff + greenDiff * greenDiff + blueDiff * blueDiff;
if (dist < minDist) {
minDist = dist;
closest = i;
}
}
this.mergeArrays(newImageArray, this.colorsForCanvas[closest]);
}
}
return new Uint8ClampedArray(newImageArray);
}
}
const bct = new BasicColorTerminalLab();