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

Conversation

cheeezburga
Copy link
Member

@cheeezburga cheeezburga commented Dec 8, 2024

Description

This PR adds a few colour manipulation expressions and functions. There might be more to come in another PR, because there's plenty left which can still be included, but these seemed (to me) like the important-ish ones. Find below a list of new syntax:

  • Hex expression: returns the hex code a colour. Very useful when trying to colour strings or compare colours
  • Blend expression: blends colours together
  • Complement expression: returns the complement of a colour(s), optionally using HSL adjustments
  • shade function: shades a colour by a given amount, optionally using HSL adjustments
  • tint function: tints a colour by a given amount, optionally using HSL adjustments
  • colourBrightness function: adjusts the brightness of a colour by a given amount. This is similar to, but not the same as, the shade and tint functions
  • grayscale function: converts a colour to its grayscale equivalent
  • sepiatone function: converts a colour to its sepiatone equivalent

This PR also adds channel as an option in ExprARGB.

I used this script to do some basic testing in the early stages of the PR, so feel free to try it:

on load:
    set {block} to "█"
    set {blocks} to {block} repeated 10 times

function hexTest():
    loop all colours:
        broadcast "%loop-value%: %hex of loop-value%"

function blendTest(colour1: colour, colour2: colour):
    set {_asHex1} to hex of {_colour1}
    set {_asHex2} to hex of {_colour2}
    send action bar (formatted "Testing blending of %{_asHex1}%%{blocks}% &fto %{_asHex2}%%{blocks}%") to all players
    loop integers from 0 to 100:
        set {_blendedColour} to {_colour1} blended with {_colour2} by a factor of loop-value
        set {_asHex} to hex of {_blendedColour}
        send title (formatted "%{_asHex}%%{blocks}%") to all players for 2 ticks with fade in 0 ticks and fade out 0 ticks
        send "%{_blendedColour}%: %loop-value%" to all players
        wait 1 tick

function complementTest(colour: colour):
    set {_asHex} to hex of {_colour}
    send action bar (formatted "Testing complement of %{_asHex}%%{blocks}%") to all players
    send title (formatted "%{_asHex}%%{blocks}%") to all players for 2 seconds with fade in 0 ticks and fade out 0 ticks
    wait 39 ticks
    set {_complement} to complement of {_colour}
    set {_asHex} to hex of {_complement}
    send title (formatted "%{_asHex}%%{blocks}%") to all players for 2 seconds with fade in 0 ticks and fade out 0 ticks
    wait 39 ticks
    set {_complement} to hsl complement of {_colour}
    set {_asHex} to hex of {_complement}
    send title (formatted "%{_asHex}%%{blocks}%") to all players for 2 seconds with fade in 0 ticks and fade out 0 ticks

function shadeTest(colour: colour):
    set {_asHex} to hex of {_colour}
    send action bar (formatted "Testing shading of %{_asHex}%%{blocks}%") to all players
    loop 100 times:
        set {_shadedColour} to shade({_colour}, loop-value)
        set {_asHex} to hex of {_shadedColour}
        send title (formatted "%{_asHex}%%{blocks}%") to all players for 2 ticks with fade in 0 ticks and fade out 0 ticks
        send {_shadedColour} to all players
        wait 1 tick

function tintTest(colour: colour):
    set {_asHex} to hex of {_colour}
    send action bar (formatted "Testing tinting of %{_asHex}%%{blocks}%") to all players
    loop 100 times:
        set {_tintedColour} to tint({_colour}, loop-value)
        set {_asHex} to hex of {_tintedColour}
        send title (formatted "%{_asHex}%%{blocks}%") to all players for 2 ticks with fade in 0 ticks and fade out 0 ticks
        send {_tintedColour} to all players
        wait 1 tick

function brightnessTest(colour: colour):
    set {_asHex} to hex of {_colour}
    send action bar (formatted "Testing brightness of %{_asHex}%%{blocks}%") to all players
    loop integers from -100 to 100:
        set {_tintedColour} to colourBrightness({_colour}, loop-value)
        set {_asHex} to hex of {_tintedColour}
        send title (formatted "%{_asHex}%%{blocks}%") to all players for 2 ticks with fade in 0 ticks and fade out 0 ticks
        send {_tintedColour} to all players
        wait 1 tick
    
function grayscaleTest(colour: colour):
    set {_asHex} to hex of {_colour}
    send action bar (formatted "Testing grayscale of shading of %{_asHex}%%{blocks}%") to all players
    loop 100 times:
        set {_shadedColour} to shade({_colour}, loop-value)
        set {_grayscale} to grayscale({_shadedColour})
        set {_asHex} to hex of {_grayscale}
        send title (formatted "%{_asHex}%%{blocks}%") to all players for 2 ticks with fade in 0 ticks and fade out 0 ticks
        send {_shadedColour} to all players
        wait 1 tick

