Skip to content

Commit 68f72a1

Browse files
committed
add get method to color
1 parent 0b6d8a3 commit 68f72a1

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ lang: en
77
## 4.15.0
88

99
- 🚀 Feat: All methods return a color now return a Color object.
10+
- 🚀 Feat: Add `get`-method to color object for comparing pixels.
1011

1112
## 4.14.0
1213

src/org/openpatch/scratch/extensions/color/Color.java

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.openpatch.scratch.extensions.color;
22

33
/**
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
68
* based on a hue value, and converting between RGB and HSB color codes.
79
*/
810
public class Color {
@@ -16,15 +18,17 @@ public class Color {
1618
private double l = 255;
1719

1820
/** Constructs a new Color object with default values. */
19-
public Color() {}
21+
public Color() {
22+
}
2023

2124
public Color(String hexCode) {
2225
if (hexCode.startsWith("#")) {
2326
hexCode = hexCode.substring(1);
2427
}
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);
2832
}
2933

3034
/**
@@ -62,6 +66,34 @@ public Color(Color c) {
6266
this.l = c.l;
6367
}
6468

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+
6597
/**
6698
* Get the color value on the HSB spectrum.
6799
*
@@ -72,7 +104,8 @@ public double getHSB() {
72104
}
73105

74106
/**
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.
76109
*
77110
* @param h A hue value [0...255]
78111
*/
@@ -132,13 +165,14 @@ public void setRGB(double r, double g, double b) {
132165
}
133166

134167
/**
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
136170
* resulting value is greater than 255 it will be reset. For example: 285 => 30.
137171
*
138172
* @param h A hue value. Could be any positive or negative number.
139173
*/
140174
public void changeColor(double h) {
141-
double newH = this.getHSB() + h;
175+
double newH = this.getH() + h;
142176
this.setHSB(newH);
143177
}
144178

@@ -151,10 +185,9 @@ public void changeColor(double h) {
151185
* @return hsb values
152186
*/
153187
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] };
158191
return hsbd;
159192
}
160193

@@ -167,8 +200,7 @@ private static double[] RGBtoHSB(double r, double g, double b) {
167200
* @return rgb values
168201
*/
169202
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));
172204
double[] rgb = new double[3];
173205
rgb[0] = colorRgb.getRed();
174206
rgb[1] = colorRgb.getGreen();

0 commit comments

Comments
 (0)