2 * Sixth 3D engine. Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 3 of the GNU Lesser General Public License
6 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.sixth.e3d.renderer.raster;
12 public final class Color {
14 public static final Color RED = new Color(255, 0, 0, 255);
15 public static final Color GREEN = new Color(0, 255, 0, 255);
16 public static final Color BLUE = new Color(0, 0, 255, 255);
17 public static final Color YELLOW = new Color(255, 255, 0, 255);
18 public static final Color WHITE = new Color(255, 255, 255, 255);
19 public static final Color BLACK = new Color(0, 0, 0, 255);
20 public static final Color PURPLE = new Color(255, 0, 255, 255);
21 public static final Color TRANSPARENT = new Color(0, 0, 0, 0);
23 public final int r, g, b;
33 public Color(final Color parentColor) {
40 public Color(final double r, final double g, final double b, final double a) {
41 this.r = ensureByteLimit((int) (r * 255d));
42 this.g = ensureByteLimit((int) (g * 255d));
43 this.b = ensureByteLimit((int) (b * 255d));
44 this.a = ensureByteLimit((int) (a * 255d));
49 * Supported formats are:
57 public Color(String colorHexCode) {
58 switch (colorHexCode.length()) {
60 r = parseHexSegment(colorHexCode, 0, 1) * 16;
61 g = parseHexSegment(colorHexCode, 1, 1) * 16;
62 b = parseHexSegment(colorHexCode, 2, 1) * 16;
67 r = parseHexSegment(colorHexCode, 0, 1) * 16;
68 g = parseHexSegment(colorHexCode, 1, 1) * 16;
69 b = parseHexSegment(colorHexCode, 2, 1) * 16;
70 a = parseHexSegment(colorHexCode, 3, 1) * 16;
74 r = parseHexSegment(colorHexCode, 0, 2);
75 g = parseHexSegment(colorHexCode, 2, 2);
76 b = parseHexSegment(colorHexCode, 4, 2);
81 r = parseHexSegment(colorHexCode, 0, 2);
82 g = parseHexSegment(colorHexCode, 2, 2);
83 b = parseHexSegment(colorHexCode, 4, 2);
84 a = parseHexSegment(colorHexCode, 6, 2);
87 throw new IllegalArgumentException("Unsupported color code: " + colorHexCode);
91 public Color(final int rgb) {
92 r = (rgb & 0xFF0000) >> 16;
93 g = (rgb & 0xFF00) >> 8;
98 public Color(final int r, final int g, final int b) {
102 public Color(final int r, final int g, final int b, final int a) {
103 this.r = ensureByteLimit(r);
104 this.g = ensureByteLimit(g);
105 this.b = ensureByteLimit(b);
106 this.a = ensureByteLimit(a);
109 private int parseHexSegment(String hexString, int start, int length) {
110 return Integer.parseInt(hexString.substring(start, start + length), 16);
114 * Ensure that color values are within allowed limits of 0 to 255.
116 private int ensureByteLimit(final int value) {
126 public boolean isTransparent() {
130 public java.awt.Color toAwtColor() {
131 return new java.awt.Color(r, g, b, a);
135 return toAwtColor().getRGB();
139 public boolean equals(Object o) {
140 if (this == o) return true;
141 if (o == null || getClass() != o.getClass()) return false;
143 Color color = (Color) o;
145 if (r != color.r) return false;
146 if (g != color.g) return false;
147 if (b != color.b) return false;
152 public int hashCode() {
154 result = 31 * result + g;
155 result = 31 * result + b;
156 result = 31 * result + a;