1
1
package org .openpatch .scratch .extensions .color ;
2
2
3
3
/**
4
- * The Color class represents a color in the Scratch environment. It supports various
5
- * functionalities such as setting color values in the RGB and HSB spectrum, changing the color
4
+ * The Color class represents a color in the Scratch environment. It supports
5
+ * various
6
+ * functionalities such as setting color values in the RGB and HSB spectrum,
7
+ * changing the color
6
8
* based on a hue value, and converting between RGB and HSB color codes.
7
9
*/
8
10
public class Color {
@@ -16,15 +18,17 @@ public class Color {
16
18
private double l = 255 ;
17
19
18
20
/** Constructs a new Color object with default values. */
19
- public Color () {}
21
+ public Color () {
22
+ }
20
23
21
24
public Color (String hexCode ) {
22
25
if (hexCode .startsWith ("#" )) {
23
26
hexCode = hexCode .substring (1 );
24
27
}
25
- this .r = Integer .valueOf (hexCode .substring (0 , 2 ), 16 );
26
- this .g = Integer .valueOf (hexCode .substring (2 , 4 ), 16 );
27
- this .b = Integer .valueOf (hexCode .substring (4 , 6 ), 16 );
28
+ var r = Integer .valueOf (hexCode .substring (0 , 2 ), 16 );
29
+ var g = Integer .valueOf (hexCode .substring (2 , 4 ), 16 );
30
+ var b = Integer .valueOf (hexCode .substring (4 , 6 ), 16 );
31
+ this .setRGB (r , g , b );
28
32
}
29
33
30
34
/**
@@ -62,6 +66,34 @@ public Color(Color c) {
62
66
this .l = c .l ;
63
67
}
64
68
69
+ /**
70
+ * Get the color value as a hex code. This is useful for comparing pixels.
71
+ *
72
+ * @see Stage#getBackgroundPixels()
73
+ *
74
+ * @return hex code
75
+ */
76
+ public int get () {
77
+ var v1 = this .r ;
78
+ var v2 = this .g ;
79
+ var v3 = this .b ;
80
+
81
+ if (v1 > 255 )
82
+ v1 = 255 ;
83
+ else if (v1 < 0 )
84
+ v1 = 0 ;
85
+ if (v2 > 255 )
86
+ v2 = 255 ;
87
+ else if (v2 < 0 )
88
+ v2 = 0 ;
89
+ if (v3 > 255 )
90
+ v3 = 255 ;
91
+ else if (v3 < 0 )
92
+ v3 = 0 ;
93
+
94
+ return 0xff000000 | ((int ) v1 << 16 ) | ((int ) v2 << 8 ) | (int ) v3 ;
95
+ }
96
+
65
97
/**
66
98
* Get the color value on the HSB spectrum.
67
99
*
@@ -72,7 +104,8 @@ public double getHSB() {
72
104
}
73
105
74
106
/**
75
- * Setting the color value after the HSB spectrum. Saturation and Luminosity are fixed at 255.
107
+ * Setting the color value after the HSB spectrum. Saturation and Luminosity are
108
+ * fixed at 255.
76
109
*
77
110
* @param h A hue value [0...255]
78
111
*/
@@ -132,13 +165,14 @@ public void setRGB(double r, double g, double b) {
132
165
}
133
166
134
167
/**
135
- * Changes the color accordining to a hue value, which is added to the current hue value. When the
168
+ * Changes the color accordining to a hue value, which is added to the current
169
+ * hue value. When the
136
170
* resulting value is greater than 255 it will be reset. For example: 285 => 30.
137
171
*
138
172
* @param h A hue value. Could be any positive or negative number.
139
173
*/
140
174
public void changeColor (double h ) {
141
- double newH = this .getHSB () + h ;
175
+ double newH = this .getH () + h ;
142
176
this .setHSB (newH );
143
177
}
144
178
@@ -151,10 +185,9 @@ public void changeColor(double h) {
151
185
* @return hsb values
152
186
*/
153
187
private static double [] RGBtoHSB (double r , double g , double b ) {
154
- var hsb =
155
- java .awt .Color .RGBtoHSB (
156
- Math .round ((float ) r ), Math .round ((float ) g ), Math .round ((float ) b ), null );
157
- double [] hsbd = {hsb [0 ], hsb [1 ], hsb [2 ]};
188
+ var hsb = java .awt .Color .RGBtoHSB (
189
+ Math .round ((float ) r ), Math .round ((float ) g ), Math .round ((float ) b ), null );
190
+ double [] hsbd = { hsb [0 ], hsb [1 ], hsb [2 ] };
158
191
return hsbd ;
159
192
}
160
193
@@ -167,8 +200,7 @@ private static double[] RGBtoHSB(double r, double g, double b) {
167
200
* @return rgb values
168
201
*/
169
202
private static double [] HSBtoRGB (double h , double s , double l ) {
170
- java .awt .Color colorRgb =
171
- new java .awt .Color (java .awt .Color .HSBtoRGB ((float ) h , (float ) s , (float ) l ));
203
+ java .awt .Color colorRgb = new java .awt .Color (java .awt .Color .HSBtoRGB ((float ) h , (float ) s , (float ) l ));
172
204
double [] rgb = new double [3 ];
173
205
rgb [0 ] = colorRgb .getRed ();
174
206
rgb [1 ] = colorRgb .getGreen ();
0 commit comments