Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expression to get ARGB values of colours #7215

Open
wants to merge 3 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprARGB.java
Original file line number Diff line number Diff line change
@@ -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<Color, Integer> {

static {
register(ExprARGB.class, Integer.class, "(:alpha|:red|:green|:blue) (value|component)", "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<? extends Integer> 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<Color, Integer> get;

RGB(Function<Color, Integer> get) {
this.get = get;
}

public int getValue(Color from) {
return get.apply(from);
}

}

}
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/expressions/ExprColorOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected Color[] get(Event event, Object[] source) {
List<Color> 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]);
Expand Down
33 changes: 26 additions & 7 deletions src/main/java/ch/njol/skript/util/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
32 changes: 22 additions & 10 deletions src/main/java/ch/njol/skript/util/ColorRGB.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
43 changes: 30 additions & 13 deletions src/main/java/ch/njol/skript/util/SkriptColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprARGB.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test "argb":

set {_colour} to rgb(0, 0, 0, 0)
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 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 value is 9 with "failed to get red"
assert {_colour}'s green value is 102 with "failed to get green"
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 value is 176 with "failed to get red"
assert {_colour}'s green value is 46 with "failed to get green"
assert blue value of {_colour} is 38 with "failed to get blue"
assert alpha value of {_colour} is 255 with "failed to get alpha"