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
5
- * various
6
- * functionalities such as setting color values in the RGB and HSB spectrum,
7
- * changing the color
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
8
6
* based on a hue value, and converting between RGB and HSB color codes.
9
7
*/
10
8
public class Color {
@@ -18,8 +16,7 @@ public class Color {
18
16
private double l = 255 ;
19
17
20
18
/** Constructs a new Color object with default values. */
21
- public Color () {
22
- }
19
+ public Color () {}
23
20
24
21
public Color (String hexCode ) {
25
22
if (hexCode .startsWith ("#" )) {
@@ -68,28 +65,21 @@ public Color(Color c) {
68
65
69
66
/**
70
67
* Get the color value as a hex code. This is useful for comparing pixels.
71
- *
68
+ *
72
69
* @see Stage#getBackgroundPixels()
73
- *
74
70
* @return hex code
75
71
*/
76
72
public int get () {
77
73
var v1 = this .r ;
78
74
var v2 = this .g ;
79
75
var v3 = this .b ;
80
76
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 ;
77
+ if (v1 > 255 ) v1 = 255 ;
78
+ else if (v1 < 0 ) v1 = 0 ;
79
+ if (v2 > 255 ) v2 = 255 ;
80
+ else if (v2 < 0 ) v2 = 0 ;
81
+ if (v3 > 255 ) v3 = 255 ;
82
+ else if (v3 < 0 ) v3 = 0 ;
93
83
94
84
return 0xff000000 | ((int ) v1 << 16 ) | ((int ) v2 << 8 ) | (int ) v3 ;
95
85
}
@@ -104,8 +94,7 @@ public double getHSB() {
104
94
}
105
95
106
96
/**
107
- * Setting the color value after the HSB spectrum. Saturation and Luminosity are
108
- * fixed at 255.
97
+ * Setting the color value after the HSB spectrum. Saturation and Luminosity are fixed at 255.
109
98
*
110
99
* @param h A hue value [0...255]
111
100
*/
@@ -165,8 +154,7 @@ public void setRGB(double r, double g, double b) {
165
154
}
166
155
167
156
/**
168
- * Changes the color accordining to a hue value, which is added to the current
169
- * hue value. When the
157
+ * Changes the color accordining to a hue value, which is added to the current hue value. When the
170
158
* resulting value is greater than 255 it will be reset. For example: 285 => 30.
171
159
*
172
160
* @param h A hue value. Could be any positive or negative number.
@@ -185,9 +173,10 @@ public void changeColor(double h) {
185
173
* @return hsb values
186
174
*/
187
175
private static double [] RGBtoHSB (double r , double g , double b ) {
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 ] };
176
+ var hsb =
177
+ java .awt .Color .RGBtoHSB (
178
+ Math .round ((float ) r ), Math .round ((float ) g ), Math .round ((float ) b ), null );
179
+ double [] hsbd = {hsb [0 ], hsb [1 ], hsb [2 ]};
191
180
return hsbd ;
192
181
}
193
182
@@ -200,7 +189,8 @@ private static double[] RGBtoHSB(double r, double g, double b) {
200
189
* @return rgb values
201
190
*/
202
191
private static double [] HSBtoRGB (double h , double s , double l ) {
203
- java .awt .Color colorRgb = new java .awt .Color (java .awt .Color .HSBtoRGB ((float ) h , (float ) s , (float ) l ));
192
+ java .awt .Color colorRgb =
193
+ new java .awt .Color (java .awt .Color .HSBtoRGB ((float ) h , (float ) s , (float ) l ));
204
194
double [] rgb = new double [3 ];
205
195
rgb [0 ] = colorRgb .getRed ();
206
196
rgb [1 ] = colorRgb .getGreen ();
0 commit comments