-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rgb.js
87 lines (74 loc) · 1.55 KB
/
Rgb.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
import { Vector2D } from "@inwebo/vector";
/**
* Basic RGB representation.
*
* this._x = R, this._y = G, this._z = B
* If R | G | B greater than 255 they will be clamped at 255.
*/
export default class Rgb extends Vector2D {
/**
* @return {number} [0, 255]
*/
getRed() {
return super.getX();
}
/**
* @return {number} [0, 255]
*/
getGreen() {
return super.getY();
}
/**
* @return {number} [0, 255]
*/
getBlue() {
return this._z;
}
/**
* @param {Number} value
*/
setZ(value) {
this._z = value;
}
/**
* @return {Number}
*/
getZ() {
return this._z;
}
/**
* @param {Number} x R
* @param {Number} y G
* @param {Number} z B
*/
constructor(x = 0, y = 0, z = 0) {
super(x, y);
this._z = z;
this.clamp(new Vector2D(0,0), new Vector2D(256, 256))
}
/**
* @param {Rgb} vector
* @return {boolean}
*/
equals(vector) {
return super.equals(vector) && this._z === vector.getZ();
}
/**
* @param {Vector2D} min
* @param {Vector2D} max
* @param {boolean} strict
* @return {Rgb}
*/
clamp(min, max, strict = true) {
super.clamp(min, max, strict);
if(strict === true) {
if(this.getZ() < min.getX()) {
this.setZ(min.getX());
}
if(this.getZ() > max.getY()) {
this.setZ(max.getY());
}
}
return this;
}
}