-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.java
More file actions
214 lines (192 loc) · 6.32 KB
/
Copy pathColor.java
File metadata and controls
214 lines (192 loc) · 6.32 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* The Color class stores information about a particular color.
*
* @author Nazzzerine Waldon
* @version 09/19/2017
*/
package project1;
public class Color implements Comparable<Color>{
// colorHexValue and colorName need to be accessed in the ColorList class
protected String colorHexValue;
protected String colorName;
// red, green, and blue only need to be accessed in the Color class
private int red;
private int green;
private int blue;
/**
* Overrides the equals method in the Object class
*
* @param o is an instance of Object that is converted to an instance of Color
*
* @return true if the hexadecimal strings of the two Color objects are the same.
*/
@Override
public boolean equals(Object o) {
// if the hexadecimal representations are equal
Color c = (Color) o;
if (this.colorHexValue.equalsIgnoreCase(c.colorHexValue)) {
return true;
} else {
return false;
}
}
/**
* Alpha numerically compares the hexadecimal value associated with the color
*
* @param c is a Color object
*
* @return the integer associated with the comparison
*/
public int compareTo (Color c) {
return (this.colorHexValue.compareToIgnoreCase(c.colorHexValue));
}
/**
* Constructs an instance of color using the hexadecimal value and converts from Hex to RGB
*
* @param colorHexValue is the hexadecimal value of a color
*
* @throws IllegalArgumentException if what is entered is not a valid color specification.
*/
public Color ( String colorHexValue ) throws IllegalArgumentException {
// if the input is in the wrong format, throw exception
if (colorHexValue.charAt(0) != '#') {
throw new IllegalArgumentException("Error: This is not a valid color specifiction.");
}
if (colorHexValue.length() != 7) {
throw new IllegalArgumentException("Error: This is not a valid color specifiction.");
}
// if valid, make sure that the letters are valid, otherwise throw exception
if (colorHexValue.charAt(0) == '#' && colorHexValue.length() == 7) {
for (int i = 1; i < colorHexValue.length(); i++)
{
char index = colorHexValue.charAt(i);
if (Character.isLetter(index)) {
index = Character.toLowerCase(index);
if (index <'a'|| index > 'f') {
throw new IllegalArgumentException("Error: This is not a valid color specification.");
}
}
}
}
this.colorHexValue = colorHexValue;
// convert from Hex to RGB for red, green, and blue
red = Integer.valueOf(colorHexValue.substring(1, 3), 16);
blue = Integer.valueOf(colorHexValue.substring(5, 7), 16);
green = Integer.valueOf(colorHexValue.substring(3,5), 16);
}
/**
* Constructs an instance of color using the hexadecimal value and name of the color. It also converts from Hex to RGB
*
* @param colorHexValue is the hexadecimal value of a color, colorName is the name of the color.
*
* @throws IllegalArgumentException if what is entered is not a valid color specification.
*/
public Color ( String colorHexValue, String colorName ) throws IllegalArgumentException {
// if input is in the wrong format, throw exception
if (colorHexValue.charAt(0) != '#') {
throw new IllegalArgumentException("Error: This is not a valid color specifiction.");
}
if (colorHexValue.length() != 7) {
throw new IllegalArgumentException("Error: This is not a valid color specifiction.");
}
// if valid, make sure that the letters are valid, otherwise throw exception
if (colorHexValue.charAt(0) == '#' && colorHexValue.length() == 7) {
for (int i = 1; i < colorHexValue.length(); i++)
{
char index = colorHexValue.charAt(i);
if (Character.isLetter(index)) {
index = Character.toLowerCase(index);
if (index <'a'|| index > 'f') {
throw new IllegalArgumentException("Error: This is not a valid color specifiction.");
}
}
}
}
this.colorHexValue = colorHexValue;
this.colorName = colorName;
// convert from Hex to RGB for red, green, and blue
red = Integer.valueOf(colorHexValue.substring(1, 3), 16);
green = Integer.valueOf(colorHexValue.substring(3,5), 16);
blue = Integer.valueOf(colorHexValue.substring(5, 7), 16);
}
/**
* Constructs an instance of color using the RGB values and converts from RGB to Hex
*
* @param red, green, and blue are integer RGB values
*
* @throws IllegalArgumentException if what is entered is not a valid color specification.
*/
public Color ( int red, int green, int blue ) throws IllegalArgumentException {
// since given rgb values, no rgb to hex converter needed
this.red = red;
this.green = green;
this.blue = blue;
//if the rgb values aren't valid, throw exception
if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue> 255) {
throw new IllegalArgumentException("Error: This is not a valid color specifiction.");
}
// convert from rgb to hex
colorHexValue = String.format("#%02x%02x%02x", red, green, blue);
}
/**
* Returns the value of the red component.
*
* @return the red component
*/
public int getRed() {
return red;
}
/**
* Returns the value of the green component.
*
* @return the green component
*/
public int getGreen() {
return green;
}
/**
* Returns the value of the blue component.
*
* @return the blue component
*/
public int getBlue() {
return blue;
}
/**
* Returns the name of the color from colorName
*
* @return name of the color or null if there is none
*/
public String getName() {
if (this.colorName != null) {
return this.colorName;
} else {
return null;
}
}
/**
* Returns the Hex Value
*
* @return the hex value of each color
*/
public String getHexValue() {
return colorHexValue;
}
/**
* Overrides the toString method so that it returns in the desired format
*
* @return #HEXVAL, ( 0, 0, 0), COLORNAME if colorName exists
* otherwise, #HEXVAL, ( 0, 0, 0)
*/
@Override
public String toString() {
// override the toString method so that it returns in the desired format
String stringToReturn;
if (this.colorName == null) {
stringToReturn = (this.colorHexValue.toUpperCase() + ", " + String.format("(%3d,%3d,%3d)", this.red, this.green, this.blue));
} else {
stringToReturn = (this.colorHexValue.toUpperCase() + ", " + String.format("(%3d,%3d,%3d)", this.red, this.green, this.blue) + ", " + this.colorName);
}
return stringToReturn;
}
}