-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorHSB.java
More file actions
61 lines (55 loc) · 2.12 KB
/
Copy pathColorHSB.java
File metadata and controls
61 lines (55 loc) · 2.12 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
public class ColorHSB {
private final int hue, sat, bri;
// Creates a color with hue h, saturation s, and brightness b.
public ColorHSB(int h, int s, int b) {
if (h < 0 || h > 359)
throw new IllegalArgumentException("the hue must be between 0 and 359");
if (s < 0 || s > 100)
throw new IllegalArgumentException("the saturation must be between 0 and 100");
if (b < 0 || b > 100)
throw new IllegalArgumentException("the brightness must be between 0 and 100");
hue = h;
sat = s;
bri = b;
}
// Returns a string representation of this color, using the format (h, s, b).
public String toString() {
return "(" + hue + ", " + sat + ", " + bri + ")";
}
// Is this color a shade of gray?
public boolean isGrayscale() {
if (sat == 0 || bri == 0) return true;
return false;
}
// Returns the squared distance between the two colors.
public int distanceSquaredTo(ColorHSB that) {
if (that == null)
throw new IllegalArgumentException("argument shouldn't be null!");
int dh = hue - that.hue;
int ds = sat - that.sat;
int db = bri - that.bri;
int x = 360 - Math.abs(dh);
int rtrn = Math.min((dh * dh), (x * x)) + (ds * ds) + (db * db);
return rtrn;
}
public static void main(String[] args) {
int h1 = Integer.parseInt(args[0]);
int s1 = Integer.parseInt(args[1]);
int b1 = Integer.parseInt(args[2]);
int f = Integer.MAX_VALUE;
String colorName = "";
ColorHSB colorHSB1 = new ColorHSB(h1, s1, b1);
while (!StdIn.isEmpty()) {
String string = StdIn.readString();
int h2 = StdIn.readInt();
int s2 = StdIn.readInt();
int b2 = StdIn.readInt();
ColorHSB colorHSB2 = new ColorHSB(h2, s2, b2);
if (colorHSB1.distanceSquaredTo(colorHSB2) < f) {
f = colorHSB1.distanceSquaredTo(colorHSB2);
colorName = string + " " + colorHSB2;
}
}
System.out.println(colorName);
}
}