-
Notifications
You must be signed in to change notification settings - Fork 0
/
Color.java
147 lines (131 loc) · 2.6 KB
/
Color.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package primitives;
public class Color {
private double _r;
private double _g;
private double _b;
public static final Color BLACK = new Color(0, 0, 0);
public static final Color WHITE = new Color(255, 255, 255);
public static final Color RED = new Color(255, 0, 0);
public static final Color GREEN = new Color(0, 255, 0);
public static final Color BLUE = new Color(0, 0, 255);
// ***************** Constructors ********************** //
/**
* construct color with 3 numbers
*
* @param r
* - double
* @param g
* - double
* @param b
* - double
*/
public Color(double r, double g, double b) {
_r = r;
_g = g;
_b = b;
}
/**
* construct color with 3 numbers
*
* @param r
* - int
* @param g
* - int
* @param b
* - int
*/
public Color(int r, int g, int b) {
_r = r;
_g = g;
_b = b;
}
/**
* copy constructor
*
* @param other
*/
public Color(Color other) {
_r = other._r;
_g = other._g;
_b = other._b;
}
/**
* construct color with java color
*
* @param color
*/
public Color(java.awt.Color color) {
_r = color.getRed();
_g = color.getGreen();
_b = color.getBlue();
}
/**
* default constructor
*/
public Color() {
_r = 0;
_g = 0;
_b = 0;
}
// ***************** Getters/Setters ********************** //
public java.awt.Color getColor() {
int r = _r > 255 ? 255 : (int) _r;
int g = _g > 255 ? 255 : (int) _g;
int b = _b > 255 ? 255 : (int) _b;
return new java.awt.Color(r, g, b);
}
public void setColor(double r, double g, double b) {
_r = r;
_g = g;
_b = b;
}
public void setColor(Color other) {
setColor(other._r, other._g, other._b);
}
// ***************** Operations ******************** //
/**
* adds a color
*
* @param x
* - color
* @return color intensified by x
*/
public Color add(Color... colors) {
for (Color x : colors) {
_r += x._r;
_g += x._g;
_b += x._b;
}
return this;
}
/**
* multiply a color
*
* @param x
* - double
* @return color intensified by x
*/
public Color scale(double x) {
_r *= x;
_g *= x;
_b *= x;
return this;
}
/**
* divide a color
*
* @param x
* - double
* @return color reduced by x
*/
public Color reduce(double x) {
_r /= x;
_g /= x;
_b /= x;
return this;
}
@Override
public String toString() {
return "Color [" + _r + ", " + _g + ", " + _b + "]";
}
}