Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.preferences / src / net / mograsim / preferences / ColorDefinition.java
1 package net.mograsim.preferences;
2
3 /**
4  * A way to define a color with the possibility to use colors built into the system (called "system colors" in SWT).
5  * <p>
6  * A {@link ColorDefinition} is defined either by a {@link BuiltInColor} constant, in which case <code>r==g==b==-1</code>, or by red / green
7  * / blue components, in which case <code>builtInColor==null</code>
8  * 
9  * @author Daniel Kirschten
10  */
11 public class ColorDefinition
12 {
13         /**
14          * The built-in color constant defining this color.
15          */
16         public final ColorDefinition.BuiltInColor builtInColor;
17         /**
18          * The red color component defining this color.
19          */
20         public final int r;
21         /**
22          * The green color component defining this color.
23          */
24         public final int g;
25         /**
26          * The blue color component defining this color.
27          */
28         public final int b;
29
30         public ColorDefinition(ColorDefinition.BuiltInColor col)
31         {
32                 if (col == null)
33                         throw new IllegalArgumentException("Illegal built-in color: " + col);
34                 this.builtInColor = col;
35                 this.r = -1;
36                 this.g = -1;
37                 this.b = -1;
38         }
39
40         public ColorDefinition(int r, int g, int b)
41         {
42                 if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
43                         throw new IllegalArgumentException("Illegal color components: r=" + r + "; g=" + g + "; b=" + b);
44                 this.builtInColor = null;
45                 this.r = r;
46                 this.g = g;
47                 this.b = b;
48         }
49
50         @Override
51         public int hashCode()
52         {
53                 final int prime = 31;
54                 int result = 1;
55                 result = prime * result + b;
56                 result = prime * result + ((builtInColor == null) ? 0 : builtInColor.hashCode());
57                 result = prime * result + g;
58                 result = prime * result + r;
59                 return result;
60         }
61
62         @Override
63         public boolean equals(Object obj)
64         {
65                 if (this == obj)
66                         return true;
67                 if (obj == null)
68                         return false;
69                 if (getClass() != obj.getClass())
70                         return false;
71                 ColorDefinition other = (ColorDefinition) obj;
72                 if (b != other.b)
73                         return false;
74                 if (builtInColor != other.builtInColor)
75                         return false;
76                 if (g != other.g)
77                         return false;
78                 if (r != other.r)
79                         return false;
80                 return true;
81         }
82
83         public static enum BuiltInColor
84         {
85                 COLOR_WHITE, COLOR_BLACK, COLOR_RED, COLOR_DARK_RED, COLOR_GREEN, COLOR_DARK_GREEN, COLOR_YELLOW, COLOR_DARK_YELLOW, COLOR_BLUE,
86                 COLOR_DARK_BLUE, COLOR_MAGENTA, COLOR_DARK_MAGENTA, COLOR_CYAN, COLOR_DARK_CYAN, COLOR_GRAY, COLOR_DARK_GRAY;
87         }
88
89 }