44import java .util .Objects ;
55
66public class Color {
7- public final int id ;
8-
9- public Color (final int r , final int g , final int b ) {
10- id = getRGBIndex (r , g , b );
11- }
12-
13- public Color (final int id ) {
14- this .id = id ;
15- }
16-
177 public final static Color Red = new Color (111 );
18-
198 public final static Color Blue = new Color (165 );
20-
219 public final static Color Teal = new Color (159 );
22-
2310 public final static Color Purple = new Color (164 );
24-
2511 public final static Color Orange = new Color (179 );
26-
2712 public final static Color Brown = new Color (19 );
28-
2913 public final static Color White = new Color (255 );
30-
3114 public final static Color Yellow = new Color (135 );
32-
3315 public final static Color Green = new Color (117 );
34-
3516 public final static Color Cyan = new Color (128 );
36-
3717 public final static Color Black = new Color (0 );
38-
3918 public final static Color Grey = new Color (74 );
40-
41- int red () {
42- return id < 256 ? defaultPalette [id ].rgbRed : 0 ;
43- }
44- int green () {
45- return id < 256 ? defaultPalette [id ].rgbGreen : 0 ;
46- }
47- int blue () {
48- return id < 256 ? defaultPalette [id ].rgbBlue : 0 ;
49- }
50-
51- @ Override
52- public boolean equals (Object o ) {
53- if (!(o instanceof Color )) {
54- return false ;
55- }
56- return id == ((Color ) o ).id ;
57- }
58-
59- @ Override
60- public int hashCode () {
61- return Objects .hash (id );
62- }
63-
64- /// BROODWAR COLOR IMPLEMENTATION
65- private static class RGBQUAD {
66- byte rgbRed ;
67- byte rgbGreen ;
68- byte rgbBlue ;
69- byte rgbReserved ;
70-
71- RGBQUAD (int rgbRed , int rgbGreen , int rgbBlue ) {
72- this (rgbRed ,rgbGreen , rgbBlue , 0 );
73- }
74-
75- RGBQUAD (int rgbRed , int rgbGreen , int rgbBlue , int rgbReserved ) {
76- this .rgbRed = (byte )rgbRed ;
77- this .rgbGreen = (byte )rgbGreen ;
78- this .rgbBlue = (byte )rgbBlue ;
79- this .rgbReserved = (byte )rgbReserved ;
80- }
81- }
82-
83- private static RGBQUAD RGBRESERVE = new RGBQUAD (0 ,0 ,0 ,0xFF );
84-
85- private static boolean rgbInitialized = false ;
19+ private static RGBQUAD RGBRESERVE = new RGBQUAD (0 , 0 , 0 , 0xFF );
8620 private static final RGBQUAD defaultPalette [] = {
8721 new RGBQUAD (0 , 0 , 0 ), RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE ,
8822 RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE ,
@@ -117,12 +51,23 @@ private static class RGBQUAD {
11751 RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE ,
11852 RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , RGBRESERVE , new RGBQUAD (255 , 255 , 255 )
11953 };
54+ private static boolean rgbInitialized = false ;
55+ private static byte closestColor [][][] = new byte [64 ][64 ][64 ];
56+ public final int id ;
57+
58+ public Color (final int r , final int g , final int b ) {
59+ id = getRGBIndex (r , g , b );
60+ }
61+
62+ public Color (final int id ) {
63+ this .id = id ;
64+ }
12065
12166 private static int getBestIdFor (int red , int green , int blue ) {
12267
123- int min_dist = 3 * 256 * 256 ;
124- int best_id = 0 ;
125- for ( int id = 0 ; id < 255 ; ++id ) {
68+ int min_dist = 3 * 256 * 256 ;
69+ int best_id = 0 ;
70+ for ( int id = 0 ; id < 255 ; ++id ) {
12671 RGBQUAD p = defaultPalette [id ];
12772 if (p .rgbReserved != 0 ) {
12873 continue ;
@@ -131,8 +76,8 @@ private static int getBestIdFor(int red, int green, int blue) {
13176 int g = green - p .rgbGreen ;
13277 int b = blue - p .rgbBlue ;
13378
134- int distance = r * r + g * g + b * b ;
135- if ( distance < min_dist ) {
79+ int distance = r * r + g * g + b * b ;
80+ if (distance < min_dist ) {
13681 min_dist = distance ;
13782 best_id = id ;
13883 if (distance == 0 ) {
@@ -143,19 +88,61 @@ private static int getBestIdFor(int red, int green, int blue) {
14388 return best_id ;
14489 }
14590
146- private static byte closestColor [][][] = new byte [64 ][64 ][64 ];
147-
14891 private static int getRGBIndex (int red , int green , int blue ) {
149- if ( !rgbInitialized ) {
92+ if (!rgbInitialized ) {
15093 rgbInitialized = true ;
151- for (int r = 0 ; r < 64 ; ++r ) {
152- for (int g = 0 ; g < 64 ; ++g ) {
153- for (int b = 0 ; b < 64 ; ++b ) {
94+ for (int r = 0 ; r < 64 ; ++r ) {
95+ for (int g = 0 ; g < 64 ; ++g ) {
96+ for (int b = 0 ; b < 64 ; ++b ) {
15497 closestColor [r ][g ][b ] = (byte ) getBestIdFor (r << 2 , g << 2 , b << 2 );
15598 }
15699 }
157100 }
158101 }
159- return closestColor [(byte )(red >> 2 )][(byte )(green >> 2 )][(byte )(blue >> 2 )];
102+ return closestColor [(byte ) (red >> 2 )][(byte ) (green >> 2 )][(byte ) (blue >> 2 )];
103+ }
104+
105+ int red () {
106+ return id < 256 ? defaultPalette [id ].rgbRed : 0 ;
107+ }
108+
109+ int green () {
110+ return id < 256 ? defaultPalette [id ].rgbGreen : 0 ;
111+ }
112+
113+ int blue () {
114+ return id < 256 ? defaultPalette [id ].rgbBlue : 0 ;
115+ }
116+
117+ @ Override
118+ public boolean equals (Object o ) {
119+ if (!(o instanceof Color )) {
120+ return false ;
121+ }
122+ return id == ((Color ) o ).id ;
123+ }
124+
125+ @ Override
126+ public int hashCode () {
127+ return Objects .hash (id );
128+ }
129+
130+ /// BROODWAR COLOR IMPLEMENTATION
131+ private static class RGBQUAD {
132+ byte rgbRed ;
133+ byte rgbGreen ;
134+ byte rgbBlue ;
135+ byte rgbReserved ;
136+
137+ RGBQUAD (int rgbRed , int rgbGreen , int rgbBlue ) {
138+ this (rgbRed , rgbGreen , rgbBlue , 0 );
139+ }
140+
141+ RGBQUAD (int rgbRed , int rgbGreen , int rgbBlue , int rgbReserved ) {
142+ this .rgbRed = (byte ) rgbRed ;
143+ this .rgbGreen = (byte ) rgbGreen ;
144+ this .rgbBlue = (byte ) rgbBlue ;
145+ this .rgbReserved = (byte ) rgbReserved ;
146+ }
160147 }
161148}
0 commit comments