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

Colours Are Cool 🌈 #7246

Open
wants to merge 24 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
401baaf
Still a WIP, implements some functions and expressions
cheeezburga Dec 7, 2024
bd64435
Adds blend expression
cheeezburga Dec 7, 2024
ae81c9e
Adds complement expression
cheeezburga Dec 7, 2024
d5985d6
Small change
cheeezburga Dec 7, 2024
fc342b8
Fixed invalid spelling of colour throwing a pattern exception
cheeezburga Dec 7, 2024
50e064c
Fixed invalid spelling of colour throwing a pattern exception
cheeezburga Dec 7, 2024
a1cadb7
Adds hex code expression
cheeezburga Dec 7, 2024
e2d9081
Adds channel as an option in ExprARGB's patterns
cheeezburga Dec 8, 2024
b50ed36
Fixes number logic and adds another pattern to ExprBlend
cheeezburga Dec 8, 2024
2427be4
Removes the <> being included in the hex code util method
cheeezburga Dec 8, 2024
d725215
Adds hex codes test
cheeezburga Dec 8, 2024
9875ffe
Adds colour blending test
cheeezburga Dec 8, 2024
67320f9
Adds colour complements test
cheeezburga Dec 8, 2024
3123bdb
Fixes tests
cheeezburga Dec 8, 2024
34d2024
Adds function tests (shade, tint, brightness, grayscale, sepiatone)
cheeezburga Dec 8, 2024
d07ce14
Fixes brightness test
cheeezburga Dec 8, 2024
c0e9cd7
Adds documentation to ColourUtils
cheeezburga Dec 8, 2024
e1f6364
Changes all 'colour's to 'color's and adds better descriptions and ex…
cheeezburga Dec 9, 2024
ed49cfd
Changes all 'colour's to 'color's and adds better descriptions and ex…
cheeezburga Dec 9, 2024
a8758b3
Moves hex conversion to Color class in lieu of static util method and…
cheeezburga Dec 9, 2024
99fb214
Adds some comments and changes variable names to not be just single c…
cheeezburga Dec 9, 2024
b6b4dee
Adds doc annotations to ExprHex
cheeezburga Dec 11, 2024
641fa49
Some small doc/pattern changes
cheeezburga Dec 11, 2024
7340a72
First attempt at serialization of colors
cheeezburga Dec 14, 2024
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
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -106,6 +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.colors.ColorModule;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -537,6 +536,7 @@ public void onEnable() {
BreedingModule.load();
DisplayModule.load();
InputModule.load();
ColorModule.load();
} catch (final Exception e) {
exception(e, "Could not load required .class files: " + e.getLocalizedMessage());
setEnabled(false);
Expand Down
83 changes: 50 additions & 33 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to move the Color ClassInfo registration to the module

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,8 +71,7 @@ public SkriptClasses() {}
.supplier(() -> (Iterator) Classes.getClassInfos().iterator())
.parser(new Parser<ClassInfo>() {
@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));
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -144,8 +143,7 @@ public boolean mustSyncDeserialization() {
.defaultExpression(new SimpleLiteral<>(WeatherType.CLEAR, true))
.parser(new Parser<WeatherType>() {
@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);
}

Expand All @@ -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);
}

})
Expand Down Expand Up @@ -184,8 +182,7 @@ public String toVariableNameString(final WeatherType o) {
.iterator())
.parser(new Parser<ItemType>() {
@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);
}