function sepiatoneTest(colour: colour):
    set {_asHex} to hex of {_colour}
    send action bar (formatted "Testing sepiatone of shading of %{_asHex}%%{blocks}%") to all players
    loop 100 times:
        set {_shadedColour} to shade({_colour}, loop-value)
        set {_sepia} to sepiatone({_shadedColour})
        set {_asHex} to hex of {_sepia}
        send title (formatted "%{_asHex}%%{blocks}%") to all players for 2 ticks with fade in 0 ticks and fade out 0 ticks
        send {_shadedColour} to all players
        wait 1 tick

Target Minecraft Versions: any
Requirements: none
Related Issues: none

Copy link
Member

@sovdeeth sovdeeth left a comment

Choose a reason for hiding this comment

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

nice work

Comment on lines 27 to 48
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 };
}
Copy link
Member

Choose a reason for hiding this comment

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

Why not return a new Color?

Comment on lines 85 to 91
/* return new SyntaxStringBuilder(event, debug)
.append(this.colours)
.append("blended with")
.append(this.blendWith)
.append("by a factor of")
.append(this.amount)
.toString(); */
Copy link
Member

Choose a reason for hiding this comment

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

can uncomment now

Copy link
Member Author

Choose a reason for hiding this comment

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

This relies on #7244

Copy link
Member

Choose a reason for hiding this comment

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

merged now

Comment on lines 9 to 26
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<? extends String> getReturnType() {
return String.class;
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this
I think i'd rather see it implemented as taking in a number and returning a string, rather than being restricted to colors

Copy link
Contributor

@Fusezion Fusezion left a comment

Choose a reason for hiding this comment

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

If we're gonna be moving colors into a module can we move the existing color classes into it?
While we're here can we also complete #5176, #6490 and #5492 (this one was completed)

Copy link
Member

@Efnilite Efnilite left a comment

Choose a reason for hiding this comment

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

colour is used everywhere, and i think skript generally uses color, as do most people. you should probably replace all colour mentions with color

@Efnilite Efnilite added the feature Pull request adding a new feature. label Dec 8, 2024
@cheeezburga
Copy link
Member Author

colour is used everywhere, and i think skript generally uses color, as do most people. you should probably replace all colour mentions with color

Yeah already discussed briefly on Discord - I'll change them all over in my next round of commits 👍

@@ -0,0 +1,144 @@
package org.skriptlang.skript.misc.colors;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
package org.skriptlang.skript.misc.colors;
package org.skriptlang.skript.common.colors;

For native/non-Bukkit dependent syntax, we had sort of informally agreed on using common as the package name for, which I think would work better here.

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

@@ -81,12 +57,17 @@ public enum SkriptColor implements Color {

private ChatColor chat;
private DyeColor dye;
@Nullable
private Adjective adjective;
private @Nullable Adjective adjective;
Copy link
Member

Choose a reason for hiding this comment

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

Is this field still Nullable now that the setter is gone?

import org.jetbrains.annotations.NotNull;

/**
* Utility class for colour manipulation and conversion.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Utility class for colour manipulation and conversion.
* Utility class for color manipulation and conversion.

*/
public class ColorUtils {

public static Color fromInt(int asInt) {
Copy link
Member

Choose a reason for hiding this comment

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

javadocs

public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
this.colours = (Expression<Color>) exprs[0];
this.blendWith = (Expression<Color>) exprs[1];
this.amount = (Expression<Number>) exprs[2];
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
this.amount = (Expression<Number>) exprs[2];
this.amount = (Expression<Number>) exprs[2];

Comment on lines +38 to +40
"%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%]",
"%colors% and %colors% blen(ded|t) together [by [a[n] (factor|amount) of] %-number%]");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"%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%]",
"%colors% and %colors% blen(ded|t) together [by [a[n] (factor|amount) of] %-number%]");
"%colors% (blended|mixed) with %colors% [by [(a factor|an amount) of] %-number%]",
"blend of %colors% (and|with) %colors% [by [(a factor|an amount) of] %-number%]",
"%colors% and %colors% blen(ded|t) together [by [(a factor|an amount) of] %-number%]");

I think this is somewhat easier to read (and a bit more correct!)


@Override
protected String getPropertyName() {
return "complementary color";
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return "complementary color";
return (hsl ? "hsl " : "") + "complementary color";

}

@Override
protected String getPropertyName() {
Copy link
Member

Choose a reason for hiding this comment

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

Should be placed after getReturnType

@Override
public Class<? extends String> getReturnType() {
return String.class;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Pull request adding a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants