From 463d3206426b7fdf3867fdf9f3a91f7192f1ed52 Mon Sep 17 00:00:00 2001 From: sovdee <10354869+sovdeeth@users.noreply.github.com> Date: Fri, 15 Nov 2024 01:06:39 -0500 Subject: [PATCH 1/3] argb expression + tests --- .../ch/njol/skript/expressions/ExprARGB.java | 78 +++++++++++++++++++ .../njol/skript/expressions/ExprColorOf.java | 2 +- src/main/java/ch/njol/skript/util/Color.java | 33 ++++++-- .../java/ch/njol/skript/util/ColorRGB.java | 32 +++++--- .../java/ch/njol/skript/util/SkriptColor.java | 43 ++++++---- .../tests/syntaxes/expressions/ExprARGB.sk | 20 +++++ 6 files changed, 177 insertions(+), 31 deletions(-) create mode 100644 src/main/java/ch/njol/skript/expressions/ExprARGB.java create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprARGB.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprARGB.java b/src/main/java/ch/njol/skript/expressions/ExprARGB.java new file mode 100644 index 00000000000..c81aaa435be --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprARGB.java @@ -0,0 +1,78 @@ +package ch.njol.skript.expressions; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Keywords; +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 java.util.Locale; +import java.util.function.Function; + +@Name("Alpha/Red/Green/Blue Color Value") +@Description({ + "The alpha, red, green, or blue value of colors. Ranges from 0 to 255.", + "Alpha represents opacity." +}) +@Examples({ + "broadcast red value of rgb(100, 0, 50) # sends '100'", + "set {_red} to red's red value + 10" +}) +@Keywords({"ARGB", "RGB", "color", "colour"}) +@Since("INSERT VERSION") +public class ExprARGB extends SimplePropertyExpression { + + static { + register(ExprARGB.class, Integer.class, "(:alpha|:red|:green|:blue) [value]", "colors"); + } + + RGB color; + + @Override + public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + color = RGB.valueOf(parseResult.tags.get(0).toUpperCase(Locale.ENGLISH)); + return super.init(expressions, matchedPattern, isDelayed, parseResult); + } + + @Override + public Integer convert(Color from) { + return color.getValue(from); + } + + @Override + public Class getReturnType() { + return Integer.class; + } + + @Override + protected String getPropertyName() { + return color.name().toLowerCase(Locale.ENGLISH); + } + + /** + * helper enum for getting argb values of {@link Color}s. + */ + private enum RGB { + ALPHA(Color::getAlpha), + RED(Color::getRed), + GREEN(Color::getGreen), + BLUE(Color::getBlue); + + private final Function get; + + RGB(Function get) { + this.get = get; + } + + public int getValue(Color from) { + return get.apply(from); + } + + } + +} diff --git a/src/main/java/ch/njol/skript/expressions/ExprColorOf.java b/src/main/java/ch/njol/skript/expressions/ExprColorOf.java index e46cfa1fc33..b1698656dbe 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprColorOf.java +++ b/src/main/java/ch/njol/skript/expressions/ExprColorOf.java @@ -85,7 +85,7 @@ protected Color[] get(Event event, Object[] source) { List colors = new ArrayList<>(); for (FireworkEffect effect : (FireworkEffect[]) source) { effect.getColors().stream() - .map(SkriptColor::fromBukkitColor) + .map(ColorRGB::fromBukkitColor) .forEach(colors::add); } return colors.toArray(new Color[0]); diff --git a/src/main/java/ch/njol/skript/util/Color.java b/src/main/java/ch/njol/skript/util/Color.java index 1c5671aea0a..9f74780c82b 100644 --- a/src/main/java/ch/njol/skript/util/Color.java +++ b/src/main/java/ch/njol/skript/util/Color.java @@ -18,30 +18,49 @@ */ package ch.njol.skript.util; +import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable; import org.bukkit.DyeColor; import org.jetbrains.annotations.Nullable; -import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable; - public interface Color extends YggdrasilExtendedSerializable { - + + /** * Gets Bukkit color representing this color. * @return Bukkit color. */ org.bukkit.Color asBukkitColor(); - - + + /** + * @return The alpha component of this color. + */ + int getAlpha(); + + /** + * @return The red component of this color. + */ + int getRed(); + + /** + * @return The green component of this color. + */ + int getGreen(); + + /** + * @return The blue component of this color. + */ + int getBlue(); + /** * Gets Bukkit dye color representing this color, if one exists. * @return Dye color or null. */ @Nullable DyeColor asDyeColor(); - + /** * @return Name of the color. */ String getName(); - + } diff --git a/src/main/java/ch/njol/skript/util/ColorRGB.java b/src/main/java/ch/njol/skript/util/ColorRGB.java index 141f753f701..180b26e7c81 100644 --- a/src/main/java/ch/njol/skript/util/ColorRGB.java +++ b/src/main/java/ch/njol/skript/util/ColorRGB.java @@ -1,6 +1,5 @@ package ch.njol.skript.util; -import ch.njol.skript.Skript; import ch.njol.skript.variables.Variables; import ch.njol.util.Math2; import ch.njol.yggdrasil.Fields; @@ -18,7 +17,6 @@ public class ColorRGB implements Color { - private static final boolean HAS_ARGB = Skript.methodExists(org.bukkit.Color.class, "getAlpha"); private static final Pattern RGB_PATTERN = Pattern.compile("(?>rgb|RGB) (\\d+), (\\d+), (\\d+)"); private org.bukkit.Color bukkit; @@ -60,13 +58,7 @@ public ColorRGB(org.bukkit.Color bukkit) { */ @Contract("_,_,_,_ -> new") public static @NotNull ColorRGB fromRGBA(int red, int green, int blue, int alpha) { - org.bukkit.Color bukkit; - if (HAS_ARGB) { - bukkit = org.bukkit.Color.fromARGB(alpha, red, green, blue); - } else { - bukkit = org.bukkit.Color.fromRGB(red, green, blue); - } - return new ColorRGB(bukkit); + return new ColorRGB(org.bukkit.Color.fromARGB(alpha, red, green, blue)); } /** @@ -93,6 +85,26 @@ public ColorRGB(org.bukkit.Color bukkit) { return new ColorRGB(bukkit); } + @Override + public int getAlpha() { + return bukkit.getAlpha(); + } + + @Override + public int getRed() { + return bukkit.getRed(); + } + + @Override + public int getGreen() { + return bukkit.getGreen(); + } + + @Override + public int getBlue() { + return bukkit.getBlue(); + } + @Override public org.bukkit.Color asBukkitColor() { return bukkit; @@ -106,7 +118,7 @@ public org.bukkit.Color asBukkitColor() { @Override public String getName() { String rgb = bukkit.getRed() + ", " + bukkit.getGreen() + ", " + bukkit.getBlue(); - if (HAS_ARGB && bukkit.getAlpha() != 255) + if (bukkit.getAlpha() != 255) return "argb " + bukkit.getAlpha() + ", " + rgb; return "rgb " + rgb; } diff --git a/src/main/java/ch/njol/skript/util/SkriptColor.java b/src/main/java/ch/njol/skript/util/SkriptColor.java index 7d03b13ed81..598fc66b6fa 100644 --- a/src/main/java/ch/njol/skript/util/SkriptColor.java +++ b/src/main/java/ch/njol/skript/util/SkriptColor.java @@ -18,6 +18,15 @@ */ package ch.njol.skript.util; +import ch.njol.skript.localization.Adjective; +import ch.njol.skript.localization.Language; +import ch.njol.skript.variables.Variables; +import ch.njol.yggdrasil.Fields; +import org.bukkit.ChatColor; +import org.bukkit.DyeColor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.io.NotSerializableException; import java.io.StreamCorruptedException; import java.util.Arrays; @@ -26,18 +35,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; -import java.util.regex.Pattern; - -import org.bukkit.ChatColor; -import org.bukkit.DyeColor; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import ch.njol.skript.Skript; -import ch.njol.skript.localization.Adjective; -import ch.njol.skript.localization.Language; -import ch.njol.skript.variables.Variables; -import ch.njol.yggdrasil.Fields; @SuppressWarnings("null") public enum SkriptColor implements Color { @@ -96,7 +93,27 @@ public enum SkriptColor implements Color { public org.bukkit.Color asBukkitColor() { return dye.getColor(); } - + + @Override + public int getAlpha() { + return dye.getColor().getAlpha(); + } + + @Override + public int getRed() { + return dye.getColor().getRed(); + } + + @Override + public int getGreen() { + return dye.getColor().getGreen(); + } + + @Override + public int getBlue() { + return dye.getColor().getBlue(); + } + @Override public DyeColor asDyeColor() { return dye; diff --git a/src/test/skript/tests/syntaxes/expressions/ExprARGB.sk b/src/test/skript/tests/syntaxes/expressions/ExprARGB.sk new file mode 100644 index 00000000000..4110a22aa8a --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprARGB.sk @@ -0,0 +1,20 @@ +test "argb": + + set {_colour} to rgb(0, 0, 0, 0) + assert {_colour}'s red is 0 with "failed to get red" + assert {_colour}'s green value is 0 with "failed to get green" + assert blue of {_colour} is 0 with "failed to get blue" + assert alpha value of {_colour} is 0 with "failed to get alpha" + + set {_colour} to rgb(9, 102, 55, 10) + assert {_colour}'s red is 9 with "failed to get red" + assert {_colour}'s green value is 102 with "failed to get green" + assert blue of {_colour} is 55 with "failed to get blue" + assert alpha value of {_colour} is 10 with "failed to get alpha" + + set {_colour} to red + assert {_colour}'s red is 176 with "failed to get red" + assert {_colour}'s green value is 46 with "failed to get green" + assert blue of {_colour} is 38 with "failed to get blue" + assert alpha value of {_colour} is 255 with "failed to get alpha" + From 8fca6557977e5933d2db5751d1fe6f1d04999925 Mon Sep 17 00:00:00 2001 From: sovdee <10354869+sovdeeth@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:57:14 -0500 Subject: [PATCH 2/3] force value --- .../java/ch/njol/skript/expressions/ExprARGB.java | 2 +- .../skript/tests/syntaxes/expressions/ExprARGB.sk | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprARGB.java b/src/main/java/ch/njol/skript/expressions/ExprARGB.java index c81aaa435be..0b95ec3e544 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]", "colors"); + register(ExprARGB.class, Integer.class, "(:alpha|:red|:green|:blue) value", "colors"); } RGB color; diff --git a/src/test/skript/tests/syntaxes/expressions/ExprARGB.sk b/src/test/skript/tests/syntaxes/expressions/ExprARGB.sk index 4110a22aa8a..854fddf55ee 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprARGB.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprARGB.sk @@ -1,20 +1,20 @@ test "argb": set {_colour} to rgb(0, 0, 0, 0) - assert {_colour}'s red is 0 with "failed to get red" + assert {_colour}'s red value is 0 with "failed to get red" assert {_colour}'s green value is 0 with "failed to get green" - assert blue of {_colour} is 0 with "failed to get blue" + assert blue value of {_colour} is 0 with "failed to get blue" assert alpha value of {_colour} is 0 with "failed to get alpha" set {_colour} to rgb(9, 102, 55, 10) - assert {_colour}'s red is 9 with "failed to get red" + assert {_colour}'s red value is 9 with "failed to get red" assert {_colour}'s green value is 102 with "failed to get green" - assert blue of {_colour} is 55 with "failed to get blue" + assert blue value of {_colour} is 55 with "failed to get blue" assert alpha value of {_colour} is 10 with "failed to get alpha" set {_colour} to red - assert {_colour}'s red is 176 with "failed to get red" + assert {_colour}'s red value is 176 with "failed to get red" assert {_colour}'s green value is 46 with "failed to get green" - assert blue of {_colour} is 38 with "failed to get blue" + assert blue value of {_colour} is 38 with "failed to get blue" assert alpha value of {_colour} is 255 with "failed to get alpha" From 22ac79d16a7aea6c011d21190c62983da632f792 Mon Sep 17 00:00:00 2001 From: sovdee <10354869+sovdeeth@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:06:16 -0500 Subject: [PATCH 3/3] component --- 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 0b95ec3e544..7691f8aaad5 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", "colors"); + register(ExprARGB.class, Integer.class, "(:alpha|:red|:green|:blue) (value|component)", "colors"); } RGB color;