-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pixel.js
64 lines (50 loc) · 1.78 KB
/
Pixel.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
import {Point} from './Point';
import {Color} from './Color';
export class Pixel extends Point {
constructor(coord, color) {
super(...coord);
this.color = color;
}
// Avanzar un pixel desde la posición actual en la dirección del punto 2
advance(pixel2) {
let address = pixel2.coords.subtraction(this.coords).normalize();
this.dimmensions = this.dimmensions.map((d, i) => Math.round(d + address[i]));
}
// some useful effects
invert() {
this.color.r = 255 - this.color.r;
this.color.g = 255 - this.color.g;
this.color.b = 255 - this.color.b;
}
contrast(factor) {
this.color.r = factor * (this.color.r - 128) + 128;
this.color.g = factor * (this.color.g - 128) + 128;
this.color.b = factor * (this.color.b - 128) + 128;
}
grayScale() {
let avg = (this.color.r + this.color.g + this.color.b) / 3;
this.color.r = avg;
this.color.g = avg;
this.color.b = avg;
}
sepia() {
this.color.r = 255 - this.color.r;
this.color.g = 255 - this.color.g;
this.color.b = 255 - this.color.b;
this.color.r = (this.color.r * .393) + (this.color.r * .769) + (this.color.r * .189);
this.color.g = (this.color.g * .349) + (this.color.g * .686) + (this.color.g * .168);
this.color.b = (this.color.b * .272) + (this.color.b * .534) + (this.color.b * .131);
}
distanciaObjetiva(pixel2) {
return Math.sqrt(Math.pow(this.x - pixel2.x, 2)
+ Math.pow(this.x - pixel2.y, 2)
+ Math.pow(this.color.r - pixel2.color.r, 2)
+ Math.pow(this.color.g - pixel2.color.g, 2)
+ Math.pow(this.color.b - pixel2.color.b, 2)
+ Math.pow(this.color.a - pixel2.color.a, 2));
}
distanciaSubjetiva(pixel2) {
return this.coords.distance(pixel2.coords);
}
// Incluir el imagedata del canvas en un objeto
}