Skip to content
Draft
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
2 changes: 2 additions & 0 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.skriptlang.skript.bukkit.registration.BukkitSyntaxInfos;
import org.skriptlang.skript.bukkit.tags.TagModule;
import org.skriptlang.skript.common.CommonModule;
import org.skriptlang.skript.common.colors.ColorModule;
import org.skriptlang.skript.docs.Origin;
import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.Comparators;
Expand Down Expand Up @@ -577,6 +578,7 @@ public void onEnable() {
skript.loadModules(
new CommonModule(),
new BrewingModule(),
new ColorModule(),
new EntityModule(),
new DamageSourceModule(),
new InteractionModule(),
Expand Down
28 changes: 3 additions & 25 deletions src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.DefaultClasses;
import ch.njol.skript.util.*;
import ch.njol.skript.util.Contract;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Utils;
import ch.njol.util.Math2;
import ch.njol.util.StringUtils;
import ch.njol.util.coll.CollectionUtils;
Expand Down Expand Up @@ -355,7 +356,7 @@ public Class<?> getReturnType(Expression<?>... arguments) {

Functions.register(DefaultFunction.builder(skript, "toBase", String[].class)
.description("""
Turns a number in a string using a specific base (decimal, hexadecimal, octal).
Turns a number into a string using a specific base (decimal, hexadecimal, octal).
For example, converting 32 to hexadecimal (base 16) would be 'toBase(32, 16)', which would return "20".
You can use any base between 2 and 36.
""")
Expand Down Expand Up @@ -592,29 +593,6 @@ public Long[] executeSimple(Object[][] params) {
}.description("Calculates the total amount of experience needed to achieve given level from scratch in Minecraft.")
.since("2.2-dev32"));

Functions.registerFunction(new SimpleJavaFunction<Color>("rgb", new Parameter[] {
new Parameter<>("red", DefaultClasses.LONG, true, null),
new Parameter<>("green", DefaultClasses.LONG, true, null),
new Parameter<>("blue", DefaultClasses.LONG, true, null),
new Parameter<>("alpha", DefaultClasses.LONG, true, new SimpleLiteral<>(255L,true))
}, DefaultClasses.COLOR, true) {
@Override
public ColorRGB[] executeSimple(Object[][] params) {
Long red = (Long) params[0][0];
Long green = (Long) params[1][0];
Long blue = (Long) params[2][0];
Long alpha = (Long) params[3][0];

return CollectionUtils.array(ColorRGB.fromRGBA(red.intValue(), green.intValue(), blue.intValue(), alpha.intValue()));
}
}).description("Returns a RGB color from the given red, green and blue parameters. Alpha values can be added optionally, " +
"but these only take affect in certain situations, like text display backgrounds.")
.examples(
"dye player's leggings rgb(120, 30, 45)",
"set the colour of a text display to rgb(10, 50, 100, 50)"
)
.since("2.5, 2.10 (alpha)");

Functions.register(DefaultFunction.builder(skript, "player", Player.class)
.description(
"Returns an online player from their name or UUID, if player is offline function will return nothing.",
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,37 +351,6 @@ public String toVariableNameString(final Direction o) {

Classes.registerClass(new SlotClassInfo());

Classes.registerClass(new ClassInfo<>(Color.class, "color")
.user("colou?rs?")
.name("Color")
.description("Wool, dye and chat colors.")
.usage("black, dark grey/dark gray, grey/light grey/gray/light gray/silver, white, blue/dark blue, cyan/aqua/dark cyan/dark aqua, light blue/light cyan/light aqua, green/dark green, light green/lime/lime green, yellow/light yellow, orange/gold/dark yellow, red/dark red, pink/light red, purple/dark purple, magenta/light purple, brown/indigo")
.examples("color of the sheep is red or black",
"set the color of the block to green",
"message \"You're holding a <%color of tool%>%color of tool%<reset> wool block\"")
.since("")
.supplier(SkriptColor.values())
.parser(new Parser<Color>() {
@Override
@Nullable
public Color parse(String input, ParseContext context) {
Color rgbColor = ColorRGB.fromString(input);
if (rgbColor != null)
return rgbColor;
return SkriptColor.fromName(input);
}

@Override
public String toString(Color c, int flags) {
return c.getName();
}

@Override
public String toVariableNameString(Color color) {
return "" + color.getName().toLowerCase(Locale.ENGLISH).replace('_', ' ');
}
}));

Classes.registerClass(new ClassInfo<>(StructureType.class, "structuretype")
.user("tree ?types?", "trees?")
.name("Tree Type")
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/ch/njol/skript/expressions/ExprARGB.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ch.njol.skript.expressions;

import ch.njol.skript.doc.*;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Example;
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.Literal;
Expand All @@ -24,7 +28,7 @@
public class ExprARGB extends SimplePropertyExpression<Color, Integer> {

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.jetbrains.annotations.NotNull;

import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.util.Color;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Timespan;

Expand All @@ -31,7 +30,6 @@ public class DefaultClasses {
public static ClassInfo<World> WORLD = getClassInfo(World.class);

// Skript
public static ClassInfo<Color> COLOR = getClassInfo(Color.class);
public static ClassInfo<Date> DATE = getClassInfo(Date.class);
public static ClassInfo<Timespan> TIMESPAN = getClassInfo(Timespan.class);

Expand Down
31 changes: 17 additions & 14 deletions src/main/java/ch/njol/skript/util/Color.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
package ch.njol.skript.util;

import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable;
import org.bukkit.DyeColor;
import org.jetbrains.annotations.Nullable;

public interface Color extends YggdrasilExtendedSerializable {
public interface Color {

/**
* 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 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();

/**
* Gets Bukkit dye color representing this color, if one exists.
* @return Dye color or null.
* @return The {@link DyeColor} representing this color if one exists, or null otherwise.
*/
@Nullable
DyeColor asDyeColor();
@Nullable DyeColor asDyeColor();

/**
* @return Name of the color.
*/
String getName();

/**
* @return the color as an ARGB integer.
* @return The color as an ARGB integer.
*/
default int asARGB() {
return asBukkitColor().asARGB();
}

/**
* @return the colour as an RGB hex value: RRGGBB
* @return The color as an RGB hex value: RRGGBB
*/
default String toHexString() {
return String.format("%02X%02X%02X", getRed(), getGreen(), getBlue());
}

/**
* @return The integer representing this color. Used for serialization.
*/
default int asInt() {
return (getAlpha() << 24) | (getRed() << 16) | (getGreen() << 8) | getBlue();
}

}
101 changes: 101 additions & 0 deletions src/main/java/ch/njol/skript/util/ColorHSB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package ch.njol.skript.util;

import ch.njol.util.Math2;
import org.bukkit.DyeColor;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.common.colors.ColorUtils;

/**
* Immutable representation of a color in the HSB/HSV color-space, with an alpha channel.
* Hue, saturation, and brightness are stored in normalized form: 0-1.
* Alpha is stored as a value from 0-255 (like the red, green, and blue channels).
*/
public final class ColorHSB implements Color {

private final float hue;
private final float saturation;
private final float brightness;
private final int alpha;

private final ColorRGB rgb;
private final @Nullable DyeColor dye;

private ColorHSB(float hue, float saturation, float brightness, int alpha) {
this.hue = hue;
this.saturation = saturation;
this.brightness = brightness;
this.alpha = alpha;

this.rgb = ColorUtils.hsbToRgb(this);
this.dye = rgb.asDyeColor();
}

public static @NotNull ColorHSB fromHSBA(float hue, float saturation, float brightness, int alpha) {
return new ColorHSB(
Math2.fit(0f, hue, 1f),
Math2.fit(0f, saturation, 1f),
Math2.fit(0f, brightness, 1f),
Math2.fit(0, alpha, 255)
);
}

public static @NotNull ColorHSB fromHSB(float hue, float saturation, float brightness) {
return fromHSBA(hue, saturation, brightness, 255);
}

@ApiStatus.Internal
public static @NotNull ColorHSB fromUncheckedHSBA(float hue, float saturation, float brightness, int alpha) {
return new ColorHSB(hue, saturation, brightness, alpha);
}

public float getHue() {
return hue;
}

public float getSaturation() {
return saturation;
}

public float getBrightness() {
return brightness;
}

@Override
public org.bukkit.Color asBukkitColor() {
return rgb.asBukkitColor();
}

@Override
public int getAlpha() {
return alpha;
}

@Override
public int getRed() {
return rgb.getRed();
}

@Override
public int getGreen() {
return rgb.getGreen();
}

@Override
public int getBlue() {
return rgb.getBlue();
}

@Override
public @Nullable DyeColor asDyeColor() {
return dye;
}

@Override
public String getName() {
String hsb = String.format("%.3f, %.3f, %.3f", hue, saturation, brightness);
return alpha == 255 ? "hsb " + hsb : "hsba " + hsb + ", " + alpha;
}

}
Loading
Loading