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 35 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 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 👍

Copy link
Member

@APickledWalrus APickledWalrus left a comment

Choose a reason for hiding this comment

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

Looking really good, great work. Just a few last things. There are a few colour usages in the tests (and their directory) that we might want to change for consistency.

public class ColorModule {

public static void load() throws IOException {
Skript.getAddonInstance().loadClasses("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
Skript.getAddonInstance().loadClasses("org.skriptlang.skript.misc", "colors");
Skript.getAddonInstance().loadClasses("org.skriptlang.skript.misc.colors.elements");

I would move the syntax into an elements subpackage

float max = Math.max(red, Math.max(green, blue));
float min = Math.min(red, Math.min(green, blue));

float hue, saturation, lightness = (max + min) / 2f; // lightness = midpoint of max and min
Copy link
Member

Choose a reason for hiding this comment

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

I might split this onto new lines for clarity

hue = ((red - green) / delta + 4f) / 6f;
}
}
return new float[]{ hue, saturation, lightness };
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 new float[]{ hue, saturation, lightness };
return new float[]{hue, saturation, lightness};

- Separates the classinfos, functions, and elements registrations
- Moves RGB function to ColorModule
- Optimizes imports
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.

6 participants