From 401baafdc0dbb1db709b2ec5aee68b24275f1b6b Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sat, 7 Dec 2024 17:58:47 +1000 Subject: [PATCH 01/39] Still a WIP, implements some functions and expressions --- src/main/java/ch/njol/skript/Skript.java | 2 + .../skript/misc/colours/ColourModule.java | 110 +++++++++ .../skript/misc/colours/ColourUtils.java | 214 ++++++++++++++++++ .../skript/misc/colours/ExprBlend.java | 61 +++++ .../skript/misc/colours/ExprComplement.java | 28 +++ 5 files changed, 415 insertions(+) create mode 100644 src/main/java/org/skriptlang/skript/misc/colours/ColourModule.java create mode 100644 src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java create mode 100644 src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java create mode 100644 src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index 4b9418c5636..da5f335aff4 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -106,6 +106,7 @@ import org.skriptlang.skript.lang.script.Script; import org.skriptlang.skript.lang.structure.Structure; import org.skriptlang.skript.lang.structure.StructureInfo; +import org.skriptlang.skript.misc.colours.ColourModule; import java.io.File; import java.io.IOException; @@ -537,6 +538,7 @@ public void onEnable() { BreedingModule.load(); DisplayModule.load(); InputModule.load(); + ColourModule.load(); } catch (final Exception e) { exception(e, "Could not load required .class files: " + e.getLocalizedMessage()); setEnabled(false); diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ColourModule.java b/src/main/java/org/skriptlang/skript/misc/colours/ColourModule.java new file mode 100644 index 00000000000..05a794c9e18 --- /dev/null +++ b/src/main/java/org/skriptlang/skript/misc/colours/ColourModule.java @@ -0,0 +1,110 @@ +package org.skriptlang.skript.misc.colours; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.function.Functions; +import ch.njol.skript.lang.function.Parameter; +import ch.njol.skript.lang.function.SimpleJavaFunction; +import ch.njol.skript.lang.util.SimpleLiteral; +import ch.njol.skript.registrations.DefaultClasses; +import ch.njol.skript.util.Color; +import ch.njol.skript.util.ColorRGB; +import ch.njol.util.coll.CollectionUtils; + +import java.io.IOException; + +public class ColourModule { + + public static void load() throws IOException { + Skript.getAddonInstance().loadClasses("org.skriptlang.skript.misc", "colours"); + + Functions.registerFunction(new SimpleJavaFunction("shade", new Parameter[] { + new Parameter<>("colour", DefaultClasses.COLOR, true, null), + new Parameter<>("amount", DefaultClasses.LONG, true, new SimpleLiteral<>(1L, true)), + new Parameter<>("hsl", DefaultClasses.BOOLEAN, true, new SimpleLiteral<>(false, true)) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color colour = (Color) params[0][0]; + Long amount = (Long) params[1][0]; + boolean hsl = (Boolean) params[2][0]; + return CollectionUtils.array(hsl + ? ColourUtils.shadeColorHSL(colour, amount.intValue()) + : ColourUtils.shadeColor(colour, amount.intValue())); + } + }).description( + "Shades a given colour by a given amount. Optionally can use HSL methods to achieve this instead.", + "The amount should be from 0-100, with 0 doing nothing, and 100 shading it all the way to black." + ).examples( + "set {_darkRed} to shade(red, 10)", + "set {_darkerRed} to shade(red, 20)" + ).since("INSERT VERSION"); + + Functions.registerFunction(new SimpleJavaFunction("tint", new Parameter[] { + new Parameter<>("colour", DefaultClasses.COLOR, true, null), + new Parameter<>("amount", DefaultClasses.LONG, true, new SimpleLiteral<>(1L, true)), + new Parameter<>("hsl", DefaultClasses.BOOLEAN, true, new SimpleLiteral<>(false, true)) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color colour = (Color) params[0][0]; + Long amount = (Long) params[1][0]; + boolean hsl = (Boolean) params[2][0]; + return CollectionUtils.array(hsl + ? ColourUtils.tintColorHSL(colour, amount.intValue()) + : ColourUtils.tintColor(colour, amount.intValue())); + } + }).description( + "Tints a given colour by a given amount. Optionally can use HSL methods to achieve this instead.", + "The amount should be from 0-100, with 0 doing nothing, and 100 tinting it all the way to white." + ).examples( + "set {_lightRed} to tint(red, 10)", + "set {_lighterRed} to tint(red, 20)" + ).since("INSERT VERSION"); + + Functions.registerFunction(new SimpleJavaFunction("colourBrightness", new Parameter[] { + new Parameter<>("colour", DefaultClasses.COLOR, true, null), + new Parameter<>("amount", DefaultClasses.LONG, true, null) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color colour = (Color) params[0][0]; + Long amount = (Long) params[1][0]; + return CollectionUtils.array(ColourUtils.adjustBrightness(colour, amount.intValue())); + } + }).description( + "Adjusts the brightness of a colour by a given amount from -100 to 100." + ).examples( + "set {_brighterRed} to colourBrightness(red, 10)", + "set {_darkerRed} to colourBrightness(red, -10)" + ).since("INSERT VERSION"); + + Functions.registerFunction(new SimpleJavaFunction("grayscale", new Parameter[] { + new Parameter<>("colour", DefaultClasses.COLOR, true, null) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color colour = (Color) params[0][0]; + return CollectionUtils.array(ColourUtils.toGrayscale(colour)); + } + }).description( + "Returns a colour converted to grayscale." + ).examples( + "set {_redButGrayscale} to grayscale(red)" + ).since("INSERT VERSION"); + + Functions.registerFunction(new SimpleJavaFunction("sepiatone", new Parameter[] { + new Parameter<>("colour", DefaultClasses.COLOR, true, null) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color colour = (Color) params[0][0]; + return CollectionUtils.array(ColourUtils.toSepia(colour)); + } + }).description( + "Returns a colour converted to sepiatone." + ).examples( + "set {_redButSepiatone} to sepiatone(red)" + ).since("INSERT VERSION"); + } + +} diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java b/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java new file mode 100644 index 00000000000..7dbb6d705b5 --- /dev/null +++ b/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java @@ -0,0 +1,214 @@ +package org.skriptlang.skript.misc.colours; + +import ch.njol.skript.util.Color; +import ch.njol.skript.util.ColorRGB; + +public class ColourUtils { + + public static float[] rgbToHsl(Color color) { + float r = color.getRed() / 255f; + float g = color.getGreen() / 255f; + float b = color.getBlue() / 255f; + float max = Math.max(r, Math.max(g, b)); + float min = Math.min(r, Math.min(g, b)); + float h, s, l = (max + min) / 2f; + if (max == min) { + h = s = 0f; + } else { + float delta = max - min; + s = l > 0.5f ? delta / (2f - max - min) : delta / (max + min); + if (max == r) { + h = ((g - b) / delta + (g < b ? 6f : 0f)) / 6f; + } else if (max == g) { + h = ((b - r) / delta + 2f) / 6f; + } else { + h = ((r - g) / delta + 4f) / 6f; + } + } + return new float[]{ h, s, l }; + } + + public static ColorRGB hslToRgb(float[] hsl) { + float h = hsl[0], s = hsl[1], l = hsl[2]; + float r, g, b; + if (s == 0f) { + r = g = b = l; + } else { + float q = l < 0.5f ? l * (1f + s) : l + s - l * s; + float p = 2f * l - q; + r = hueToRgb(p, q, h + 1f / 3f); + g = hueToRgb(p, q, h); + b = hueToRgb(p, q, h - 1f / 3f); + } + int red = Math.round(r * 255f); + int green = Math.round(g * 255f); + int blue = Math.round(b * 255f); + return ColorRGB.fromRGBA(red, green, blue, 255); + } + + private static float hueToRgb(float p, float q, float t) { + if (t < 0f) + t += 1f; + if (t > 1f) + t -= 1f; + if (t < 1f / 6f) + return p + (q - p) * 6f * t; + if (t < 1f / 2f) + return q; + if (t < 2f / 3f) + return p + (q - p) * (2f / 3f - t) * 6f; + return p; + } + + public static Color blendColors(Color c1, Color c2, double amount) { + amount = Math.max(0, Math.min(1, amount)); + int r = (int) (c1.getRed() * (1 - amount) + c2.getRed() * amount); + int g = (int) (c1.getGreen() * (1 - amount) + c2.getGreen() * amount); + int b = (int) (c1.getBlue() * (1 - amount) + c2.getBlue() * amount); + int a = (int) (c1.getAlpha() * (1 - amount) + c2.getAlpha() * amount); + return ColorRGB.fromRGBA(r, g, b, a); + } + + public static Color complementColor(Color color) { + int r = 255 - color.getRed(); + int g = 255 - color.getGreen(); + int b = 255 - color.getBlue(); + return ColorRGB.fromRGBA(r, g, b, color.getAlpha()); + } + + public static Color complementColorHSL(Color color) { + float[] hsl = rgbToHsl(color); + hsl[0] = (hsl[0] + 0.5f) % 1f; + return hslToRgb(hsl); + } + + public static ColorRGB shadeColor(Color color, int amount) { + amount = Math.max(1, Math.min(100, amount)); + double factor = (100 - amount) / 100.0; + int r = (int) (color.getRed() * factor); + int g = (int) (color.getGreen() * factor); + int b = (int) (color.getBlue() * factor); + return ColorRGB.fromRGBA(r, g, b, color.getAlpha()); + } + + public static ColorRGB shadeColorHSL(Color color, int amount) { + amount = Math.max(1, Math.min(100, amount)); + float[] hsl = rgbToHsl(color); + hsl[2] *= (100 - amount) / 100f; + return hslToRgb(hsl); + } + + public static ColorRGB tintColor(Color color, int amount) { + amount = Math.max(1, Math.min(100, amount)); + double factor = amount / 100.0; + int r = (int) (color.getRed() + (255 - color.getRed()) * factor); + int g = (int) (color.getGreen() + (255 - color.getGreen()) * factor); + int b = (int) (color.getBlue() + (255 - color.getBlue()) * factor); + return ColorRGB.fromRGBA(r, g, b, color.getAlpha()); + } + + public static ColorRGB tintColorHSL(Color color, int amount) { + amount = Math.max(1, Math.min(100, amount)); + float[] hsl = rgbToHsl(color); + hsl[2] += (1f - hsl[2]) * (amount / 100f); + hsl[2] = Math.min(1f, hsl[2]); + return hslToRgb(hsl); + } + + public static Color rotateHue(Color color, int degrees) { + float[] hsl = rgbToHsl(color); + hsl[0] = (hsl[0] + degrees / 360f) % 1f; + if (hsl[0] < 0f) + hsl[0] += 1f; + return hslToRgb(hsl); + } + + public static ColorRGB adjustBrightness(Color color, int amount) { + amount = Math.max(-100, Math.min(100, amount)); + float[] hsb = rgbToHsb(color); + float factor = amount / 100f; + hsb[2] = hsb[2] + hsb[2] * factor; + hsb[2] = Math.max(0f, Math.min(1f, hsb[2])); + return hsbToRgb(hsb); + } + + private static float[] rgbToHsb(Color color) { + float r = color.getRed() / 255f; + float g = color.getGreen() / 255f; + float b = color.getBlue() / 255f; + float max = Math.max(r, Math.max(g, b)); + float min = Math.min(r, Math.min(g, b)); + float delta = max - min; + float h = 0f; + float s = max == 0f ? 0f : delta / max; + float v = max; + if (delta != 0f) { + if (max == r) { + h = ((g - b) / delta) % 6f; + } else if (max == g) { + h = ((b - r) / delta) + 2f; + } else { + h = ((r - g) / delta) + 4f; + } + h *= 60f; + if (h < 0f) h += 360f; + } + h /= 360f; + return new float[]{ h, s, v }; + } + + private static ColorRGB hsbToRgb(float[] hsb) { + float h = hsb[0], s = hsb[1], v = hsb[2]; + float r = 0f, g = 0f, b = 0f; + int i = (int) (h * 6f); + float f = h * 6f - i; + float p = v * (1f - s); + float q = v * (1f - f * s); + float t = v * (1f - (1f - f) * s); + switch (i % 6) { + case 0: + r = v; g = t; b = p; break; + case 1: + r = q; g = v; b = p; break; + case 2: + r = p; g = v; b = t; break; + case 3: + r = p; g = q; b = v; break; + case 4: + r = t; g = p; b = v; break; + case 5: + r = v; g = p; b = q; break; + } + int red = Math.round(r * 255f); + int green = Math.round(g * 255f); + int blue = Math.round(b * 255f); + return ColorRGB.fromRGBA(red, green, blue, 255); + } + + public static ColorRGB toGrayscale(Color color) { + int gray = (int)(0.299 * color.getRed() + 0.587 * color.getGreen() + 0.114 * color.getBlue()); + return ColorRGB.fromRGBA(gray, gray, gray, color.getAlpha()); + } + + public static ColorRGB toSepia(Color color) { + int r = color.getRed(); + int g = color.getGreen(); + int b = color.getBlue(); + int tr = (int) (0.393 * r + 0.769 * g + 0.189 * b); + int tg = (int) (0.349 * r + 0.686 * g + 0.168 * b); + int tb = (int) (0.272 * r + 0.534 * g + 0.131 * b); + tr = Math.min(255, tr); + tg = Math.min(255, tg); + tb = Math.min(255, tb); + return ColorRGB.fromRGBA(tr, tg, tb, color.getAlpha()); + } + + public static ColorRGB adjustTemperature(Color color, int amount) { + int r = color.getRed() + amount; + int b = color.getBlue() - amount; + r = Math.max(0, Math.min(255, r)); + b = Math.max(0, Math.min(255, b)); + return ColorRGB.fromRGBA(r, color.getGreen(), b, color.getAlpha()); + } + +} diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java new file mode 100644 index 00000000000..c4e2cb13c36 --- /dev/null +++ b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java @@ -0,0 +1,61 @@ +package org.skriptlang.skript.misc.colours; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.SyntaxStringBuilder; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.util.Color; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.jetbrains.annotations.Nullable; + +public class ExprBlend extends SimpleExpression { + + static { + Skript.registerExpression(ExprBlend.class, Color.class, ExpressionType.COMBINED, + "%colours% (blended|mixed) with %colours% [by [[a[n] (factor|amount)] %-number%]", + "blend of %colours% (and|with) %colours% [by [[a[n] (factor|amount)] %-number%]"); + } + + private Expression colours, blendWith; + private Expression amount; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + this.colours = (Expression) exprs[0]; + this.blendWith = (Expression) exprs[1]; + this.amount = (Expression) exprs[2]; + return true; + } + + @Override + protected Color @Nullable [] get(Event event) { + + return new Color[0]; + } + + @Override + public boolean isSingle() { + return this.colours.isSingle(); + } + + @Override + public Class getReturnType() { + return Color.class; + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return new SyntaxStringBuilder(event, debug) + .append(this.colours) + .append("blended with") + .append(this.blendWith) + .append("by a factor of") + .append(this.amount) + .toString(); + } + +} diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java b/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java new file mode 100644 index 00000000000..4a9a9ba81c7 --- /dev/null +++ b/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java @@ -0,0 +1,28 @@ +package org.skriptlang.skript.misc.colours; + +import ch.njol.skript.expressions.base.SimplePropertyExpression; +import ch.njol.skript.util.Color; +import org.jetbrains.annotations.Nullable; + +public class ExprComplement extends SimplePropertyExpression { + + static { + register(ExprComplement.class, Color.class, "complement[ary colo[u]r]", "colours"); + } + + @Override + public @Nullable Color convert(Color from) { + return ColourUtils.complementColor(from); + } + + @Override + protected String getPropertyName() { + return "complement"; + } + + @Override + public Class getReturnType() { + return Color.class; + } + +} From bd64435f7adf656136d502b75b2209bc3261ca77 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sat, 7 Dec 2024 19:12:53 +1000 Subject: [PATCH 02/39] Adds blend expression --- .../skript/misc/colours/ExprBlend.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java index c4e2cb13c36..74088fb8555 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java +++ b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java @@ -1,6 +1,10 @@ package org.skriptlang.skript.misc.colours; import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.SkriptParser; @@ -11,6 +15,20 @@ import org.bukkit.event.Event; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.List; + +@Name("Blended Colours") +@Description({ + "Returns the result of blending colours together. Optionally takes an amount to blend the colours by, which is", + "a number from 0 to 100. In that range, a 50 would be an expected equal blend of each colour (the default behaviour)." +}) +@Examples({ + "set {_purple} to red blended with blue", + "set {_goldyPurple} to {_purple} blended with gold", + "set {_aBunch} to red blended with all colours where [input is not red]" +}) +@Since("INSERT VERSION") public class ExprBlend extends SimpleExpression { static { @@ -33,8 +51,22 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye @Override protected Color @Nullable [] get(Event event) { + Color[] colours = this.colours.getArray(event); + Color[] blendWiths = this.blendWith.getArray(event); + Number amount = this.amount.getSingle(event); + if (amount == null) + amount = 50; + + List blendedColours = new ArrayList<>(); + for (Color colour : colours) { + Color blended = colour; + for (Color blendWith : blendWiths) { + blended = ColourUtils.blendColors(blended, blendWith, amount.doubleValue()); + } + blendedColours.add(blended); + } - return new Color[0]; + return blendedColours.toArray(new Color[0]); } @Override @@ -49,13 +81,14 @@ public Class getReturnType() { @Override public String toString(@Nullable Event event, boolean debug) { - return new SyntaxStringBuilder(event, debug) + return "colours blended together"; + /* return new SyntaxStringBuilder(event, debug) .append(this.colours) .append("blended with") .append(this.blendWith) .append("by a factor of") .append(this.amount) - .toString(); + .toString(); */ } } From ae81c9e6da7ba0afafcd15d0659b61ebcdbd590b Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sat, 7 Dec 2024 19:19:01 +1000 Subject: [PATCH 03/39] Adds complement expression --- .../skript/misc/colours/ExprComplement.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java b/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java index 4a9a9ba81c7..627f58a71c7 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java +++ b/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java @@ -1,18 +1,43 @@ package org.skriptlang.skript.misc.colours; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; import ch.njol.skript.expressions.base.SimplePropertyExpression; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.util.Color; +import ch.njol.util.Kleenean; import org.jetbrains.annotations.Nullable; +@Name("Complementary Colours") +@Description({ + "Returns the complementary colour of a given colour(s).", + "Can optionally use a HSL-based approach if need be, but this is rarely required." +}) +@Examples({ + "set {_bluesComplement} to complement of blue", + "set {_allComplements} to complementary colour of all colours" +}) +@Since("INSERT VERSION") public class ExprComplement extends SimplePropertyExpression { static { - register(ExprComplement.class, Color.class, "complement[ary colo[u]r]", "colours"); + register(ExprComplement.class, Color.class, "[:hsl] complement[ary colo[u]r]", "colours"); + } + + private boolean hsl; + + @Override + public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + this.hsl = parseResult.hasTag("hsl"); + return super.init(expressions, matchedPattern, isDelayed, parseResult); } @Override public @Nullable Color convert(Color from) { - return ColourUtils.complementColor(from); + return hsl ? ColourUtils.complementColorHSL(from) : ColourUtils.complementColor(from); } @Override From d5985d6181795db9dfb76af12ea38433a9d952f0 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sat, 7 Dec 2024 19:33:23 +1000 Subject: [PATCH 04/39] Small change --- .../org/skriptlang/skript/misc/colours/ExprBlend.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java index 74088fb8555..6eb0d63cae1 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java +++ b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java @@ -7,7 +7,7 @@ import ch.njol.skript.doc.Since; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.SyntaxStringBuilder; import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.skript.util.Color; @@ -26,6 +26,7 @@ @Examples({ "set {_purple} to red blended with blue", "set {_goldyPurple} to {_purple} blended with gold", + "set {_morePurpleThanGold} to {_purple} blended with gold by an amount of 10", "set {_aBunch} to red blended with all colours where [input is not red]" }) @Since("INSERT VERSION") @@ -33,8 +34,8 @@ public class ExprBlend extends SimpleExpression { static { Skript.registerExpression(ExprBlend.class, Color.class, ExpressionType.COMBINED, - "%colours% (blended|mixed) with %colours% [by [[a[n] (factor|amount)] %-number%]", - "blend of %colours% (and|with) %colours% [by [[a[n] (factor|amount)] %-number%]"); + "%colours% (blended|mixed) with %colours% [by [a[n] (factor|amount) of] %-number%]", + "blend of %colours% (and|with) %colours% [by [a[n] (factor|amount) of] %-number%]"); } private Expression colours, blendWith; @@ -42,7 +43,7 @@ public class ExprBlend extends SimpleExpression { @Override @SuppressWarnings("unchecked") - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { this.colours = (Expression) exprs[0]; this.blendWith = (Expression) exprs[1]; this.amount = (Expression) exprs[2]; From fc342b84820f29062551b58e7f97ab77eb0cc271 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sat, 7 Dec 2024 20:26:47 +1000 Subject: [PATCH 05/39] Fixed invalid spelling of colour throwing a pattern exception --- .../java/org/skriptlang/skript/misc/colours/ExprBlend.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java index 6eb0d63cae1..24a92a0d626 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java +++ b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java @@ -34,8 +34,8 @@ public class ExprBlend extends SimpleExpression { static { Skript.registerExpression(ExprBlend.class, Color.class, ExpressionType.COMBINED, - "%colours% (blended|mixed) with %colours% [by [a[n] (factor|amount) of] %-number%]", - "blend of %colours% (and|with) %colours% [by [a[n] (factor|amount) of] %-number%]"); + "%colors% (blended|mixed) with %colors% [by [a[n] (factor|amount) of] %-number%]", + "blend of %colors% (and|with) %colors% [by [a[n] (factor|amount) of] %-number%]"); } private Expression colours, blendWith; From 50e064c5c0f2c8129c4491cd03ae174c47f9b29f Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sat, 7 Dec 2024 20:26:56 +1000 Subject: [PATCH 06/39] Fixed invalid spelling of colour throwing a pattern exception --- .../java/org/skriptlang/skript/misc/colours/ExprComplement.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java b/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java index 627f58a71c7..23022ad5f95 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java +++ b/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java @@ -24,7 +24,7 @@ public class ExprComplement extends SimplePropertyExpression { static { - register(ExprComplement.class, Color.class, "[:hsl] complement[ary colo[u]r]", "colours"); + register(ExprComplement.class, Color.class, "[:hsl] complement[ary colo[u]r]", "colors"); } private boolean hsl; From a1cadb790751e0818ee63a68620dc50d6375e99f Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sat, 7 Dec 2024 20:27:36 +1000 Subject: [PATCH 07/39] Adds hex code expression - Also fixes blending util method using old, wrong scale --- .../skript/misc/colours/ColourUtils.java | 7 ++++- .../skript/misc/colours/ExprHex.java | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/skriptlang/skript/misc/colours/ExprHex.java diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java b/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java index 7dbb6d705b5..87378021489 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java +++ b/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java @@ -5,6 +5,10 @@ public class ColourUtils { + public static String toHex(Color color) { + return String.format("<#%02X%02X%02X>", color.getRed(), color.getGreen(), color.getBlue()); + } + public static float[] rgbToHsl(Color color) { float r = color.getRed() / 255f; float g = color.getGreen() / 255f; @@ -61,7 +65,8 @@ private static float hueToRgb(float p, float q, float t) { } public static Color blendColors(Color c1, Color c2, double amount) { - amount = Math.max(0, Math.min(1, amount)); + amount = Math.max(0, Math.min(100, amount)); + amount /= 100.0; int r = (int) (c1.getRed() * (1 - amount) + c2.getRed() * amount); int g = (int) (c1.getGreen() * (1 - amount) + c2.getGreen() * amount); int b = (int) (c1.getBlue() * (1 - amount) + c2.getBlue() * amount); diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprHex.java b/src/main/java/org/skriptlang/skript/misc/colours/ExprHex.java new file mode 100644 index 00000000000..903c14c5040 --- /dev/null +++ b/src/main/java/org/skriptlang/skript/misc/colours/ExprHex.java @@ -0,0 +1,27 @@ +package org.skriptlang.skript.misc.colours; + +import ch.njol.skript.expressions.base.SimplePropertyExpression; +import ch.njol.skript.util.Color; +import org.jetbrains.annotations.Nullable; + +public class ExprHex extends SimplePropertyExpression { + + static { + register(ExprHex.class, String.class, "hex [code]", "colors"); + } + + @Override + public @Nullable String convert(Color from) { + return ColourUtils.toHex(from); + } + + @Override + protected String getPropertyName() { + return "hex code"; + } + + @Override + public Class getReturnType() { + return String.class; + } +} From e2d90819a008fb51a253fde10b4443b9b57e4d4a Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 11:50:26 +1000 Subject: [PATCH 08/39] Adds channel as an option in ExprARGB's patterns --- src/main/java/ch/njol/skript/expressions/ExprARGB.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprARGB.java b/src/main/java/ch/njol/skript/expressions/ExprARGB.java index 6731171cdad..8719703beba 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprARGB.java +++ b/src/main/java/ch/njol/skript/expressions/ExprARGB.java @@ -28,7 +28,7 @@ public class ExprARGB extends SimplePropertyExpression { static { - register(ExprARGB.class, Integer.class, "(:alpha|:red|:green|:blue) (value|component)", "colors"); + register(ExprARGB.class, Integer.class, "(:alpha|:red|:green|:blue) (value|component|channel)", "colors"); } private RGB color; From b50ed36e435e6f1531f9258b738d16e610f4a52a Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 11:50:57 +1000 Subject: [PATCH 09/39] Fixes number logic and adds another pattern to ExprBlend --- .../java/org/skriptlang/skript/misc/colours/ExprBlend.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java index 24a92a0d626..053bdefe1e3 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java +++ b/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java @@ -35,7 +35,8 @@ public class ExprBlend extends SimpleExpression { static { Skript.registerExpression(ExprBlend.class, Color.class, ExpressionType.COMBINED, "%colors% (blended|mixed) with %colors% [by [a[n] (factor|amount) of] %-number%]", - "blend of %colors% (and|with) %colors% [by [a[n] (factor|amount) of] %-number%]"); + "blend of %colors% (and|with) %colors% [by [a[n] (factor|amount) of] %-number%]", + "%colors% and %colors% blen(ded|t) together [by [a[n] (factor|amount) of] %-number%]"); } private Expression colours, blendWith; @@ -54,9 +55,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye protected Color @Nullable [] get(Event event) { Color[] colours = this.colours.getArray(event); Color[] blendWiths = this.blendWith.getArray(event); - Number amount = this.amount.getSingle(event); - if (amount == null) - amount = 50; + Number amount = this.amount == null ? 50 : this.amount.getOptionalSingle(event).orElse(50); List blendedColours = new ArrayList<>(); for (Color colour : colours) { From 2427be40451a44b19bf3feccb2dc5f079b946a95 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 11:51:27 +1000 Subject: [PATCH 10/39] Removes the <> being included in the hex code util method --- .../java/org/skriptlang/skript/misc/colours/ColourUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java b/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java index 87378021489..d4dd7baac23 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java +++ b/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java @@ -6,7 +6,7 @@ public class ColourUtils { public static String toHex(Color color) { - return String.format("<#%02X%02X%02X>", color.getRed(), color.getGreen(), color.getBlue()); + return String.format("#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue()); } public static float[] rgbToHsl(Color color) { From d72521562221942ccb9050af48ca9e59ce4ec7ad Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 11:51:48 +1000 Subject: [PATCH 11/39] Adds hex codes test --- .../syntaxes/expressions/ExprColourHex.sk | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprColourHex.sk diff --git a/src/test/skript/tests/syntaxes/expressions/ExprColourHex.sk b/src/test/skript/tests/syntaxes/expressions/ExprColourHex.sk new file mode 100644 index 00000000000..dac0d39a123 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprColourHex.sk @@ -0,0 +1,21 @@ +test "hex codes": + + assert hex code of black is raw "#1D1D21" with "black hex code was not correct" + assert hex code of dark grey is raw "#474F52" with "black hex code was not correct" + assert hex code of grey is raw "#9D9D97" with "black hex code was not correct" + assert hex code of white is raw "#F9FFFE" with "black hex code was not correct" + assert hex code of blue is raw "#3C44AA" with "black hex code was not correct" + assert hex code of brown is raw "#835432" with "black hex code was not correct" + assert hex code of cyan is raw "#169C9C" with "black hex code was not correct" + assert hex code of light cyan is raw "#3AB3DA" with "black hex code was not correct" + assert hex code of green is raw "#5E7C16" with "black hex code was not correct" + assert hex code of light green is raw "#80C71F" with "black hex code was not correct" + assert hex code of yellow is raw "#FED83D" with "black hex code was not correct" + assert hex code of orange is raw "#F9801D" with "black hex code was not correct" + assert hex code of red is raw "#B02E26" with "black hex code was not correct" + assert hex code of pink is raw "#F38BAA" with "black hex code was not correct" + assert hex code of purple is raw "#8932B8" with "black hex code was not correct" + assert hex code of magenta is raw "#C74EBD" with "black hex code was not correct" + + assert hex code of test block is not set with "hex code of an invalid type shouldn't return anything" + assert hex code of {_x} is not set with "hex code of an invalid type shouldn't return anything" From 9875ffeec85eb627b3e9eb70458c6290a011477a Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 11:53:22 +1000 Subject: [PATCH 12/39] Adds colour blending test --- .../tests/syntaxes/expressions/ExprColourBlend.sk | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprColourBlend.sk diff --git a/src/test/skript/tests/syntaxes/expressions/ExprColourBlend.sk b/src/test/skript/tests/syntaxes/expressions/ExprColourBlend.sk new file mode 100644 index 00000000000..99730fd3ede --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprColourBlend.sk @@ -0,0 +1,13 @@ +test "colour blends": + + set {_redAndBlue} to red blended with blue + set {_blackAndYellow} to black blended with yellow + set {_pinkAndPurple} to pink blended with purple by an amount of 20 + + assert hex of {_redAndBlue} is "#763968" with "hex of red and blue blended together was incorrect" + assert hex of {_blackAndYellow} is "#8D7A2F" with "hex of black and yellow blended together was incorrect" + assert hex of {_pinkAndPurple} is "#DD79AC" with "hex of pink and purple blended together by an amount of 20%% was incorrect" + + assert red blended with test block is not set with "blending invalid types should return null" + assert "x" blended with "y" is not set with "blending invalid types should return null" + assert {_x} blended with {_y} is not set with "blending invalid types should return null" From 67320f9ba738911f7ee5099935ef771e57649c16 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 12:10:30 +1000 Subject: [PATCH 13/39] Adds colour complements test --- .../expressions/ExprColourComplement.sk | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprColourComplement.sk diff --git a/src/test/skript/tests/syntaxes/expressions/ExprColourComplement.sk b/src/test/skript/tests/syntaxes/expressions/ExprColourComplement.sk new file mode 100644 index 00000000000..4043321a5a0 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprColourComplement.sk @@ -0,0 +1,23 @@ +test "colour complements": + + set {_bluesComplement} to complement of blue + set {_bluesHslComplement} to hsl complement of blue + set {_bluesComplementsComplement} to complement of {_bluesComplement} + set {_bluesHslComplementsHslComplement} to hsl complement of {_bluesHslComplement} + set {_trueBlacksComplement} to complement of rgb(0, 0, 0) + set {_trueBlacksHslComplement} to hsl complement of rgb(0, 0, 0) + set {_trueWhitesComplement} to complement of rgb(255, 255, 255) + set {_trueWhitesHslComplement} to hsl complement of rgb(255, 255, 255) + + assert hex code of {_bluesComplement} is "#C3BB55" with "complement of blue was incorrect" + assert hex code of {_bluesHslComplement} is "#AAA23C" with "hsl complement of blue was incorrect" + assert hex code of {_bluesComplementsComplement} is hex code of blue with "complement of blue's complement was incorrect" + assert hex code of {_bluesHslComplementsHslComplement} is hex code of blue with "hsl complement of blue's hsl complement was incorrect" + assert hex code of {_trueBlacksComplement} is "#FFFFFF" with "complement of true black was incorrect" + assert hex code of {_trueBlacksHslComplement} is "#000000" with "hsl complement of true black was incorrect" + assert hex code of {_trueWhitesComplement} is "#000000" with "complement of true white was incorrect" + assert hex code of {_trueWhitesHslComplement} is "#FFFFFF" with "hsl complement of true white was incorrect" + + assert complement of test block is not set with "complement of an invalid type should return null" + assert complement of "x" is not set with "complement of an invalid type should return null" + assert complement of {_x} is not set with "complement of an invalid type should return null" From 3123bdbcec9afb9e05542a65d55748249a4d2387 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 12:41:24 +1000 Subject: [PATCH 14/39] Fixes tests --- .../syntaxes/expressions/ExprColourBlend.sk | 2 -- .../expressions/ExprColourComplement.sk | 20 +++++++++---------- .../syntaxes/expressions/ExprColourHex.sk | 1 - 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprColourBlend.sk b/src/test/skript/tests/syntaxes/expressions/ExprColourBlend.sk index 99730fd3ede..aeb4e31ac9d 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprColourBlend.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprColourBlend.sk @@ -8,6 +8,4 @@ test "colour blends": assert hex of {_blackAndYellow} is "#8D7A2F" with "hex of black and yellow blended together was incorrect" assert hex of {_pinkAndPurple} is "#DD79AC" with "hex of pink and purple blended together by an amount of 20%% was incorrect" - assert red blended with test block is not set with "blending invalid types should return null" - assert "x" blended with "y" is not set with "blending invalid types should return null" assert {_x} blended with {_y} is not set with "blending invalid types should return null" diff --git a/src/test/skript/tests/syntaxes/expressions/ExprColourComplement.sk b/src/test/skript/tests/syntaxes/expressions/ExprColourComplement.sk index 4043321a5a0..f46a3a84a5f 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprColourComplement.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprColourComplement.sk @@ -1,13 +1,13 @@ test "colour complements": - set {_bluesComplement} to complement of blue - set {_bluesHslComplement} to hsl complement of blue - set {_bluesComplementsComplement} to complement of {_bluesComplement} - set {_bluesHslComplementsHslComplement} to hsl complement of {_bluesHslComplement} - set {_trueBlacksComplement} to complement of rgb(0, 0, 0) - set {_trueBlacksHslComplement} to hsl complement of rgb(0, 0, 0) - set {_trueWhitesComplement} to complement of rgb(255, 255, 255) - set {_trueWhitesHslComplement} to hsl complement of rgb(255, 255, 255) + set {_bluesComplement} to complementary colour of blue + set {_bluesHslComplement} to hsl complementary colour of blue + set {_bluesComplementsComplement} to complementary colour of {_bluesComplement} + set {_bluesHslComplementsHslComplement} to hsl complementary colour of {_bluesHslComplement} + set {_trueBlacksComplement} to complementary colour of rgb(0, 0, 0) + set {_trueBlacksHslComplement} to hsl complementary colour of rgb(0, 0, 0) + set {_trueWhitesComplement} to complementary colour of rgb(255, 255, 255) + set {_trueWhitesHslComplement} to hsl complementary colour of rgb(255, 255, 255) assert hex code of {_bluesComplement} is "#C3BB55" with "complement of blue was incorrect" assert hex code of {_bluesHslComplement} is "#AAA23C" with "hsl complement of blue was incorrect" @@ -18,6 +18,4 @@ test "colour complements": assert hex code of {_trueWhitesComplement} is "#000000" with "complement of true white was incorrect" assert hex code of {_trueWhitesHslComplement} is "#FFFFFF" with "hsl complement of true white was incorrect" - assert complement of test block is not set with "complement of an invalid type should return null" - assert complement of "x" is not set with "complement of an invalid type should return null" - assert complement of {_x} is not set with "complement of an invalid type should return null" + assert complementary colour of {_x} is not set with "complement of an invalid type should return null" diff --git a/src/test/skript/tests/syntaxes/expressions/ExprColourHex.sk b/src/test/skript/tests/syntaxes/expressions/ExprColourHex.sk index dac0d39a123..8d7539c1be3 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprColourHex.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprColourHex.sk @@ -17,5 +17,4 @@ test "hex codes": assert hex code of purple is raw "#8932B8" with "black hex code was not correct" assert hex code of magenta is raw "#C74EBD" with "black hex code was not correct" - assert hex code of test block is not set with "hex code of an invalid type shouldn't return anything" assert hex code of {_x} is not set with "hex code of an invalid type shouldn't return anything" From 34d2024e5b8199696afedc585ae5056102124264 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 13:39:31 +1000 Subject: [PATCH 15/39] Adds function tests (shade, tint, brightness, grayscale, sepiatone) --- .../syntaxes/functions/colours/brightness.sk | 17 +++++++++++++++++ .../syntaxes/functions/colours/grayscale.sk | 11 +++++++++++ .../syntaxes/functions/colours/sepiatone.sk | 11 +++++++++++ .../tests/syntaxes/functions/colours/shade.sk | 19 +++++++++++++++++++ .../tests/syntaxes/functions/colours/tint.sk | 19 +++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 src/test/skript/tests/syntaxes/functions/colours/brightness.sk create mode 100644 src/test/skript/tests/syntaxes/functions/colours/grayscale.sk create mode 100644 src/test/skript/tests/syntaxes/functions/colours/sepiatone.sk create mode 100644 src/test/skript/tests/syntaxes/functions/colours/shade.sk create mode 100644 src/test/skript/tests/syntaxes/functions/colours/tint.sk diff --git a/src/test/skript/tests/syntaxes/functions/colours/brightness.sk b/src/test/skript/tests/syntaxes/functions/colours/brightness.sk new file mode 100644 index 00000000000..eb814ea4efd --- /dev/null +++ b/src/test/skript/tests/syntaxes/functions/colours/brightness.sk @@ -0,0 +1,17 @@ +test "colour brightness": + + set {_redBrightness-100} to colourBrightness(red, -100) + set {_redBrightness-10} to colourBrightness(red, -10) + set {_redBrightness10} to colourBrightness(red, 10) + set {_redBrightness100} to colourBrightness(red, 100) + set {_redBrightnessInfinity} to colourBrightness(red, infinity value) + set {_redBrightnessNan} to colourBrightness(red, nan value) + + assert hex code of {_redBrightness-100} is "#000000" with "hex code of red brightened by -100 was incorrect" + assert hex code of {_redBrightness-10} is "#9E2922" with "hex code of red brightened by -10 was incorrect" + assert hex code of {_redBrightness10} is "#C2332A" with "hex code of red brightened by 10 was incorrect" + assert hex code of {_redBrightness100} is "#FF4337" with "hex code of red brightened by 100 was incorrect" + assert hex code of {_redBrightnessInfinity} is "#B03028" with "adjusting the brightness of a colour by infinity should clamp between -100 and 100" + assert hex code of {_redBrightnessNan} is "#B03028" with "adjusting the brightness of a colour by nan should clamp between -100 and 100" + + assert colourBrightness({_x}) is not set with "adjusting the brightness an invalid type shouldn't return anything" diff --git a/src/test/skript/tests/syntaxes/functions/colours/grayscale.sk b/src/test/skript/tests/syntaxes/functions/colours/grayscale.sk new file mode 100644 index 00000000000..ba4a2943dd8 --- /dev/null +++ b/src/test/skript/tests/syntaxes/functions/colours/grayscale.sk @@ -0,0 +1,11 @@ +test "colour grayscales": + + set {_redGrayscale} to grayscale(red) + set {_redAndBlueGrayscale} to grayscale(red blended with blue) + set {_shadedRedGrayscale} to grayscale(shade(red, 50, true)) + + assert hex code of {_redGrayscale} is "#535353" with "hex code of grayscale of red was incorrect" + assert hex code of {_redAndBlueGrayscale} is "#505050" with "hex code of grayscale of red was incorrect" + assert hex code of {_shadedRedGrayscale} is "#292929" with "hex code of grayscale of red was incorrect" + + assert grayscale({_x}) is not set with "getting the grayscale of an invalid type shouldn't return anything" diff --git a/src/test/skript/tests/syntaxes/functions/colours/sepiatone.sk b/src/test/skript/tests/syntaxes/functions/colours/sepiatone.sk new file mode 100644 index 00000000000..fa263852bfe --- /dev/null +++ b/src/test/skript/tests/syntaxes/functions/colours/sepiatone.sk @@ -0,0 +1,11 @@ +test "colour sepiatones": + + set {_redSepiatone} to sepiatone(red) + set {_redAndBlueSepiatone} to sepiatone(red blended with blue) + set {_shadedRedSepiatone} to sepiatone(shade(red, 50, true)) + + assert hex code of {_redSepiatone} is "#6F634D" with "hex code of sepiatone of red was incorrect" + assert hex code of {_redAndBlueSepiatone} is "#6D614C" with "hex code of sepiatone of red was incorrect" + assert hex code of {_shadedRedSepiatone} is "#373126" with "hex code of sepiatone of red was incorrect" + + assert sepiatone({_x}) is not set with "getting the sepiatone of an invalid type shouldn't return anything" diff --git a/src/test/skript/tests/syntaxes/functions/colours/shade.sk b/src/test/skript/tests/syntaxes/functions/colours/shade.sk new file mode 100644 index 00000000000..faee17f5e59 --- /dev/null +++ b/src/test/skript/tests/syntaxes/functions/colours/shade.sk @@ -0,0 +1,19 @@ +test "colour shades": + + set {_redShade} to shade(red) + set {_redShade10} to shade(red, 10) + set {_redShade100} to shade(red, 100) + set {_redShadeHsl} to shade(red, 1, true) + set {_redShadeNegative} to shade(red, -4) + set {_redShadeInfinity} to shade(red, infinity value) + set {_redShadeNan} to shade(red, nan value) + + assert hex code of {_redShade} is "#AE2D25" with "hex code of red shaded by 1 was incorrect" + assert hex code of {_redShade10} is "#9E2922" with "hex code of red shaded by 10 was incorrect" + assert hex code of {_redShade100} is "#000000" with "hex code of red shaded by 100 was incorrect" + assert hex code of {_redShadeHsl} is "#AE2E26" with "hex code of red shaded by 1 using hsl methods was incorrect" + assert hex code of {_redShadeNegative} is "#AE2D25" with "shading a colour by a negative amount should clamp between 1 and 100" + assert hex code of {_redShadeInfinity} is "#AE2D25" with "shading a colour by infinity should clamp between 1 and 100" + assert hex code of {_redShadeNan} is "#AE2D25" with "shading a colour by nan should clamp between 1 and 100" + + assert shade({_x}) is not set with "shading an invalid type shouldn't return anything" diff --git a/src/test/skript/tests/syntaxes/functions/colours/tint.sk b/src/test/skript/tests/syntaxes/functions/colours/tint.sk new file mode 100644 index 00000000000..a92f73773c9 --- /dev/null +++ b/src/test/skript/tests/syntaxes/functions/colours/tint.sk @@ -0,0 +1,19 @@ +test "colour tints": + + set {_redTint} to tint(red) + set {_redTint10} to tint(red, 10) + set {_redTint100} to tint(red, 100) + set {_redTintHsl} to tint(red, 1, true) + set {_redTintNegative} to tint(red, -4) + set {_redTintInfinity} to tint(red, infinity value) + set {_redTintNan} to tint(red, nan value) + + assert hex code of {_redTint} is "#B03028" with "hex code of red tinted by 1 was incorrect" + assert hex code of {_redTint10} is "#B7423B" with "hex code of red tinted by 10 was incorrect" + assert hex code of {_redTint100} is "#FFFFFF" with "hex code of red tinted by 100 was incorrect" + assert hex code of {_redTintHsl} is "#B22F27" with "hex code of red tinted by 1 using hsl methods was incorrect" + assert hex code of {_redTintNegative} is "#B03028" with "tinting a colour by a negative amount should clamp between 1 and 100" + assert hex code of {_redTintInfinity} is "#B03028" with "tinting a colour by infinity should clamp between 1 and 100" + assert hex code of {_redTintNan} is "#B03028" with "tinting a colour by nan should clamp between 1 and 100" + + assert tint({_x}) is not set with "tinting an invalid type shouldn't return anything" From d07ce14f9c2e7618a94ffdf1047e156a66f47997 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 13:56:06 +1000 Subject: [PATCH 16/39] Fixes brightness test --- .../skript/tests/syntaxes/functions/colours/brightness.sk | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/skript/tests/syntaxes/functions/colours/brightness.sk b/src/test/skript/tests/syntaxes/functions/colours/brightness.sk index eb814ea4efd..b1c8cb9bd52 100644 --- a/src/test/skript/tests/syntaxes/functions/colours/brightness.sk +++ b/src/test/skript/tests/syntaxes/functions/colours/brightness.sk @@ -11,7 +11,7 @@ test "colour brightness": assert hex code of {_redBrightness-10} is "#9E2922" with "hex code of red brightened by -10 was incorrect" assert hex code of {_redBrightness10} is "#C2332A" with "hex code of red brightened by 10 was incorrect" assert hex code of {_redBrightness100} is "#FF4337" with "hex code of red brightened by 100 was incorrect" - assert hex code of {_redBrightnessInfinity} is "#B03028" with "adjusting the brightness of a colour by infinity should clamp between -100 and 100" - assert hex code of {_redBrightnessNan} is "#B03028" with "adjusting the brightness of a colour by nan should clamp between -100 and 100" + assert hex code of {_redBrightnessInfinity} is "#AE2E26" with "adjusting the brightness of a colour by infinity should clamp between -100 and 100" + assert hex code of {_redBrightnessNan} is "#B02E26" with "adjusting the brightness of a colour by nan should clamp between -100 and 100" - assert colourBrightness({_x}) is not set with "adjusting the brightness an invalid type shouldn't return anything" + assert colourBrightness({_x}, 4) is not set with "adjusting the brightness an invalid type shouldn't return anything" From c0e9cd78aab8f37e06ca9a2c8c2a3b3e34d24cbd Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sun, 8 Dec 2024 14:39:20 +1000 Subject: [PATCH 17/39] Adds documentation to ColourUtils --- .../skript/misc/colours/ColourUtils.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java b/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java index d4dd7baac23..871c52d0af1 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java +++ b/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java @@ -3,12 +3,27 @@ import ch.njol.skript.util.Color; import ch.njol.skript.util.ColorRGB; +/** + * Utility class for colour manipulation and conversion. + */ public class ColourUtils { + /** + * Converts a {@link Color} to its hexadecimal representation. + * + * @param color the {@link Color} to convert + * @return the hexadecimal string of the colour + */ public static String toHex(Color color) { return String.format("#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue()); } + /** + * Converts a {@link Color} to HSL (hue, saturation, lightness). + * + * @param color the {@link Color} to convert + * @return a float array representing the HSL values + */ public static float[] rgbToHsl(Color color) { float r = color.getRed() / 255f; float g = color.getGreen() / 255f; @@ -32,6 +47,12 @@ public static float[] rgbToHsl(Color color) { return new float[]{ h, s, l }; } + /** + * Converts HSL values to a {@link ColorRGB} object. + * + * @param hsl a float array representing HSL values + * @return a {@link ColorRGB} object given the HSL values + */ public static ColorRGB hslToRgb(float[] hsl) { float h = hsl[0], s = hsl[1], l = hsl[2]; float r, g, b; @@ -50,6 +71,14 @@ public static ColorRGB hslToRgb(float[] hsl) { return ColorRGB.fromRGBA(red, green, blue, 255); } + /** + * Helper method to convert hue values to RGB. + * + * @param p intermediate value + * @param q intermediate value + * @param t hue offset + * @return the calculated RGB value + */ private static float hueToRgb(float p, float q, float t) { if (t < 0f) t += 1f; @@ -64,6 +93,14 @@ private static float hueToRgb(float p, float q, float t) { return p; } + /** + * Blends two {@link Color}s based on an amount from 0 to 100. + * + * @param c1 the first {@link Color} + * @param c2 the second {@link Color} + * @param amount the percentage amount to blend the colours (0 - 100) + * @return the blended colour + */ public static Color blendColors(Color c1, Color c2, double amount) { amount = Math.max(0, Math.min(100, amount)); amount /= 100.0; @@ -74,6 +111,12 @@ public static Color blendColors(Color c1, Color c2, double amount) { return ColorRGB.fromRGBA(r, g, b, a); } + /** + * Calculates the complement of a {@link Color}. + * + * @param color the {@link Color} to complement + * @return the complementary colour + */ public static Color complementColor(Color color) { int r = 255 - color.getRed(); int g = 255 - color.getGreen(); @@ -81,12 +124,25 @@ public static Color complementColor(Color color) { return ColorRGB.fromRGBA(r, g, b, color.getAlpha()); } + /** + * Calculates the complement of a {@link Color} using HSL adjustments. + * + * @param color the {@link Color} to complement + * @return the complementary colour + */ public static Color complementColorHSL(Color color) { float[] hsl = rgbToHsl(color); hsl[0] = (hsl[0] + 0.5f) % 1f; return hslToRgb(hsl); } + /** + * Shades a {@link Color} by a given amount from 1 to 100. + * + * @param color the {@link Color} to shade + * @param amount the amount to shade the colour by (1 - 100) + * @return the shaded colour + */ public static ColorRGB shadeColor(Color color, int amount) { amount = Math.max(1, Math.min(100, amount)); double factor = (100 - amount) / 100.0; @@ -96,6 +152,13 @@ public static ColorRGB shadeColor(Color color, int amount) { return ColorRGB.fromRGBA(r, g, b, color.getAlpha()); } + /** + * Shades a {@link Color} by a given amount from 1 to 100 using HSL adjustments. + * + * @param color the {@link Color} to shade using HSL adjustments + * @param amount the amount to shade the colour by (1 - 100) + * @return the shaded colour + */ public static ColorRGB shadeColorHSL(Color color, int amount) { amount = Math.max(1, Math.min(100, amount)); float[] hsl = rgbToHsl(color); @@ -103,6 +166,13 @@ public static ColorRGB shadeColorHSL(Color color, int amount) { return hslToRgb(hsl); } + /** + * Tints a {@link Color} by a given amount from 1 to 100. + * + * @param color the {@link Color} to tint + * @param amount the amount to tint the colour by (1 - 100) + * @return the tinted colour + */ public static ColorRGB tintColor(Color color, int amount) { amount = Math.max(1, Math.min(100, amount)); double factor = amount / 100.0; @@ -112,6 +182,13 @@ public static ColorRGB tintColor(Color color, int amount) { return ColorRGB.fromRGBA(r, g, b, color.getAlpha()); } + /** + * Tints a {@link Color} by a given amount from 1 to 100 using HSL adjustments. + * + * @param color the {@link Color} to tint using HSL adjustments + * @param amount the amount to tint the colour by (1 - 100) + * @return the tinted colour + */ public static ColorRGB tintColorHSL(Color color, int amount) { amount = Math.max(1, Math.min(100, amount)); float[] hsl = rgbToHsl(color); @@ -120,6 +197,13 @@ public static ColorRGB tintColorHSL(Color color, int amount) { return hslToRgb(hsl); } + /** + * Rotates the hue of a {@link Color} by a given degree. + * + * @param color the {@link Color} to rotate the hue of + * @param degrees the number of degrees to rotate the hue by + * @return the hue-rotated colour + */ public static Color rotateHue(Color color, int degrees) { float[] hsl = rgbToHsl(color); hsl[0] = (hsl[0] + degrees / 360f) % 1f; @@ -128,6 +212,14 @@ public static Color rotateHue(Color color, int degrees) { return hslToRgb(hsl); } + /** + * Adjusts the brightness of a {@link Color} by an amount from -100 to 100. + * This is similar to shading and tinting, but is slightly different. + * + * @param color the {@link Color} to adjust the brightness of + * @param amount the amount to adjust the brightness by (-100 - 100) + * @return the brightness-adjusted colour + */ public static ColorRGB adjustBrightness(Color color, int amount) { amount = Math.max(-100, Math.min(100, amount)); float[] hsb = rgbToHsb(color); @@ -137,6 +229,13 @@ public static ColorRGB adjustBrightness(Color color, int amount) { return hsbToRgb(hsb); } + /** + * Converts a {@link Color} to HSB (hue, saturation, brightness). + * This is different to {@link #rgbToHsl(Color)}. + * + * @param color the {@link Color} to convert + * @return a float array representing the HSB values + */ private static float[] rgbToHsb(Color color) { float r = color.getRed() / 255f; float g = color.getGreen() / 255f; @@ -162,6 +261,13 @@ private static float[] rgbToHsb(Color color) { return new float[]{ h, s, v }; } + /** + * Converts HSB values to a {@link ColorRGB} object. + * This is different to {@link #hslToRgb(float[])}. + * + * @param hsb a float array representing HSB values + * @return a {@link ColorRGB} object given the HSB values + */ private static ColorRGB hsbToRgb(float[] hsb) { float h = hsb[0], s = hsb[1], v = hsb[2]; float r = 0f, g = 0f, b = 0f; @@ -190,11 +296,23 @@ private static ColorRGB hsbToRgb(float[] hsb) { return ColorRGB.fromRGBA(red, green, blue, 255); } + /** + * Converts a {@link Color} to its grayscale equivalent. + * + * @param color the {@link Color} to convert to grayscale + * @return the colour's grayscale equivalent + */ public static ColorRGB toGrayscale(Color color) { int gray = (int)(0.299 * color.getRed() + 0.587 * color.getGreen() + 0.114 * color.getBlue()); return ColorRGB.fromRGBA(gray, gray, gray, color.getAlpha()); } + /** + * Converts a {@link Color} to its sepiatone equivalent. + * + * @param color the {@link Color} to convert to sepiatone + * @return the colour's sepiatone equivalent + */ public static ColorRGB toSepia(Color color) { int r = color.getRed(); int g = color.getGreen(); @@ -208,6 +326,13 @@ public static ColorRGB toSepia(Color color) { return ColorRGB.fromRGBA(tr, tg, tb, color.getAlpha()); } + /** + * Adjusts the temperature of a {@link Color} by changing the red and blue channel values. + * + * @param color the {@link Color} to adjust the temperature of + * @param amount the amount to adjust the temperature by (-255 - 255) + * @return the temperature-adjusted colour + */ public static ColorRGB adjustTemperature(Color color, int amount) { int r = color.getRed() + amount; int b = color.getBlue() - amount; From e1f6364c52037b33381bfa990b5cabc27240fd32 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Mon, 9 Dec 2024 19:28:25 +1000 Subject: [PATCH 18/39] Changes all 'colour's to 'color's and adds better descriptions and examples --- src/main/java/ch/njol/skript/Skript.java | 6 ++---- .../ColourUtils.java => colors/ColorUtils.java} | 4 ++-- .../skript/misc/{colours => colors}/ExprBlend.java | 4 ++-- .../ExprComplementaryColor.java} | 10 +++++----- .../skript/misc/{colours => colors}/ExprHex.java | 4 ++-- 5 files changed, 13 insertions(+), 15 deletions(-) rename src/main/java/org/skriptlang/skript/misc/{colours/ColourUtils.java => colors/ColorUtils.java} (99%) rename src/main/java/org/skriptlang/skript/misc/{colours => colors}/ExprBlend.java (95%) rename src/main/java/org/skriptlang/skript/misc/{colours/ExprComplement.java => colors/ExprComplementaryColor.java} (78%) rename src/main/java/org/skriptlang/skript/misc/{colours => colors}/ExprHex.java (86%) diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index da5f335aff4..e2772fe84c6 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -29,7 +29,6 @@ import ch.njol.skript.lang.Trigger; import ch.njol.skript.lang.TriggerItem; import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.localization.ArgsMessage; import ch.njol.skript.localization.Language; import ch.njol.skript.localization.Message; import ch.njol.skript.localization.PluralizingArgsMessage; @@ -71,7 +70,6 @@ import com.google.gson.Gson; import org.bstats.bukkit.Metrics; -import org.bstats.charts.SimplePie; import org.bukkit.*; import org.bukkit.command.CommandSender; import org.bukkit.command.PluginCommand; @@ -106,7 +104,7 @@ import org.skriptlang.skript.lang.script.Script; import org.skriptlang.skript.lang.structure.Structure; import org.skriptlang.skript.lang.structure.StructureInfo; -import org.skriptlang.skript.misc.colours.ColourModule; +import org.skriptlang.skript.misc.colors.ColorModule; import java.io.File; import java.io.IOException; @@ -538,7 +536,7 @@ public void onEnable() { BreedingModule.load(); DisplayModule.load(); InputModule.load(); - ColourModule.load(); + ColorModule.load(); } catch (final Exception e) { exception(e, "Could not load required .class files: " + e.getLocalizedMessage()); setEnabled(false); diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java b/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java similarity index 99% rename from src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java rename to src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java index 871c52d0af1..46c773d9069 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ColourUtils.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java @@ -1,4 +1,4 @@ -package org.skriptlang.skript.misc.colours; +package org.skriptlang.skript.misc.colors; import ch.njol.skript.util.Color; import ch.njol.skript.util.ColorRGB; @@ -6,7 +6,7 @@ /** * Utility class for colour manipulation and conversion. */ -public class ColourUtils { +public class ColorUtils { /** * Converts a {@link Color} to its hexadecimal representation. diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java b/src/main/java/org/skriptlang/skript/misc/colors/ExprBlend.java similarity index 95% rename from src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java rename to src/main/java/org/skriptlang/skript/misc/colors/ExprBlend.java index 053bdefe1e3..ceb89f838d6 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ExprBlend.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ExprBlend.java @@ -1,4 +1,4 @@ -package org.skriptlang.skript.misc.colours; +package org.skriptlang.skript.misc.colors; import ch.njol.skript.Skript; import ch.njol.skript.doc.Description; @@ -61,7 +61,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye for (Color colour : colours) { Color blended = colour; for (Color blendWith : blendWiths) { - blended = ColourUtils.blendColors(blended, blendWith, amount.doubleValue()); + blended = ColorUtils.blendColors(blended, blendWith, amount.doubleValue()); } blendedColours.add(blended); } diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java b/src/main/java/org/skriptlang/skript/misc/colors/ExprComplementaryColor.java similarity index 78% rename from src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java rename to src/main/java/org/skriptlang/skript/misc/colors/ExprComplementaryColor.java index 23022ad5f95..38d91f2f6b5 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ExprComplement.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ExprComplementaryColor.java @@ -1,4 +1,4 @@ -package org.skriptlang.skript.misc.colours; +package org.skriptlang.skript.misc.colors; import ch.njol.skript.doc.Description; import ch.njol.skript.doc.Examples; @@ -21,10 +21,10 @@ "set {_allComplements} to complementary colour of all colours" }) @Since("INSERT VERSION") -public class ExprComplement extends SimplePropertyExpression { +public class ExprComplementaryColor extends SimplePropertyExpression { static { - register(ExprComplement.class, Color.class, "[:hsl] complement[ary colo[u]r]", "colors"); + register(ExprComplementaryColor.class, Color.class, "[:hsl] complement[ary colo[u]r]", "colors"); } private boolean hsl; @@ -37,12 +37,12 @@ public boolean init(Expression[] expressions, int matchedPattern, Kleenean is @Override public @Nullable Color convert(Color from) { - return hsl ? ColourUtils.complementColorHSL(from) : ColourUtils.complementColor(from); + return hsl ? ColorUtils.complementColorHSL(from) : ColorUtils.complementColor(from); } @Override protected String getPropertyName() { - return "complement"; + return "complementary color"; } @Override diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ExprHex.java b/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java similarity index 86% rename from src/main/java/org/skriptlang/skript/misc/colours/ExprHex.java rename to src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java index 903c14c5040..d64d797f5ab 100644 --- a/src/main/java/org/skriptlang/skript/misc/colours/ExprHex.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java @@ -1,4 +1,4 @@ -package org.skriptlang.skript.misc.colours; +package org.skriptlang.skript.misc.colors; import ch.njol.skript.expressions.base.SimplePropertyExpression; import ch.njol.skript.util.Color; @@ -12,7 +12,7 @@ public class ExprHex extends SimplePropertyExpression { @Override public @Nullable String convert(Color from) { - return ColourUtils.toHex(from); + return ColorUtils.toHex(from); } @Override From ed49cfd0ec1f1f89c713fae51b02753f38760d3e Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Mon, 9 Dec 2024 19:28:31 +1000 Subject: [PATCH 19/39] Changes all 'colour's to 'color's and adds better descriptions and examples --- .../skript/misc/colors/ColorModule.java | 144 ++++++++++++++++++ .../skript/misc/colours/ColourModule.java | 110 ------------- 2 files changed, 144 insertions(+), 110 deletions(-) create mode 100644 src/main/java/org/skriptlang/skript/misc/colors/ColorModule.java delete mode 100644 src/main/java/org/skriptlang/skript/misc/colours/ColourModule.java diff --git a/src/main/java/org/skriptlang/skript/misc/colors/ColorModule.java b/src/main/java/org/skriptlang/skript/misc/colors/ColorModule.java new file mode 100644 index 00000000000..39eb6830336 --- /dev/null +++ b/src/main/java/org/skriptlang/skript/misc/colors/ColorModule.java @@ -0,0 +1,144 @@ +package org.skriptlang.skript.misc.colors; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.function.Functions; +import ch.njol.skript.lang.function.Parameter; +import ch.njol.skript.lang.function.SimpleJavaFunction; +import ch.njol.skript.lang.util.SimpleLiteral; +import ch.njol.skript.registrations.DefaultClasses; +import ch.njol.skript.util.Color; +import ch.njol.skript.util.ColorRGB; +import ch.njol.util.coll.CollectionUtils; + +import java.io.IOException; + +public class ColorModule { + + public static void load() throws IOException { + Skript.getAddonInstance().loadClasses("org.skriptlang.skript.misc", "colors"); + + Functions.registerFunction(new SimpleJavaFunction("shade", new Parameter[] { + new Parameter<>("color", DefaultClasses.COLOR, true, null), + new Parameter<>("amount", DefaultClasses.LONG, true, null), + new Parameter<>("hsl", DefaultClasses.BOOLEAN, true, new SimpleLiteral<>(false, true)) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color color = (Color) params[0][0]; + Long amount = (Long) params[1][0]; + boolean hsl = (Boolean) params[2][0]; + return CollectionUtils.array(hsl + ? ColorUtils.shadeColorHSL(color, amount.intValue()) + : ColorUtils.shadeColor(color, amount.intValue())); + } + }).description( + "Shades a given color by a given amount, with optional HSL-based shading.", + "The amount parameter ranges from 1 to 100, with lower values closer to the original color and higher values closer to black.", + "Inputs below 1 will default to shading by 1%." + ).examples( + "set {_darkRed} to shade(red, 10)", + "set {_darkerRed} to shade(red, 20)", + "", + "function shadeExample(colour: colour, hsl: boolean = false):", + "\tloop 100 times:", + "\t\tset {_hex} to hex code of shade({_colour}, loop-value, {_hsl})", + "\t\tsend formatted \"%loop-value%: %{_hex}%████\" to all players", + "\t\twait 1 tick" + ).since("INSERT VERSION"); + + Functions.registerFunction(new SimpleJavaFunction("tint", new Parameter[] { + new Parameter<>("color", DefaultClasses.COLOR, true, null), + new Parameter<>("amount", DefaultClasses.LONG, true, null), + new Parameter<>("hsl", DefaultClasses.BOOLEAN, true, new SimpleLiteral<>(false, true)) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color color = (Color) params[0][0]; + Long amount = (Long) params[1][0]; + boolean hsl = (Boolean) params[2][0]; + return CollectionUtils.array(hsl + ? ColorUtils.tintColorHSL(color, amount.intValue()) + : ColorUtils.tintColor(color, amount.intValue())); + } + }).description( + "Tints a given color by a given amount, with optional HSL-based shading.", + "The amount parameter ranges from 1 to 100, with lower values closer to the original color and higher values closer to white.", + "Inputs below 1 will default to tinting by 1%." + ).examples( + "set {_lightRed} to tint(red, 10)", + "set {_lighterRed} to tint(red, 20)", + "", + "function tintExample(colour: colour, hsl: boolean = false):", + "\tloop 100 times:", + "\t\tset {_hex} to hex code of tint({_colour}, loop-value, {_hsl})", + "\t\tsend formatted \"%loop-value%: %{_hex}%████\" to all players", + "\t\twait 1 tick" + ).since("INSERT VERSION"); + + Functions.registerFunction(new SimpleJavaFunction("colorBrightness", new Parameter[] { + new Parameter<>("color", DefaultClasses.COLOR, true, null), + new Parameter<>("amount", DefaultClasses.LONG, true, null) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color color = (Color) params[0][0]; + Long amount = (Long) params[1][0]; + return CollectionUtils.array(ColorUtils.adjustBrightness(color, amount.intValue())); + } + }).description( + "Adjusts the brightness of a color by a specified amount, ranging from -100 to 100.", + "Positive values increase brightness, with higher values approaching white, and negative values decrease brightness, with lower values approaching black.", + "Inputs beyond the range will be clamped to the nearest valid value." + ).examples( + "set {_brighterRed} to colorBrightness(red, 10)", + "set {_darkerRed} to colorBrightness(red, -10)", + "", + "function brightnessExample(colour: colour):", + "\tloop integers from -100 to 100:", + "\t\tset {_hex} to hex code of colourBrightness({_colour}, loop-value)", + "\t\tsend formatted \"%loop-value%: %{_hex}%████\" to all players", + "\t\twait 1 tick" + ).since("INSERT VERSION"); + + Functions.registerFunction(new SimpleJavaFunction("grayscale", new Parameter[] { + new Parameter<>("color", DefaultClasses.COLOR, true, null) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color color = (Color) params[0][0]; + return CollectionUtils.array(ColorUtils.toGrayscale(color)); + } + }).description( + "Converts a given color to its grayscale equivalent.", + "The resulting color retains its brightness but loses all hue, appearing as a shade of gray." + ).examples( + "set {_redButGrayscale} to grayscale(red)", + "", + "function grayscaleExample():", + "\tloop all colours:", + "\t\tset {_hex} to hex code of grayscale(loop-value)", + "\t\tsend formatted \"%loop-value%: %{_hex}%████\" to all players" + ).since("INSERT VERSION"); + + Functions.registerFunction(new SimpleJavaFunction("sepiatone", new Parameter[] { + new Parameter<>("color", DefaultClasses.COLOR, true, null) + }, DefaultClasses.COLOR, true) { + @Override + public ColorRGB[] executeSimple(Object[][] params) { + Color color = (Color) params[0][0]; + return CollectionUtils.array(ColorUtils.toSepia(color)); + } + }).description( + "Converts a given color to its sepiatone equivalent.", + "The resulting color mimics the warm, brownish look of vintage photographs." + ).examples( + "set {_redButSepiatone} to sepiatone(red)", + "", + "function sepiatoneExample():", + "\tloop all colours:", + "\t\tset {_hex} to hex code of sepiatone(loop-value)", + "\t\tsend formatted \"%loop-value%: %{_hex}%████\" to all players" + ).since("INSERT VERSION"); + } + +} diff --git a/src/main/java/org/skriptlang/skript/misc/colours/ColourModule.java b/src/main/java/org/skriptlang/skript/misc/colours/ColourModule.java deleted file mode 100644 index 05a794c9e18..00000000000 --- a/src/main/java/org/skriptlang/skript/misc/colours/ColourModule.java +++ /dev/null @@ -1,110 +0,0 @@ -package org.skriptlang.skript.misc.colours; - -import ch.njol.skript.Skript; -import ch.njol.skript.lang.function.Functions; -import ch.njol.skript.lang.function.Parameter; -import ch.njol.skript.lang.function.SimpleJavaFunction; -import ch.njol.skript.lang.util.SimpleLiteral; -import ch.njol.skript.registrations.DefaultClasses; -import ch.njol.skript.util.Color; -import ch.njol.skript.util.ColorRGB; -import ch.njol.util.coll.CollectionUtils; - -import java.io.IOException; - -public class ColourModule { - - public static void load() throws IOException { - Skript.getAddonInstance().loadClasses("org.skriptlang.skript.misc", "colours"); - - Functions.registerFunction(new SimpleJavaFunction("shade", new Parameter[] { - new Parameter<>("colour", DefaultClasses.COLOR, true, null), - new Parameter<>("amount", DefaultClasses.LONG, true, new SimpleLiteral<>(1L, true)), - new Parameter<>("hsl", DefaultClasses.BOOLEAN, true, new SimpleLiteral<>(false, true)) - }, DefaultClasses.COLOR, true) { - @Override - public ColorRGB[] executeSimple(Object[][] params) { - Color colour = (Color) params[0][0]; - Long amount = (Long) params[1][0]; - boolean hsl = (Boolean) params[2][0]; - return CollectionUtils.array(hsl - ? ColourUtils.shadeColorHSL(colour, amount.intValue()) - : ColourUtils.shadeColor(colour, amount.intValue())); - } - }).description( - "Shades a given colour by a given amount. Optionally can use HSL methods to achieve this instead.", - "The amount should be from 0-100, with 0 doing nothing, and 100 shading it all the way to black." - ).examples( - "set {_darkRed} to shade(red, 10)", - "set {_darkerRed} to shade(red, 20)" - ).since("INSERT VERSION"); - - Functions.registerFunction(new SimpleJavaFunction("tint", new Parameter[] { - new Parameter<>("colour", DefaultClasses.COLOR, true, null), - new Parameter<>("amount", DefaultClasses.LONG, true, new SimpleLiteral<>(1L, true)), - new Parameter<>("hsl", DefaultClasses.BOOLEAN, true, new SimpleLiteral<>(false, true)) - }, DefaultClasses.COLOR, true) { - @Override - public ColorRGB[] executeSimple(Object[][] params) { - Color colour = (Color) params[0][0]; - Long amount = (Long) params[1][0]; - boolean hsl = (Boolean) params[2][0]; - return CollectionUtils.array(hsl - ? ColourUtils.tintColorHSL(colour, amount.intValue()) - : ColourUtils.tintColor(colour, amount.intValue())); - } - }).description( - "Tints a given colour by a given amount. Optionally can use HSL methods to achieve this instead.", - "The amount should be from 0-100, with 0 doing nothing, and 100 tinting it all the way to white." - ).examples( - "set {_lightRed} to tint(red, 10)", - "set {_lighterRed} to tint(red, 20)" - ).since("INSERT VERSION"); - - Functions.registerFunction(new SimpleJavaFunction("colourBrightness", new Parameter[] { - new Parameter<>("colour", DefaultClasses.COLOR, true, null), - new Parameter<>("amount", DefaultClasses.LONG, true, null) - }, DefaultClasses.COLOR, true) { - @Override - public ColorRGB[] executeSimple(Object[][] params) { - Color colour = (Color) params[0][0]; - Long amount = (Long) params[1][0]; - return CollectionUtils.array(ColourUtils.adjustBrightness(colour, amount.intValue())); - } - }).description( - "Adjusts the brightness of a colour by a given amount from -100 to 100." - ).examples( - "set {_brighterRed} to colourBrightness(red, 10)", - "set {_darkerRed} to colourBrightness(red, -10)" - ).since("INSERT VERSION"); - - Functions.registerFunction(new SimpleJavaFunction("grayscale", new Parameter[] { - new Parameter<>("colour", DefaultClasses.COLOR, true, null) - }, DefaultClasses.COLOR, true) { - @Override - public ColorRGB[] executeSimple(Object[][] params) { - Color colour = (Color) params[0][0]; - return CollectionUtils.array(ColourUtils.toGrayscale(colour)); - } - }).description( - "Returns a colour converted to grayscale." - ).examples( - "set {_redButGrayscale} to grayscale(red)" - ).since("INSERT VERSION"); - - Functions.registerFunction(new SimpleJavaFunction("sepiatone", new Parameter[] { - new Parameter<>("colour", DefaultClasses.COLOR, true, null) - }, DefaultClasses.COLOR, true) { - @Override - public ColorRGB[] executeSimple(Object[][] params) { - Color colour = (Color) params[0][0]; - return CollectionUtils.array(ColourUtils.toSepia(colour)); - } - }).description( - "Returns a colour converted to sepiatone." - ).examples( - "set {_redButSepiatone} to sepiatone(red)" - ).since("INSERT VERSION"); - } - -} From a8758b35d12cbe4d59186169c09d2f53e0c7a02c Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Mon, 9 Dec 2024 20:00:03 +1000 Subject: [PATCH 20/39] Moves hex conversion to Color class in lieu of static util method and some cleanup --- src/main/java/ch/njol/skript/util/Color.java | 50 ++++++++++--------- .../java/ch/njol/skript/util/ColorRGB.java | 13 +++-- .../java/ch/njol/skript/util/SkriptColor.java | 48 ++++++------------ .../skript/misc/colors/ColorUtils.java | 43 +++++++--------- .../skript/misc/colors/ExprHex.java | 2 +- 5 files changed, 68 insertions(+), 88 deletions(-) diff --git a/src/main/java/ch/njol/skript/util/Color.java b/src/main/java/ch/njol/skript/util/Color.java index 127e809652f..ef1587c2b2b 100644 --- a/src/main/java/ch/njol/skript/util/Color.java +++ b/src/main/java/ch/njol/skript/util/Color.java @@ -1,21 +1,3 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.util; import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable; @@ -25,28 +7,48 @@ public interface Color extends YggdrasilExtendedSerializable { /** - * Gets Bukkit color representing this color. - * @return Bukkit color. + * @return The Bukkit color representing this color. */ org.bukkit.Color asBukkitColor(); /** - * @return The alpha component of this color. + * @return The hexadecimal code representing this color. Can be used to color text. + */ + default String getHex() { + return String.format("#%02X%02X%02X", getRed(), getGreen(), getBlue()); + } + + /** + * @return The hexadecimal code representing this color, including the alpha channel. Cannot be used to color text. + */ + default String getFullHex() { + return String.format("#%02X%02X%02X%02X", getAlpha(), getRed(), getGreen(), getBlue()); + } + + /** + * @return The integer representing this color. + */ + default int asInt() { + return (getAlpha() << 24) | (getRed() << 16) | (getGreen() << 8) | getBlue(); + } + + /** + * @return The alpha channel of this color. */ int getAlpha(); /** - * @return The red component of this color. + * @return The red channel of this color. */ int getRed(); /** - * @return The green component of this color. + * @return The green channel of this color. */ int getGreen(); /** - * @return The blue component of this color. + * @return The blue channel of this color. */ int getBlue(); diff --git a/src/main/java/ch/njol/skript/util/ColorRGB.java b/src/main/java/ch/njol/skript/util/ColorRGB.java index 180b26e7c81..070bf5df126 100644 --- a/src/main/java/ch/njol/skript/util/ColorRGB.java +++ b/src/main/java/ch/njol/skript/util/ColorRGB.java @@ -22,6 +22,7 @@ public class ColorRGB implements Color { private org.bukkit.Color bukkit; private @Nullable DyeColor dye; + private final int alpha, red, green, blue; /** * Subject to being private in the future. Use {@link #fromRGB(int, int, int)} @@ -45,6 +46,10 @@ public ColorRGB(int red, int green, int blue) { public ColorRGB(org.bukkit.Color bukkit) { this.dye = DyeColor.getByColor(bukkit); this.bukkit = bukkit; + this.alpha = bukkit.getAlpha(); + this.red = bukkit.getRed(); + this.green = bukkit.getGreen(); + this.blue = bukkit.getBlue(); } /** @@ -87,22 +92,22 @@ public ColorRGB(org.bukkit.Color bukkit) { @Override public int getAlpha() { - return bukkit.getAlpha(); + return alpha; } @Override public int getRed() { - return bukkit.getRed(); + return red; } @Override public int getGreen() { - return bukkit.getGreen(); + return green; } @Override public int getBlue() { - return bukkit.getBlue(); + return blue; } @Override diff --git a/src/main/java/ch/njol/skript/util/SkriptColor.java b/src/main/java/ch/njol/skript/util/SkriptColor.java index 598fc66b6fa..b1294a5feab 100644 --- a/src/main/java/ch/njol/skript/util/SkriptColor.java +++ b/src/main/java/ch/njol/skript/util/SkriptColor.java @@ -1,21 +1,3 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.util; import ch.njol.skript.localization.Adjective; @@ -81,12 +63,16 @@ public enum SkriptColor implements Color { private ChatColor chat; private DyeColor dye; - @Nullable - private Adjective adjective; + private @Nullable Adjective adjective; + private final int alpha, red, green, blue; SkriptColor(DyeColor dye, ChatColor chat) { this.chat = chat; this.dye = dye; + this.alpha = dye.getColor().getAlpha(); + this.red = dye.getColor().getRed(); + this.green = dye.getColor().getGreen(); + this.blue = dye.getColor().getBlue(); } @Override @@ -96,22 +82,22 @@ public org.bukkit.Color asBukkitColor() { @Override public int getAlpha() { - return dye.getColor().getAlpha(); + return alpha; } @Override public int getRed() { - return dye.getColor().getRed(); + return red; } @Override public int getGreen() { - return dye.getColor().getGreen(); + return green; } @Override public int getBlue() { - return dye.getColor().getBlue(); + return blue; } @Override @@ -142,9 +128,8 @@ public void deserialize(@NotNull Fields fields) throws StreamCorruptedException public String getFormattedChat() { return "" + chat; } - - @Nullable - public Adjective getAdjective() { + + public @Nullable Adjective getAdjective() { return adjective; } @@ -171,8 +156,7 @@ private void setAdjective(@Nullable Adjective adjective) { * @param name The String name of the color defined by Skript's .lang files. * @return Skript Color if matched up with the defined name */ - @Nullable - public static SkriptColor fromName(String name) { + public static @Nullable SkriptColor fromName(String name) { return names.get(name); } @@ -206,8 +190,7 @@ public static SkriptColor fromBukkitColor(org.bukkit.Color color) { * @return Skript Color if matched up with the defined short */ @Deprecated - @Nullable - public static SkriptColor fromDyeData(short data) { + public static @Nullable SkriptColor fromDyeData(short data) { if (data < 0 || data >= 16) return null; @@ -226,8 +209,7 @@ public static SkriptColor fromDyeData(short data) { * @return Skript Color if matched up with the defined short */ @Deprecated - @Nullable - public static SkriptColor fromWoolData(short data) { + public static @Nullable SkriptColor fromWoolData(short data) { if (data < 0 || data >= 16) return null; for (SkriptColor color : colors) { diff --git a/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java b/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java index 46c773d9069..40013c0cca1 100644 --- a/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java @@ -2,29 +2,20 @@ import ch.njol.skript.util.Color; import ch.njol.skript.util.ColorRGB; +import org.jetbrains.annotations.NotNull; /** * Utility class for colour manipulation and conversion. */ public class ColorUtils { - /** - * Converts a {@link Color} to its hexadecimal representation. - * - * @param color the {@link Color} to convert - * @return the hexadecimal string of the colour - */ - public static String toHex(Color color) { - return String.format("#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue()); - } - /** * Converts a {@link Color} to HSL (hue, saturation, lightness). * * @param color the {@link Color} to convert * @return a float array representing the HSL values */ - public static float[] rgbToHsl(Color color) { + public static float @NotNull [] rgbToHsl(@NotNull Color color) { float r = color.getRed() / 255f; float g = color.getGreen() / 255f; float b = color.getBlue() / 255f; @@ -53,7 +44,7 @@ public static float[] rgbToHsl(Color color) { * @param hsl a float array representing HSL values * @return a {@link ColorRGB} object given the HSL values */ - public static ColorRGB hslToRgb(float[] hsl) { + public static @NotNull ColorRGB hslToRgb(float @NotNull [] hsl) { float h = hsl[0], s = hsl[1], l = hsl[2]; float r, g, b; if (s == 0f) { @@ -101,7 +92,7 @@ private static float hueToRgb(float p, float q, float t) { * @param amount the percentage amount to blend the colours (0 - 100) * @return the blended colour */ - public static Color blendColors(Color c1, Color c2, double amount) { + public static @NotNull Color blendColors(@NotNull Color c1, @NotNull Color c2, double amount) { amount = Math.max(0, Math.min(100, amount)); amount /= 100.0; int r = (int) (c1.getRed() * (1 - amount) + c2.getRed() * amount); @@ -117,7 +108,7 @@ public static Color blendColors(Color c1, Color c2, double amount) { * @param color the {@link Color} to complement * @return the complementary colour */ - public static Color complementColor(Color color) { + public static @NotNull Color complementColor(@NotNull Color color) { int r = 255 - color.getRed(); int g = 255 - color.getGreen(); int b = 255 - color.getBlue(); @@ -130,7 +121,7 @@ public static Color complementColor(Color color) { * @param color the {@link Color} to complement * @return the complementary colour */ - public static Color complementColorHSL(Color color) { + public static @NotNull Color complementColorHSL(@NotNull Color color) { float[] hsl = rgbToHsl(color); hsl[0] = (hsl[0] + 0.5f) % 1f; return hslToRgb(hsl); @@ -143,7 +134,7 @@ public static Color complementColorHSL(Color color) { * @param amount the amount to shade the colour by (1 - 100) * @return the shaded colour */ - public static ColorRGB shadeColor(Color color, int amount) { + public static @NotNull ColorRGB shadeColor(@NotNull Color color, int amount) { amount = Math.max(1, Math.min(100, amount)); double factor = (100 - amount) / 100.0; int r = (int) (color.getRed() * factor); @@ -159,7 +150,7 @@ public static ColorRGB shadeColor(Color color, int amount) { * @param amount the amount to shade the colour by (1 - 100) * @return the shaded colour */ - public static ColorRGB shadeColorHSL(Color color, int amount) { + public static @NotNull ColorRGB shadeColorHSL(@NotNull Color color, int amount) { amount = Math.max(1, Math.min(100, amount)); float[] hsl = rgbToHsl(color); hsl[2] *= (100 - amount) / 100f; @@ -173,7 +164,7 @@ public static ColorRGB shadeColorHSL(Color color, int amount) { * @param amount the amount to tint the colour by (1 - 100) * @return the tinted colour */ - public static ColorRGB tintColor(Color color, int amount) { + public static @NotNull ColorRGB tintColor(@NotNull Color color, int amount) { amount = Math.max(1, Math.min(100, amount)); double factor = amount / 100.0; int r = (int) (color.getRed() + (255 - color.getRed()) * factor); @@ -189,7 +180,7 @@ public static ColorRGB tintColor(Color color, int amount) { * @param amount the amount to tint the colour by (1 - 100) * @return the tinted colour */ - public static ColorRGB tintColorHSL(Color color, int amount) { + public static @NotNull ColorRGB tintColorHSL(@NotNull Color color, int amount) { amount = Math.max(1, Math.min(100, amount)); float[] hsl = rgbToHsl(color); hsl[2] += (1f - hsl[2]) * (amount / 100f); @@ -204,7 +195,7 @@ public static ColorRGB tintColorHSL(Color color, int amount) { * @param degrees the number of degrees to rotate the hue by * @return the hue-rotated colour */ - public static Color rotateHue(Color color, int degrees) { + public static @NotNull Color rotateHue(@NotNull Color color, int degrees) { float[] hsl = rgbToHsl(color); hsl[0] = (hsl[0] + degrees / 360f) % 1f; if (hsl[0] < 0f) @@ -220,7 +211,7 @@ public static Color rotateHue(Color color, int degrees) { * @param amount the amount to adjust the brightness by (-100 - 100) * @return the brightness-adjusted colour */ - public static ColorRGB adjustBrightness(Color color, int amount) { + public static @NotNull ColorRGB adjustBrightness(@NotNull Color color, int amount) { amount = Math.max(-100, Math.min(100, amount)); float[] hsb = rgbToHsb(color); float factor = amount / 100f; @@ -236,7 +227,7 @@ public static ColorRGB adjustBrightness(Color color, int amount) { * @param color the {@link Color} to convert * @return a float array representing the HSB values */ - private static float[] rgbToHsb(Color color) { + private static float @NotNull [] rgbToHsb(@NotNull Color color) { float r = color.getRed() / 255f; float g = color.getGreen() / 255f; float b = color.getBlue() / 255f; @@ -268,7 +259,7 @@ private static float[] rgbToHsb(Color color) { * @param hsb a float array representing HSB values * @return a {@link ColorRGB} object given the HSB values */ - private static ColorRGB hsbToRgb(float[] hsb) { + private static @NotNull ColorRGB hsbToRgb(float @NotNull [] hsb) { float h = hsb[0], s = hsb[1], v = hsb[2]; float r = 0f, g = 0f, b = 0f; int i = (int) (h * 6f); @@ -302,7 +293,7 @@ private static ColorRGB hsbToRgb(float[] hsb) { * @param color the {@link Color} to convert to grayscale * @return the colour's grayscale equivalent */ - public static ColorRGB toGrayscale(Color color) { + public static @NotNull ColorRGB toGrayscale(@NotNull Color color) { int gray = (int)(0.299 * color.getRed() + 0.587 * color.getGreen() + 0.114 * color.getBlue()); return ColorRGB.fromRGBA(gray, gray, gray, color.getAlpha()); } @@ -313,7 +304,7 @@ public static ColorRGB toGrayscale(Color color) { * @param color the {@link Color} to convert to sepiatone * @return the colour's sepiatone equivalent */ - public static ColorRGB toSepia(Color color) { + public static @NotNull ColorRGB toSepia(@NotNull Color color) { int r = color.getRed(); int g = color.getGreen(); int b = color.getBlue(); @@ -333,7 +324,7 @@ public static ColorRGB toSepia(Color color) { * @param amount the amount to adjust the temperature by (-255 - 255) * @return the temperature-adjusted colour */ - public static ColorRGB adjustTemperature(Color color, int amount) { + public static @NotNull ColorRGB adjustTemperature(@NotNull Color color, int amount) { int r = color.getRed() + amount; int b = color.getBlue() - amount; r = Math.max(0, Math.min(255, r)); diff --git a/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java b/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java index d64d797f5ab..7497a4fe88f 100644 --- a/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java @@ -12,7 +12,7 @@ public class ExprHex extends SimplePropertyExpression { @Override public @Nullable String convert(Color from) { - return ColorUtils.toHex(from); + return from.getHex(); } @Override From 99fb214152d4932d314aa3b7693e878a4a3baca3 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Mon, 9 Dec 2024 22:30:51 +1000 Subject: [PATCH 21/39] Adds some comments and changes variable names to not be just single characters --- .../skript/misc/colors/ColorUtils.java | 193 +++++++++++------- 1 file changed, 114 insertions(+), 79 deletions(-) diff --git a/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java b/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java index 40013c0cca1..a3a8903877d 100644 --- a/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ColorUtils.java @@ -16,26 +16,36 @@ public class ColorUtils { * @return a float array representing the HSL values */ public static float @NotNull [] rgbToHsl(@NotNull Color color) { - float r = color.getRed() / 255f; - float g = color.getGreen() / 255f; - float b = color.getBlue() / 255f; - float max = Math.max(r, Math.max(g, b)); - float min = Math.min(r, Math.min(g, b)); - float h, s, l = (max + min) / 2f; + // normalize rgb to between 0 and 1 + float red = color.getRed() / 255f; + float green = color.getGreen() / 255f; + float blue = color.getBlue() / 255f; + + float max = Math.max(red, Math.max(green, blue)); + float min = Math.min(red, Math.min(green, blue)); + + float hue, saturation, lightness = (max + min) / 2f; // lightness = midpoint of max and min + if (max == min) { - h = s = 0f; + // achromatic (no hue or saturation) + hue = saturation = 0f; } else { float delta = max - min; - s = l > 0.5f ? delta / (2f - max - min) : delta / (max + min); - if (max == r) { - h = ((g - b) / delta + (g < b ? 6f : 0f)) / 6f; - } else if (max == g) { - h = ((b - r) / delta + 2f) / 6f; + + // saturation depends on lightness (scales differently if lightness > 0.5) + saturation = lightness > 0.5f ? delta / (2f - max - min) : delta / (max + min); + + // determine hue by which channel is max + // normalize hue by converting from a 0-360 scale to a 0-1 scale by dividing by 6 + if (max == red) { + hue = ((green - blue) / delta + (green < blue ? 6f : 0f)) / 6f; + } else if (max == green) { + hue = ((blue - red) / delta + 2f) / 6f; } else { - h = ((r - g) / delta + 4f) / 6f; + hue = ((red - green) / delta + 4f) / 6f; } } - return new float[]{ h, s, l }; + return new float[]{ hue, saturation, lightness }; } /** @@ -45,43 +55,48 @@ public class ColorUtils { * @return a {@link ColorRGB} object given the HSL values */ public static @NotNull ColorRGB hslToRgb(float @NotNull [] hsl) { - float h = hsl[0], s = hsl[1], l = hsl[2]; - float r, g, b; - if (s == 0f) { - r = g = b = l; + float hue = hsl[0], saturation = hsl[1], lightness = hsl[2]; + float red, green, blue; + if (saturation == 0f) { + // achromatic i.e. gray (all channels equal to lightness) + red = green = blue = lightness; } else { - float q = l < 0.5f ? l * (1f + s) : l + s - l * s; - float p = 2f * l - q; - r = hueToRgb(p, q, h + 1f / 3f); - g = hueToRgb(p, q, h); - b = hueToRgb(p, q, h - 1f / 3f); + // higherBound and lowerBound define two boundary colors + float lowerBound = lightness < 0.5f ? lightness * (1f + saturation) : lightness + saturation - lightness * saturation; + float higherBound = 2f * lightness - lowerBound; + red = hueToRgb(higherBound, lowerBound, hue + 1f / 3f); + green = hueToRgb(higherBound, lowerBound, hue); + blue = hueToRgb(higherBound, lowerBound, hue - 1f / 3f); } - int red = Math.round(r * 255f); - int green = Math.round(g * 255f); - int blue = Math.round(b * 255f); - return ColorRGB.fromRGBA(red, green, blue, 255); + int r = Math.round(red * 255f); + int g = Math.round(green * 255f); + int b = Math.round(blue * 255f); + return ColorRGB.fromRGBA(r, g, b, 255); } /** * Helper method to convert hue values to RGB. * - * @param p intermediate value - * @param q intermediate value - * @param t hue offset + * @param lowerBound intermediate value + * @param higherBound intermediate value + * @param hueOffset hue offset * @return the calculated RGB value */ - private static float hueToRgb(float p, float q, float t) { - if (t < 0f) - t += 1f; - if (t > 1f) - t -= 1f; - if (t < 1f / 6f) - return p + (q - p) * 6f * t; - if (t < 1f / 2f) - return q; - if (t < 2f / 3f) - return p + (q - p) * (2f / 3f - t) * 6f; - return p; + private static float hueToRgb(float lowerBound, float higherBound, float hueOffset) { + // wrap hueOffset if out of 0-1 range + if (hueOffset < 0f) + hueOffset += 1f; + if (hueOffset > 1f) + hueOffset -= 1f; + + // depending on hueOffset, interpolate between lowerBound and higherBound + if (hueOffset < 1f / 6f) + return lowerBound + (higherBound - lowerBound) * 6f * hueOffset; + if (hueOffset < 1f / 2f) + return higherBound; + if (hueOffset < 2f / 3f) + return lowerBound + (higherBound - lowerBound) * (2f / 3f - hueOffset) * 6f; + return lowerBound; } /** @@ -93,8 +108,10 @@ private static float hueToRgb(float p, float q, float t) { * @return the blended colour */ public static @NotNull Color blendColors(@NotNull Color c1, @NotNull Color c2, double amount) { - amount = Math.max(0, Math.min(100, amount)); - amount /= 100.0; + // amount is a percentage (clamp then normalize to between 0 and 1) + amount = Math.max(0, Math.min(100, amount)) / 100.0; + + // linearly interpolate each channel int r = (int) (c1.getRed() * (1 - amount) + c2.getRed() * amount); int g = (int) (c1.getGreen() * (1 - amount) + c2.getGreen() * amount); int b = (int) (c1.getBlue() * (1 - amount) + c2.getBlue() * amount); @@ -109,6 +126,7 @@ private static float hueToRgb(float p, float q, float t) { * @return the complementary colour */ public static @NotNull Color complementColor(@NotNull Color color) { + // just invert each channel int r = 255 - color.getRed(); int g = 255 - color.getGreen(); int b = 255 - color.getBlue(); @@ -123,6 +141,7 @@ private static float hueToRgb(float p, float q, float t) { */ public static @NotNull Color complementColorHSL(@NotNull Color color) { float[] hsl = rgbToHsl(color); + // adding 0.5 flips hue by 180 (i.e. finds the complement) hsl[0] = (hsl[0] + 0.5f) % 1f; return hslToRgb(hsl); } @@ -135,6 +154,7 @@ private static float hueToRgb(float p, float q, float t) { * @return the shaded colour */ public static @NotNull ColorRGB shadeColor(@NotNull Color color, int amount) { + // reducing the channel values darkens the color amount = Math.max(1, Math.min(100, amount)); double factor = (100 - amount) / 100.0; int r = (int) (color.getRed() * factor); @@ -151,6 +171,7 @@ private static float hueToRgb(float p, float q, float t) { * @return the shaded colour */ public static @NotNull ColorRGB shadeColorHSL(@NotNull Color color, int amount) { + // reducing the lightness to shade amount = Math.max(1, Math.min(100, amount)); float[] hsl = rgbToHsl(color); hsl[2] *= (100 - amount) / 100f; @@ -165,6 +186,7 @@ private static float hueToRgb(float p, float q, float t) { * @return the tinted colour */ public static @NotNull ColorRGB tintColor(@NotNull Color color, int amount) { + // move each channel closer to 255 to lighten the colour amount = Math.max(1, Math.min(100, amount)); double factor = amount / 100.0; int r = (int) (color.getRed() + (255 - color.getRed()) * factor); @@ -181,6 +203,7 @@ private static float hueToRgb(float p, float q, float t) { * @return the tinted colour */ public static @NotNull ColorRGB tintColorHSL(@NotNull Color color, int amount) { + // increasing the lightness (towards 1) to tint amount = Math.max(1, Math.min(100, amount)); float[] hsl = rgbToHsl(color); hsl[2] += (1f - hsl[2]) * (amount / 100f); @@ -196,6 +219,7 @@ private static float hueToRgb(float p, float q, float t) { * @return the hue-rotated colour */ public static @NotNull Color rotateHue(@NotNull Color color, int degrees) { + // hue is a fraction of a circle, add (degrees/360) to rotate by that angle float[] hsl = rgbToHsl(color); hsl[0] = (hsl[0] + degrees / 360f) % 1f; if (hsl[0] < 0f) @@ -212,6 +236,7 @@ private static float hueToRgb(float p, float q, float t) { * @return the brightness-adjusted colour */ public static @NotNull ColorRGB adjustBrightness(@NotNull Color color, int amount) { + // adjust brightness by scaling brightness directly (shocking ik) amount = Math.max(-100, Math.min(100, amount)); float[] hsb = rgbToHsb(color); float factor = amount / 100f; @@ -228,28 +253,32 @@ private static float hueToRgb(float p, float q, float t) { * @return a float array representing the HSB values */ private static float @NotNull [] rgbToHsb(@NotNull Color color) { + // normalize rgb to between 0 and 1 float r = color.getRed() / 255f; float g = color.getGreen() / 255f; float b = color.getBlue() / 255f; + + // max defines brightness, delta defines saturation float max = Math.max(r, Math.max(g, b)); float min = Math.min(r, Math.min(g, b)); float delta = max - min; - float h = 0f; - float s = max == 0f ? 0f : delta / max; - float v = max; + float hue = 0f; + float saturation = max == 0f ? 0f : delta / max; + + // hue depends on which channel is max if (delta != 0f) { if (max == r) { - h = ((g - b) / delta) % 6f; + hue = ((g - b) / delta) % 6f; } else if (max == g) { - h = ((b - r) / delta) + 2f; + hue = ((b - r) / delta) + 2f; } else { - h = ((r - g) / delta) + 4f; + hue = ((r - g) / delta) + 4f; } - h *= 60f; - if (h < 0f) h += 360f; + hue *= 60f; + if (hue < 0f) hue += 360f; } - h /= 360f; - return new float[]{ h, s, v }; + hue /= 360f; + return new float[]{ hue, saturation, max}; } /** @@ -260,31 +289,34 @@ private static float hueToRgb(float p, float q, float t) { * @return a {@link ColorRGB} object given the HSB values */ private static @NotNull ColorRGB hsbToRgb(float @NotNull [] hsb) { - float h = hsb[0], s = hsb[1], v = hsb[2]; - float r = 0f, g = 0f, b = 0f; - int i = (int) (h * 6f); - float f = h * 6f - i; - float p = v * (1f - s); - float q = v * (1f - f * s); - float t = v * (1f - (1f - f) * s); - switch (i % 6) { + float hue = hsb[0], saturation = hsb[1], brightness = hsb[2]; + // determine hue sector and interpolate + float red = 0f, green = 0f, blue = 0f; + int hueSector = (int) (hue * 6f); + float sectorFraction = hue * 6f - hueSector; + float lowerBound = brightness * (1f - saturation); + float higherBound = brightness * (1f - sectorFraction * saturation); + float hueOffset = brightness * (1f - (1f - sectorFraction) * saturation); + + // assign rgb values based on sector + switch (hueSector % 6) { case 0: - r = v; g = t; b = p; break; + red = brightness; green = hueOffset; blue = lowerBound; break; case 1: - r = q; g = v; b = p; break; + red = higherBound; green = brightness; blue = lowerBound; break; case 2: - r = p; g = v; b = t; break; + red = lowerBound; green = brightness; blue = hueOffset; break; case 3: - r = p; g = q; b = v; break; + red = lowerBound; green = higherBound; blue = brightness; break; case 4: - r = t; g = p; b = v; break; + red = hueOffset; green = lowerBound; blue = brightness; break; case 5: - r = v; g = p; b = q; break; + red = brightness; green = lowerBound; blue = higherBound; break; } - int red = Math.round(r * 255f); - int green = Math.round(g * 255f); - int blue = Math.round(b * 255f); - return ColorRGB.fromRGBA(red, green, blue, 255); + int r = Math.round(red * 255f); + int g = Math.round(green * 255f); + int b = Math.round(blue * 255f); + return ColorRGB.fromRGBA(r, g, b, 255); } /** @@ -294,6 +326,7 @@ private static float hueToRgb(float p, float q, float t) { * @return the colour's grayscale equivalent */ public static @NotNull ColorRGB toGrayscale(@NotNull Color color) { + // weighted average simulates human perception int gray = (int)(0.299 * color.getRed() + 0.587 * color.getGreen() + 0.114 * color.getBlue()); return ColorRGB.fromRGBA(gray, gray, gray, color.getAlpha()); } @@ -305,16 +338,17 @@ private static float hueToRgb(float p, float q, float t) { * @return the colour's sepiatone equivalent */ public static @NotNull ColorRGB toSepia(@NotNull Color color) { + // standard sepia formula int r = color.getRed(); int g = color.getGreen(); int b = color.getBlue(); - int tr = (int) (0.393 * r + 0.769 * g + 0.189 * b); - int tg = (int) (0.349 * r + 0.686 * g + 0.168 * b); - int tb = (int) (0.272 * r + 0.534 * g + 0.131 * b); - tr = Math.min(255, tr); - tg = Math.min(255, tg); - tb = Math.min(255, tb); - return ColorRGB.fromRGBA(tr, tg, tb, color.getAlpha()); + int sepiaRed = (int) (0.393 * r + 0.769 * g + 0.189 * b); + int sepiaGreen = (int) (0.349 * r + 0.686 * g + 0.168 * b); + int sepiaBlue = (int) (0.272 * r + 0.534 * g + 0.131 * b); + sepiaRed = Math.min(255, sepiaRed); + sepiaGreen = Math.min(255, sepiaGreen); + sepiaBlue = Math.min(255, sepiaBlue); + return ColorRGB.fromRGBA(sepiaRed, sepiaGreen, sepiaBlue, color.getAlpha()); } /** @@ -325,6 +359,7 @@ private static float hueToRgb(float p, float q, float t) { * @return the temperature-adjusted colour */ public static @NotNull ColorRGB adjustTemperature(@NotNull Color color, int amount) { + // increasing red and decreasing blue 'warms' the color, opposite cools int r = color.getRed() + amount; int b = color.getBlue() - amount; r = Math.max(0, Math.min(255, r)); From b6b4deee4b2d9a1da3d6ea3393ff9b607979336f Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Wed, 11 Dec 2024 18:01:01 +1000 Subject: [PATCH 22/39] Adds doc annotations to ExprHex --- .../org/skriptlang/skript/misc/colors/ExprHex.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java b/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java index 7497a4fe88f..a990390e8a9 100644 --- a/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ExprHex.java @@ -1,9 +1,19 @@ package org.skriptlang.skript.misc.colors; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; import ch.njol.skript.expressions.base.SimplePropertyExpression; import ch.njol.skript.util.Color; import org.jetbrains.annotations.Nullable; +@Name("Hex Code") +@Description("Returns the hexadecimal code representing a given color(s).") +@Examples( + "send formatted \"<%hex code of shade(red, 50)%>slightly darker red\" to all players" +) +@Since("INSERT VERSION") public class ExprHex extends SimplePropertyExpression { static { From 641fa49d3c771a800be93e70cda3a98d8f5e9528 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Wed, 11 Dec 2024 18:02:06 +1000 Subject: [PATCH 23/39] Some small doc/pattern changes --- .../java/org/skriptlang/skript/misc/colors/ExprBlend.java | 5 +++-- .../skript/misc/colors/ExprComplementaryColor.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/skriptlang/skript/misc/colors/ExprBlend.java b/src/main/java/org/skriptlang/skript/misc/colors/ExprBlend.java index ceb89f838d6..067473b17d2 100644 --- a/src/main/java/org/skriptlang/skript/misc/colors/ExprBlend.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ExprBlend.java @@ -20,8 +20,9 @@ @Name("Blended Colours") @Description({ - "Returns the result of blending colours together. Optionally takes an amount to blend the colours by, which is", - "a number from 0 to 100. In that range, a 50 would be an expected equal blend of each colour (the default behaviour)." + "Returns the result of blending colours together. Optionally takes an amount to blend the colours by, which is" + + "a number from 0 to 100.", + "In that range, a 50 would be an expected equal blend of each colour (the default behaviour)." }) @Examples({ "set {_purple} to red blended with blue", diff --git a/src/main/java/org/skriptlang/skript/misc/colors/ExprComplementaryColor.java b/src/main/java/org/skriptlang/skript/misc/colors/ExprComplementaryColor.java index 38d91f2f6b5..eb2ebfd3c27 100644 --- a/src/main/java/org/skriptlang/skript/misc/colors/ExprComplementaryColor.java +++ b/src/main/java/org/skriptlang/skript/misc/colors/ExprComplementaryColor.java @@ -24,7 +24,7 @@ public class ExprComplementaryColor extends SimplePropertyExpression { static { - register(ExprComplementaryColor.class, Color.class, "[:hsl] complement[ary colo[u]r]", "colors"); + register(ExprComplementaryColor.class, Color.class, "[:hsl] complement[ary] colo[u]r[s]", "colors"); } private boolean hsl; From 7340a72e2fda7699bc8cfe49d0127dc3755508f1 Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Sat, 14 Dec 2024 22:56:16 +1000 Subject: [PATCH 24/39] First attempt at serialization of colors - Works for ColorRGBs but not SkriptColors --- .../skript/classes/data/SkriptClasses.java | 83 +++++++++++-------- src/main/java/ch/njol/skript/util/Color.java | 3 +- .../java/ch/njol/skript/util/ColorRGB.java | 24 +----- .../java/ch/njol/skript/util/SkriptColor.java | 28 +------ .../skript/misc/colors/ColorUtils.java | 14 ++++ 5 files changed, 68 insertions(+), 84 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java b/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java index c846b00e680..77c51ec4f5b 100644 --- a/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java @@ -40,7 +40,9 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.Nullable; +import org.skriptlang.skript.misc.colors.ColorUtils; +import java.io.NotSerializableException; import java.io.StreamCorruptedException; import java.util.Arrays; import java.util.Iterator; @@ -69,8 +71,7 @@ public SkriptClasses() {} .supplier(() -> (Iterator) Classes.getClassInfos().iterator()) .parser(new Parser() { @Override - @Nullable - public ClassInfo parse(final String s, final ParseContext context) { + public @Nullable ClassInfo parse(final String s, final ParseContext context) { return Classes.getClassInfoFromUserInput(Noun.stripIndefiniteArticle(s)); } @@ -119,10 +120,8 @@ protected ClassInfo deserialize(final Fields fields) throws StreamCorruptedExcep return ci; } -// return c.getCodeName(); @Override - @Nullable - public ClassInfo deserialize(final String s) { + public @Nullable ClassInfo deserialize(final String s) { return Classes.getClassInfoNoError(s); } @@ -144,8 +143,7 @@ public boolean mustSyncDeserialization() { .defaultExpression(new SimpleLiteral<>(WeatherType.CLEAR, true)) .parser(new Parser() { @Override - @Nullable - public WeatherType parse(final String s, final ParseContext context) { + public @Nullable WeatherType parse(final String s, final ParseContext context) { return WeatherType.parse(s); } @@ -156,7 +154,7 @@ public String toString(final WeatherType o, final int flags) { @Override public String toVariableNameString(final WeatherType o) { - return "" + o.name().toLowerCase(Locale.ENGLISH); + return o.name().toLowerCase(Locale.ENGLISH); } }) @@ -184,8 +182,7 @@ public String toVariableNameString(final WeatherType o) { .iterator()) .parser(new Parser() { @Override - @Nullable - public ItemType parse(final String s, final ParseContext context) { + public @Nullable ItemType parse(final String s, final ParseContext context) { return Aliases.parseItemType(s); } @@ -219,7 +216,7 @@ public String toVariableNameString(final ItemType t) { b.append(":" + ench.getLevel()); } } - return "" + b.toString(); + return b.toString(); } }) .cloner(ItemType::clone) @@ -239,8 +236,7 @@ public String toVariableNameString(final ItemType t) { .defaultExpression(new EventValueExpression<>(Time.class)) .parser(new Parser