-
Notifications
You must be signed in to change notification settings - Fork 1
/
Color.pde
119 lines (119 loc) · 3.28 KB
/
Color.pde
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
class Color{
int red, green, blue;
final static float EPSILON = 0.001;
/**
* Constructors
* Empty parameter list returns Color with RGB values set to zero (black)
* Can take another Color and creates a copy of that color
* Can take a processing primitive color and creates a Color of that color
*/
Color(){
this.red=0;this.green=0;this.blue=0;
}
Color( Color c ){
this.red = c.red;
this.green = c.green;
this.blue = c.blue;
}
Color( color c ){
this.red = c >> 16 & 0xFF;
this.green = c >> 8 & 0xFF;
this.blue = c & 0xFF;
}
/**
* Given either a Color or a color,
* returns true if they are within 0.0001 of each other
*/
boolean equals(Color other){
return (Math.abs(this.red - other.red) < Color.EPSILON)
&& (Math.abs(this.green - other.green) < Color.EPSILON)
&& (Math.abs(this.blue - other.blue) < Color.EPSILON);
}
boolean equals(color c){
Color other = new Color(c);
return (Math.abs(this.red - other.red) < Color.EPSILON)
&& (Math.abs(this.green - other.green) < Color.EPSILON)
&& (Math.abs(this.blue - other.blue) < Color.EPSILON);
}
/**
* Given a factor, divides each RGB value by the int or float factor
*/
void divideByFactor(int factor){
this.red /= factor;
this.green /= factor;
this.blue /= factor;
}
void divideByFactor(float factor){
this.red /= factor;
this.green /= factor;
this.blue /= factor;
}
/**
* Given a factor, multiplies each RGB value by the int or float factor
*/
void multiplyByFactor(int factor){
this.red *= factor;
this.green *= factor;
this.blue *= factor;
}
void multiplyByFactor(float factor){
this.red *= factor;
this.green *= factor;
this.blue *= factor;
}
/**
* Adds the RGB values of either Color c or color c to this Color
* WARNING: Allows for values greater than 255
*/
void addColor(color c){
this.red += c >> 16 & 0xFF;
this.green += c >> 8 & 0xFF;
this.blue += c & 0xFF;
}
void addColor(Color c){
this.red += c.red;
this.green += c.green;
this.blue += c.blue;
}
/**
* Subtracts the RGB values of either Color c or color c from this Color
* WARNING: Allows for values less than 0
*/
void subtractColor(color c){
this.red -= c >> 16 & 0xFF;
this.green -= c >> 8 & 0xFF;
this.blue -= c & 0xFF;
}
void subtractColor(Color c){
this.red -= c.red;
this.green -= c.green;
this.blue -= c.blue;
}
/**
* Returns a color from the RGB values of this Color
*/
color getColor(){
return color( this.red, this.green, this.blue );
}
/**
* Returns the squared distance between two Colors
*/
double getColorSquaredDistance( Color other ){
return Math.abs( this.red - other.red ) * Math.abs( this.red - other.red ) +
Math.abs( this.green - other.green ) * Math.abs( this.green - other.green ) +
Math.abs( this.blue - other.blue ) * Math.abs( this.blue - other.blue );
}
/**
* Returns the distance between two Colors
*/
double getColorDistance( Color other ){
return Math.sqrt( getColorSquaredDistance( other ) );
}
/**
* String representation of this Color:
* ( R, G, B)
*/
String toString(){
return "( " + this.red + ", " + this.green + ", " + this.blue + " )";
}
}