public class MyColor
1: /**
2: * A MUTABLE colour class. Note that you need to call toJavaAwtColor or
3: * toJavaFXColor on it before you can use it with coloured objects from other
4: * packages.
5: *
6: * @author Mark Young (A00000000)
7: */
8: public class MyColor {
10: // ---------- Instance Variables ---------------------------------------
11: private int red;
12: private int green;
13: private int blue;
14: private int alpha;
16: // ---------- Class Variables ------------------------------------------
17: public static final MyColor BLACK = new MyColor(0, 0, 0);
18: public static final MyColor WHITE = new MyColor(255, 255, 255);
19: public static final MyColor RED = new MyColor(255, 0, 0);
20: public static final MyColor ORANGE = new MyColor(255, 127, 0);
21: public static final MyColor YELLOW = new MyColor(255, 255, 0);
22: public static final MyColor GREEN = new MyColor(0, 255, 0);
23: public static final MyColor BLUE = new MyColor(0, 0, 255);
24: public static final MyColor PURPLE = new MyColor(127, 0, 255);
26: // ---------- Constructors ---------------------------------------------
27: public MyColor(int r, int g, int b, int a) {
28: requireInRange(r, "red");
29: requireInRange(g, "green");
30: requireInRange(b, "blue");
31: requireInRange(a, "alpha");
33: red = r;
34: green = g;
35: blue = b;
36: alpha = a;
37: }
39: public MyColor(int r, int g, int b) {
40: this(r, g, b, 255);
41: }
43: // ---------- Getters --------------------------------------------------
44: public int getRed() {
45: return red;
46: }
48: public int getGreen() {
49: return green;
50: }
52: public int getBlue() {
53: return blue;
54: }
56: public int getAlpha() {
57: return alpha;
58: }
60: // ---------- Setters --------------------------------------------------
61: public void setRed(int r) {
62: requireInRange(r, "red");
63: red = r;
64: }
66: public void setGreen(int g) {
67: requireInRange(g, "green");
68: green = g;
69: }
71: public void setBlue(int b) {
72: requireInRange(b, "blue");
73: blue = b;
74: }
76: public void setAlpha(int a) {
77: requireInRange(a, "alpha");
78: alpha = a;
79: }
81: // ---------- Observers ------------------------------------------------
82: public String toString() {
83: return String.format("Colour[%d, %d, %d, %d]", red, green, blue, alpha);
84: }
86: public MyColor brighter() {
87: return new MyColor(
88: brighter(red),
89: brighter(green),
90: brighter(blue),
91: alpha);
92: }
94: public MyColor darker() {
95: return new MyColor(
96: darker(red),
97: darker(green),
98: darker(blue),
99: alpha);
100: }
102: public java.awt.Color toJavaAwtColor() {
103: return new java.awt.Color(red, green, blue, alpha);
104: }
106: public javafx.scene.paint.Color toJavaFXColor() {
107: return new javafx.scene.paint.Color(
108: red/255f, green/255f, blue/255f, alpha/255f);
109: }
111: // ---------- Class methods --------------------------------------------
112: public static MyColor decode(String s) {
113: int r, g, b, a;
114: java.awt.Color decoded = java.awt.Color.decode(s);
116: return new MyColor(
117: decoded.getRed(),
118: decoded.getGreen(),
119: decoded.getBlue(),
120: decoded.getAlpha());
121: }
123: // ---------- Private methods ------------------------------------------
124: private static void requireInRange(int component, String part) {
125: if (component < 0 || 255 < component) {
126: throw new IllegalArgumentException(part + ": " + component);
127: }
128: }
130: private static int brighter(int n) {
131: return (int)Math.round(Math.sqrt(n * 255));
132: }
134: private static int darker(int n) {
135: return 255 - brighter(255 - n);
136: }
138: }