16 public final double red;
24 private String m_name;
41 this.red = roundAndClamp(
red);
42 this.green = roundAndClamp(
green);
43 this.blue = roundAndClamp(
blue);
64 this(color.red / 255.0, color.green / 255.0, color.blue / 255.0);
75 this.red = roundAndClamp(
red);
76 this.green = roundAndClamp(
green);
77 this.blue = roundAndClamp(
blue);
90 if (str ==
null || str.isBlank()) {
91 throw new IllegalArgumentException(
"Invalid color string \"" + str +
"\"");
95 if (str.charAt(0) ==
'#') {
96 if (str.length() != 7) {
97 throw new IllegalArgumentException(
"Invalid hex string \"" + str +
"\"");
100 Integer.parseInt(str.substring(1, 3), 16),
101 Integer.parseInt(str.substring(3, 5), 16),
102 Integer.parseInt(str.substring(5, 7), 16));
106 if (str.startsWith(
"rgb(") && str.endsWith(
")")) {
107 String[] components = str.substring(4, str.length() - 1).split(
",");
108 if (components.length != 3) {
109 throw new IllegalArgumentException(
"Invalid RGB string \"" + str +
"\"");
113 Integer.parseInt(components[0].trim()),
114 Integer.parseInt(components[1].trim()),
115 Integer.parseInt(components[2].trim()));
116 }
catch (NumberFormatException e) {
117 throw new IllegalArgumentException(
"Invalid RGB string \"" + str +
"\"");
123 for (var field :
Color.class.getFields()) {
124 if (java.lang.reflect.Modifier.isStatic(field.getModifiers())
125 &&
Color.class.isAssignableFrom(field.getType())
126 && field.getName().equalsIgnoreCase(str)) {
128 return (
Color) field.get(
null);
129 }
catch (IllegalAccessException e) {
135 throw new IllegalArgumentException(
"Invalid color string \"" + str +
"\"");
155 public boolean equals(Object other) {
163 return other instanceof
Color color
164 && Double.compare(color.red,
red) == 0
165 && Double.compare(color.green,
green) == 0
166 && Double.compare(color.blue,
blue) == 0;
170 public int hashCode() {
175 public String toString() {
176 if (m_name ==
null) {
189 return String.format(
190 "#%02X%02X%02X", (
int) (
red * 255), (
int) (
green * 255), (
int) (
blue * 255));
193 private static double roundAndClamp(
double value) {
194 return Math.clamp(Math.ceil(value * (1 << 12)) / (1 << 12), 0.0, 1.0);
197 private static int lerp(
int a,
int b,
double t) {
198 return (
int) (a + (b - a) * t);
220 final int chroma = (s * v) / 255;
223 final int region = (h / 30) % 6;
226 final int remainder = (int) Math.round((h % 30) * (255 / 30.0));
229 final int m = v - chroma;
232 final int X = (chroma * remainder) >> 8;
292 public static int packRGB(
int r,
int g,
int b) {
293 return (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF);
306 return switch (channel) {
307 case RED -> (packedColor >> 16) & 0xFF;
308 case GREEN -> (packedColor >> 8) & 0xFF;
309 case BLUE -> packedColor & 0xFF;
346 double r1,
double g1,
double b1,
double r2,
double g2,
double b2,
double t) {
372 public static int lerpRGB(
int r1,
int g1,
int b1,
int r2,
int g2,
int b2,
double t) {
373 return packRGB(lerp(r1, r2, t), lerp(g1, g2, t), lerp(b1, b2, t));
381 public static final Color DENIM =
new Color(0.0823529412, 0.376470589, 0.7411764706,
"DENIM");
388 new Color(0.9294117648, 0.1098039216, 0.1411764706,
"FIRST_RED");
399 new Color(0.98039216f, 0.92156863f, 0.84313726f,
"ANTIQUE_WHITE");
411 public static final Color BEIGE =
new Color(0.9607843f, 0.9607843f, 0.8627451f,
"BEIGE");
421 new Color(1.0f, 0.92156863f, 0.8039216f,
"BLANCHED_ALMOND");
428 new Color(0.5411765f, 0.16862746f, 0.8862745f,
"BLUE_VIOLET");
431 public static final Color BROWN =
new Color(0.64705884f, 0.16470589f, 0.16470589f,
"BROWN");
435 new Color(0.87058824f, 0.72156864f, 0.5294118f,
"BURLYWOOD");
439 new Color(0.37254903f, 0.61960787f, 0.627451f,
"CADET_BLUE");
452 new Color(0.39215687f, 0.58431375f, 0.92941177f,
"CORNFLOWER_BLUE");
458 public static final Color CRIMSON =
new Color(0.8627451f, 0.078431375f, 0.23529412f,
"CRIMSON");
471 new Color(0.72156864f, 0.5254902f, 0.043137256f,
"DARK_GOLDENROD");
481 new Color(0.7411765f, 0.7176471f, 0.41960785f,
"DARK_KHAKI");
485 new Color(0.54509807f, 0.0f, 0.54509807f,
"DARK_MAGENTA");
489 new Color(0.33333334f, 0.41960785f, 0.18431373f,
"DARK_OLIVE_GREEN");
502 new Color(0.9137255f, 0.5882353f, 0.47843137f,
"DARK_SALMON");
506 new Color(0.56078434f, 0.7372549f, 0.56078434f,
"DARK_SEA_GREEN");
510 new Color(0.28235295f, 0.23921569f, 0.54509807f,
"DARK_SLATE_BLUE");
514 new Color(0.18431373f, 0.30980393f, 0.30980393f,
"DARK_SLATE_GRAY");
518 new Color(0.0f, 0.80784315f, 0.81960785f,
"DARK_TURQUOISE");
537 new Color(0.69803923f, 0.13333334f, 0.13333334f,
"FIREBRICK");
544 new Color(0.13333334f, 0.54509807f, 0.13333334f,
"FOREST_GREEN");
560 new Color(0.85490197f, 0.64705884f, 0.1254902f,
"GOLDENROD");
563 public static final Color GRAY =
new Color(0.5019608f, 0.5019608f, 0.5019608f,
"GRAY");
579 new Color(0.8039216f, 0.36078432f, 0.36078432f,
"INDIAN_RED");
588 public static final Color KHAKI =
new Color(0.9411765f, 0.9019608f, 0.54901963f,
"KHAKI");
595 new Color(1.0f, 0.9411765f, 0.9607843f,
"LAVENDER_BLUSH");
602 new Color(1.0f, 0.98039216f, 0.8039216f,
"LEMON_CHIFFON");
606 new Color(0.6784314f, 0.84705883f, 0.9019608f,
"LIGHT_BLUE");
610 new Color(0.9411765f, 0.5019608f, 0.5019608f,
"LIGHT_CORAL");
617 new Color(0.98039216f, 0.98039216f, 0.8235294f,
"LIGHT_GOLDENROD_YELLOW");
624 new Color(0.5647059f, 0.93333334f, 0.5647059f,
"LIGHT_GREEN");
634 new Color(0.1254902f, 0.69803923f, 0.6666667f,
"LIGHT_SEA_GREEN");
638 new Color(0.5294118f, 0.80784315f, 0.98039216f,
"LIGHT_SKY_BLUE");
642 new Color(0.46666667f, 0.53333336f, 0.6f,
"LIGHT_SLATE_GRAY");
646 new Color(0.6901961f, 0.76862746f, 0.87058824f,
"LIGHT_STEEL_BLUE");
656 new Color(0.19607843f, 0.8039216f, 0.19607843f,
"LIME_GREEN");
659 public static final Color LINEN =
new Color(0.98039216f, 0.9411765f, 0.9019608f,
"LINEN");
669 new Color(0.4f, 0.8039216f, 0.6666667f,
"MEDIUM_AQUAMARINE");
676 new Color(0.7294118f, 0.33333334f, 0.827451f,
"MEDIUM_ORCHID");
680 new Color(0.5764706f, 0.4392157f, 0.85882354f,
"MEDIUM_PURPLE");
684 new Color(0.23529412f, 0.7019608f, 0.44313726f,
"MEDIUM_SEA_GREEN");
688 new Color(0.48235294f, 0.40784314f, 0.93333334f,
"MEDIUM_SLATE_BLUE");
692 new Color(0.0f, 0.98039216f, 0.6039216f,
"MEDIUM_SPRING_GREEN");
696 new Color(0.28235295f, 0.81960785f, 0.8f,
"MEDIUM_TURQUOISE");
700 new Color(0.78039217f, 0.08235294f, 0.52156866f,
"MEDIUM_VIOLET_RED");
704 new Color(0.09803922f, 0.09803922f, 0.4392157f,
"MIDNIGHT_BLUE");
729 new Color(0.41960785f, 0.5568628f, 0.13725491f,
"OLIVE_DRAB");
738 public static final Color ORCHID =
new Color(0.85490197f, 0.4392157f, 0.8392157f,
"ORCHID");
742 new Color(0.93333334f, 0.9098039f, 0.6666667f,
"PALE_GOLDENROD");
746 new Color(0.59607846f, 0.9843137f, 0.59607846f,
"PALE_GREEN");
750 new Color(0.6862745f, 0.93333334f, 0.93333334f,
"PALE_TURQUOISE");
754 new Color(0.85882354f, 0.4392157f, 0.5764706f,
"PALE_VIOLET_RED");
763 public static final Color PERU =
new Color(0.8039216f, 0.52156866f, 0.24705882f,
"PERU");
766 public static final Color PINK =
new Color(1.0f, 0.7529412f, 0.79607844f,
"PINK");
769 public static final Color PLUM =
new Color(0.8666667f, 0.627451f, 0.8666667f,
"PLUM");
773 new Color(0.6901961f, 0.8784314f, 0.9019608f,
"POWDER_BLUE");
783 new Color(0.7372549f, 0.56078434f, 0.56078434f,
"ROSY_BROWN");
787 new Color(0.25490198f, 0.4117647f, 0.88235295f,
"ROYAL_BLUE");
791 new Color(0.54509807f, 0.27058825f, 0.07450981f,
"SADDLE_BROWN");
794 public static final Color SALMON =
new Color(0.98039216f, 0.5019608f, 0.44705883f,
"SALMON");
798 new Color(0.95686275f, 0.6431373f, 0.3764706f,
"SANDY_BROWN");
802 new Color(0.18039216f, 0.54509807f, 0.34117648f,
"SEA_GREEN");
808 public static final Color SIENNA =
new Color(0.627451f, 0.32156864f, 0.1764706f,
"SIENNA");
811 public static final Color SILVER =
new Color(0.7529412f, 0.7529412f, 0.7529412f,
"SILVER");
818 new Color(0.41568628f, 0.3529412f, 0.8039216f,
"SLATE_BLUE");
822 new Color(0.4392157f, 0.5019608f, 0.5647059f,
"SLATE_GRAY");
825 public static final Color SNOW =
new Color(1.0f, 0.98039216f, 0.98039216f,
"SNOW");
832 new Color(0.27450982f, 0.50980395f, 0.7058824f,
"STEEL_BLUE");
835 public static final Color TAN =
new Color(0.8235294f, 0.7058824f, 0.54901963f,
"TAN");
838 public static final Color TEAL =
new Color(0.0f, 0.5019608f, 0.5019608f,
"TEAL");
850 public static final Color VIOLET =
new Color(0.93333334f, 0.50980395f, 0.93333334f,
"VIOLET");
853 public static final Color WHEAT =
new Color(0.9607843f, 0.87058824f, 0.7019608f,
"WHEAT");
860 new Color(0.9607843f, 0.9607843f, 0.9607843f,
"WHITE_SMOKE");
867 new Color(0.6039216f, 0.8039216f, 0.19607843f,
"YELLOW_GREEN");