Expand Down Expand Up @@ -219,7 +216,7 @@ public String toVariableNameString(final ItemType t) {
b.append(":" + ench.getLevel());
}
}
return "" + b.toString();
return b.toString();
}
})
.cloner(ItemType::clone)
Expand All @@ -239,8 +236,7 @@ public String toVariableNameString(final ItemType t) {
.defaultExpression(new EventValueExpression<>(Time.class))
.parser(new Parser<Time>() {
@Override
@Nullable
public Time parse(final String s, final ParseContext context) {
public @Nullable Time parse(final String s, final ParseContext context) {
return Time.parse(s);
}

Expand Down Expand Up @@ -272,8 +268,7 @@ public String toVariableNameString(final Time o) {
.since("1.0, 2.6.1 (weeks, months, years)")
.parser(new Parser<Timespan>() {
@Override
@Nullable
public Timespan parse(final String s, final ParseContext context) {
public @Nullable Timespan parse(final String s, final ParseContext context) {
try {
return Timespan.parse(s, context);
} catch (IllegalArgumentException e) {
Expand Down Expand Up @@ -307,8 +302,7 @@ public String toVariableNameString(final Timespan o) {
.defaultExpression(new SimpleLiteral<>(new Timeperiod(0, 23999), true))
.parser(new Parser<Timeperiod>() {
@Override
@Nullable
public Timeperiod parse(final String s, final ParseContext context) {
public @Nullable Timeperiod parse(final String s, final ParseContext context) {
if (s.equalsIgnoreCase("day")) {
return new Timeperiod(0, 11999);
} else if (s.equalsIgnoreCase("dusk")) {
Expand Down Expand Up @@ -369,8 +363,7 @@ public String toVariableNameString(final Timeperiod o) {
.defaultExpression(new SimpleLiteral<>(new Direction(new double[] {0, 0, 0}), true))
.parser(new Parser<Direction>() {
@Override
@Nullable
public Direction parse(final String s, final ParseContext context) {
public @Nullable Direction parse(final String s, final ParseContext context) {
return null;
}

Expand Down Expand Up @@ -408,10 +401,9 @@ public String toVariableNameString(final Direction o) {
.since("")
.defaultExpression(new EventValueExpression<>(Slot.class))
.changer(new Changer<Slot>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<Object>[] acceptChange(final ChangeMode mode) {
@SuppressWarnings("unchecked")
public @Nullable Class<Object>[] acceptChange(final ChangeMode mode) {
if (mode == ChangeMode.RESET)
return null;
if (mode == ChangeMode.SET)
Expand Down Expand Up @@ -520,8 +512,7 @@ public String toVariableNameString(Slot o) {
.supplier(SkriptColor.values())
.parser(new Parser<Color>() {
@Override
@Nullable
public Color parse(String input, ParseContext context) {
public @Nullable Color parse(String input, ParseContext context) {
Color rgbColor = ColorRGB.fromString(input);
if (rgbColor != null)
return rgbColor;
Expand All @@ -535,7 +526,36 @@ public String toString(Color c, int flags) {

@Override
public String toVariableNameString(Color color) {
return "" + color.getName().toLowerCase(Locale.ENGLISH).replace('_', ' ');
return color.getName().toLowerCase(Locale.ENGLISH).replace('_', ' ');
}
})
.serializer(new Serializer<Color>() {
@Override
public Fields serialize(Color color) throws NotSerializableException {
Fields f = new Fields();
f.putPrimitive("asInt", color.asInt());
return f;
}

@Override
public void deserialize(Color o, Fields f) throws StreamCorruptedException {
assert false;
}

@Override
protected Color deserialize(Fields fields) throws StreamCorruptedException {
int asInt = fields.getPrimitive("asInt", int.class);
return ColorUtils.fromInt(asInt);
}

@Override
public boolean mustSyncDeserialization() {
return false;
}

@Override
protected boolean canBeInstantiated() {
return false;
}
}));

Expand All @@ -550,8 +570,7 @@ public String toVariableNameString(Color color) {
.defaultExpression(new SimpleLiteral<>(StructureType.TREE, true))
.parser(new Parser<StructureType>() {
@Override
@Nullable
public StructureType parse(final String s, final ParseContext context) {
public @Nullable StructureType parse(final String s, final ParseContext context) {
return StructureType.fromName(s);
}

Expand All @@ -562,7 +581,7 @@ public String toString(final StructureType o, final int flags) {

@Override
public String toVariableNameString(final StructureType o) {
return "" + o.name().toLowerCase(Locale.ENGLISH);
return o.name().toLowerCase(Locale.ENGLISH);
}
}).serializer(new EnumSerializer<>(StructureType.class)));

Expand Down Expand Up @@ -605,8 +624,7 @@ public String toVariableNameString(final EnchantmentType o) {
private final RegexMessage pattern = new RegexMessage("types.experience.pattern", Pattern.CASE_INSENSITIVE);

@Override
@Nullable
public Experience parse(String s, final ParseContext context) {
public @Nullable Experience parse(String s, final ParseContext context) {
int xp = -1;
if (s.matches("\\d+ .+")) {
xp = Utils.parseInt("" + s.substring(0, s.indexOf(' ')));
Expand Down Expand Up @@ -641,8 +659,7 @@ public String toVariableNameString(final Experience xp) {
.after("itemtype")
.parser(new Parser<VisualEffect>() {
@Override
@Nullable
public VisualEffect parse(String s, ParseContext context) {
public @Nullable VisualEffect parse(String s, ParseContext context) {
return VisualEffects.parse(s);
}

Expand All @@ -666,7 +683,7 @@ public String toVariableNameString(VisualEffect e) {
.usage("")
.examples("")
.since("2.5")
.serializer(new YggdrasilSerializer<GameruleValue>())
.serializer(new YggdrasilSerializer<>())
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/expressions/ExprARGB.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,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
53 changes: 27 additions & 26 deletions src/main/java/ch/njol/skript/util/Color.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
/**
* 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 <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
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 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();

Expand Down
Loading
Loading