Skip to content

Commit 0456227

Browse files
committed
Implements ColorHSL and ColorHSB classes for clarity and extensibility
- This is instead of just passing around an array of numbers
1 parent 935df98 commit 0456227

File tree

3 files changed

+378
-171
lines changed

3 files changed

+378
-171
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package ch.njol.skript.util;
2+
3+
import ch.njol.util.Math2;
4+
import org.bukkit.DyeColor;
5+
import org.jetbrains.annotations.ApiStatus;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
import org.skriptlang.skript.common.colors.ColorUtils;
9+
10+
/**
11+
/**
12+
* Immutable representation of a color in the HSB/HSV color-space, with an alpha channel.
13+
* Hue, saturation, and brightness are stored in normalized form: 0-1.
14+
* Alpha is stored as a value from 0-255 (like the red, green, and blue channels).
15+
*/
16+
public final class ColorHSB implements Color {
17+
18+
private final float hue;
19+
private final float saturation;
20+
private final float brightness;
21+
private final int alpha;
22+
23+
private final ColorRGB rgb;
24+
private final @Nullable DyeColor dye;
25+
26+
private ColorHSB(float hue, float saturation, float brightness, int alpha) {
27+
this.hue = hue;
28+
this.saturation = saturation;
29+
this.brightness = brightness;
30+
this.alpha = alpha;
31+
32+
this.rgb = ColorUtils.hsbToRgb(this);
33+
this.dye = rgb.asDyeColor();
34+
}
35+
36+
public static @NotNull ColorHSB fromHSBA(float hue, float saturation, float brightness, int alpha) {
37+
return new ColorHSB(
38+
Math2.fit(0f, hue, 1f),
39+
Math2.fit(0f, saturation, 1f),
40+
Math2.fit(0f, brightness, 1f),
41+
Math2.fit(0, alpha, 255)
42+
);
43+
}
44+
45+
public static @NotNull ColorHSB fromHSB(float hue, float saturation, float brightness) {
46+
return fromHSBA(hue, saturation, brightness, 255);
47+
}
48+
49+
@ApiStatus.Internal
50+
public static @NotNull ColorHSB fromUncheckedHSBA(float hue, float saturation, float brightness, int alpha) {
51+
return new ColorHSB(hue, saturation, brightness, alpha);
52+
}
53+
54+
public float getHue() {
55+
return hue;
56+
}
57+
58+
public float getSaturation() {
59+
return saturation;
60+
}
61+
62+
public float getBrightness() {
63+
return brightness;
64+
}
65+
66+
@Override
67+
public org.bukkit.Color asBukkitColor() {
68+
return rgb.asBukkitColor();
69+
}
70+
71+
@Override
72+
public int getAlpha() {
73+
return alpha;
74+
}
75+
76+
@Override
77+
public int getRed() {
78+
return rgb.getRed();
79+
}
80+
81+
@Override
82+
public int getGreen() {
83+
return rgb.getGreen();
84+
}
85+
86+
@Override
87+
public int getBlue() {
88+
return rgb.getBlue();
89+
}
90+
91+
@Override
92+
public @Nullable DyeColor asDyeColor() {
93+
return dye;
94+
}
95+
96+
@Override
97+
public String getName() {
98+
String hsb = String.format("%.3f, %.3f, %.3f", hue, saturation, brightness);
99+
return alpha == 255 ? "hsb " + hsb : "hsba " + hsb + ", " + alpha;
100+
}
101+
102+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package ch.njol.skript.util;
2+
3+
import ch.njol.util.Math2;
4+
import org.bukkit.DyeColor;
5+
import org.jetbrains.annotations.ApiStatus;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
import org.skriptlang.skript.common.colors.ColorUtils;
9+
10+
/**
11+
* Immutable representation of a color in the HSL color-space, with an alpha channel.
12+
* Hue, saturation, and lightness are stored in normalized form: 0-1.
13+
* Alpha is stored as a value from 0-255 (like the red, green, and blue channels).
14+
*/
15+
public class ColorHSL implements Color {
16+
17+
private final float hue;
18+
private final float saturation;
19+
private final float lightness;
20+
private final int alpha;
21+
22+
private final ColorRGB rgb;
23+
private final @Nullable DyeColor dye;
24+
25+
private ColorHSL(float hue, float saturation, float lightness, int alpha) {
26+
this.hue = hue;
27+
this.saturation = saturation;
28+
this.lightness = lightness;
29+
this.alpha = alpha;
30+
31+
this.rgb = ColorUtils.hslToRgb(this);
32+
this.dye = rgb.asDyeColor();
33+
}
34+
35+
public static @NotNull ColorHSL fromHSLA(float hue, float saturation, float lightness, int alpha) {
36+
return new ColorHSL(
37+
Math2.fit(0f, hue, 1f),
38+
Math2.fit(0f, saturation, 1f),
39+
Math2.fit(0f, lightness, 1f),
40+
Math2.fit(0, alpha, 255)
41+
);
42+
}
43+
44+
public static @NotNull ColorHSL fromHSL(float hue, float saturation, float lightness) {
45+
return fromHSLA(hue, saturation, lightness, 255);
46+
}
47+
48+
@ApiStatus.Internal
49+
public static @NotNull ColorHSL fromUncheckedHSLA(float hue, float saturation, float lightness, int alpha) {
50+
return new ColorHSL(hue, saturation, lightness, alpha);
51+
}
52+
53+
public float getHue() {
54+
return hue;
55+
}
56+
57+
public float getSaturation() {
58+
return saturation;
59+
}
60+
61+
public float getLightness() {
62+
return lightness;
63+
}
64+
65+
@Override
66+
public org.bukkit.Color asBukkitColor() {
67+
return rgb.asBukkitColor();
68+
}
69+
70+
@Override
71+
public int getAlpha() {
72+
return alpha;
73+
}
74+
75+
@Override
76+
public int getRed() {
77+
return rgb.getRed();
78+
}
79+
80+
@Override
81+
public int getGreen() {
82+
return rgb.getGreen();
83+
}
84+
85+
@Override
86+
public int getBlue() {
87+
return rgb.getBlue();
88+
}
89+
90+
@Override
91+
public @Nullable DyeColor asDyeColor() {
92+
return dye;
93+
}
94+
95+
@Override
96+
public String getName() {
97+
String hslString = String.format("%.3f, %.3f, %.3f", hue, saturation, lightness);
98+
if (alpha != 255)
99+
return "hsla " + hslString + ", " + alpha;
100+
return "hsl " + hslString;
101+
}
102+
103+
}

0 commit comments

Comments
 (0)