Changed license to Creative Commons Zero (CC0).
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / Color.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  *
5 *
6  */
7
8 package eu.svjatoslav.sixth.e3d.renderer.raster;
9
10 public final class Color {
11
12     public static final Color RED = new Color(255, 0, 0, 255);
13     public static final Color GREEN = new Color(0, 255, 0, 255);
14     public static final Color BLUE = new Color(0, 0, 255, 255);
15     public static final Color YELLOW = new Color(255, 255, 0, 255);
16     public static final Color WHITE = new Color(255, 255, 255, 255);
17     public static final Color BLACK = new Color(0, 0, 0, 255);
18     public static final Color PURPLE = new Color(255, 0, 255, 255);
19     public static final Color TRANSPARENT = new Color(0, 0, 0, 0);
20
21     public final int r, g, b;
22
23     /**
24      * <pre>
25      * 255 is opaque.
26      * 0 is transparent.
27      * </pre>
28      */
29     public int a;
30
31     public Color(final Color parentColor) {
32         r = parentColor.r;
33         g = parentColor.g;
34         b = parentColor.b;
35         a = parentColor.a;
36     }
37
38     public Color(final double r, final double g, final double b, final double a) {
39         this.r = ensureByteLimit((int) (r * 255d));
40         this.g = ensureByteLimit((int) (g * 255d));
41         this.b = ensureByteLimit((int) (b * 255d));
42         this.a = ensureByteLimit((int) (a * 255d));
43     }
44
45     /**
46      * <pre>
47      *     Supported formats are:
48      *
49      *     RGB
50      *     RGBA
51      *     RRGGBB
52      *     RRGGBBAA
53      * </pre>
54      */
55     public Color(String colorHexCode) {
56         switch (colorHexCode.length()) {
57             case 3:
58                 r = parseHexSegment(colorHexCode, 0, 1) * 16;
59                 g = parseHexSegment(colorHexCode, 1, 1) * 16;
60                 b = parseHexSegment(colorHexCode, 2, 1) * 16;
61                 a = 255;
62                 return;
63
64             case 4:
65                 r = parseHexSegment(colorHexCode, 0, 1) * 16;
66                 g = parseHexSegment(colorHexCode, 1, 1) * 16;
67                 b = parseHexSegment(colorHexCode, 2, 1) * 16;
68                 a = parseHexSegment(colorHexCode, 3, 1) * 16;
69                 return;
70
71             case 6:
72                 r = parseHexSegment(colorHexCode, 0, 2);
73                 g = parseHexSegment(colorHexCode, 2, 2);
74                 b = parseHexSegment(colorHexCode, 4, 2);
75                 a = 255;
76                 return;
77
78             case 8:
79                 r = parseHexSegment(colorHexCode, 0, 2);
80                 g = parseHexSegment(colorHexCode, 2, 2);
81                 b = parseHexSegment(colorHexCode, 4, 2);
82                 a = parseHexSegment(colorHexCode, 6, 2);
83                 return;
84             default:
85                 throw new IllegalArgumentException("Unsupported color code: " + colorHexCode);
86         }
87     }
88
89     public Color(final int rgb) {
90         r = (rgb & 0xFF0000) >> 16;
91         g = (rgb & 0xFF00) >> 8;
92         b = rgb & 0xFF;
93         a = 255;
94     }
95
96     public Color(final int r, final int g, final int b) {
97         this(r, g, b, 255);
98     }
99
100     public Color(final int r, final int g, final int b, final int a) {
101         this.r = ensureByteLimit(r);
102         this.g = ensureByteLimit(g);
103         this.b = ensureByteLimit(b);
104         this.a = ensureByteLimit(a);
105     }
106
107     private int parseHexSegment(String hexString, int start, int length) {
108         return Integer.parseInt(hexString.substring(start, start + length), 16);
109     }
110
111     /**
112      * Ensure that color values are within allowed limits of 0 to 255.
113      */
114     private int ensureByteLimit(final int value) {
115         if (value < 0)
116             return 0;
117
118         if (value > 255)
119             return 255;
120
121         return value;
122     }
123
124     public boolean isTransparent() {
125         return a == 0;
126     }
127
128     public java.awt.Color toAwtColor() {
129         return new java.awt.Color(r, g, b, a);
130     }
131
132     public int toInt() {
133         return toAwtColor().getRGB();
134     }
135
136     @Override
137     public boolean equals(Object o) {
138         if (this == o) return true;
139         if (o == null || getClass() != o.getClass()) return false;
140
141         Color color = (Color) o;
142
143         if (r != color.r) return false;
144         if (g != color.g) return false;
145         if (b != color.b) return false;
146         return a == color.a;
147     }
148
149     @Override
150     public int hashCode() {
151         int result = r;
152         result = 31 * result + g;
153         result = 31 * result + b;
154         result = 31 * result + a;
155         return result;
156     }
157 }