ColorUtils 1.16+

Discussion in 'Snippets' started by Sorbon, Jul 13, 2020.

Tags:
1. Sorbon Supporter

Joined:
Jul 8, 2020
Messages:
4
5
Color Utils
By Mr. Darth and Sorbon

31.10.2021 Update
Gradients can now have multiple colors, magik!

What is Color Utils?

With the update 1.16 a new feature was added, which allows us to use hex codes for colours.
Color Utils contains two main functions.
- Rainbow
More details are below.

But what are Hex Colors?
A hex triplet is a six-digit, three-byte hexadecimal number used in HTML, CSS, SVG, and other computing applications to represent colors. The bytes represent the red, green and blue components of the color. One byte represents a number in the range 00 to FF (in hexadecimal notation), or 0 to 255 in decimal notation. This represents the least (0) to the most (255) intensity of each of the color components. Thus web colors specify colors in the 24-bit RGB color scheme. The hex triplet is formed by concatenating three bytes in hexadecimal notation, in the following order:

• Byte 1: red value (color type red)
• Byte 2: green value (color type green)
• Byte 3: blue value (color type blue)
For example, consider the color where the red/green/blue values are decimal numbers: red=36, green=104, blue=160 (a grayish-blue color). The decimal numbers 36, 104 and 160 are equivalent to the hexadecimal numbers 24, 68 and A0 respectively. The hex triplet is obtained by concatenating the six hexadecimal digits together, 2468A0 in this example.

If any one of the three color values is less than 10 hex (16 decimal), it must be represented with a leading zero so that the triplet always has exactly six digits. For example, the decimal triplet 4, 8, 16 would be represented by the hex digits 04, 08, 10, forming the hex triplet 040810.

The number of colors that can be represented by this system is 166 or 2563 or 224 = 16,777,216.
tl;dr: Hex codes allow us to use 16 million different Colors. yes, we can finally use brown text color
How does one use Hex Colors?
Skript v2.6 allows us to use "<##FF00AA>" for the Hex colors.
The second # in the code is optional, but sometimes you need it, because skript isn't the smartest ;D

Note: If you're using the hex codes syntax in 'send %text%' , you will have to add "formatted" in front of it.
Example:
Code (Text):
1. send formatted "<##FF00AA>This Text is now Colored!"

Color Utils Functions:

Return Value: String / Text

Description: Function to convert decimal numbers in the range [0; 255] to their hexadecimal representation.
Parameter:
number: the number to be converted to hexadecimal

Code (Text):
1. function decimalToHexadecimal(number: number) :: string:
2.     return ("00" if {_number} <= 0, else ("FF")) if {_number} is not between 0 and 255
3.     return join (character at (mod((floor({_number} / 16)), 16) + 1) in "0123456789ABCDEF") and (character at ((mod({_number}, 16)) + 1) in "0123456789ABCDEF")
4.

Patter: HexToRGB(hex: string)
Return Value: numbers

Description: Function to convert hex to RGB values
Parameter:
Hex: hex value to be converted

Code (Text):
1. function HexToRGB(hex: string) :: numbers:
2.     replace all "##" with "" in {_hex}
3.     set {_s::*} to split "0123456789abcdefklmnor" at ""
4.     loop 3 times:
5.         set {_n} to subtext of {_hex} from characters (loop-value -1)*2+1  to loop-value*2
6.         loop {_s::*}:
7.             add loop-index parsed as number-1 to {_rgb::%loop-value-1%} if 2nd element out of (split {_n} at "") = loop-value-2
8.             add (loop-index parsed as number-1)*16 to {_rgb::%loop-value-1%} if first element out of (split {_n} at "") = loop-value-2
9.     return {_rgb::*}
10.

Patter: rainbow(input: string, wrapAmount: number, lightness: number)
Return Value: String / Text

Description: Function that colors the string in a rainbow pattern
Parameter:
Input: the string to be rainbowified
WrapAmount: the rainbow pattern will start repeating approximately every wrapAmount characters; Default value is -1
Lightness: a number from 0 to 1 (0% - 100%) representing how light the color should be; a value of 0.5 means no change, 1 is white, 0 is black. Default Value is 0.5

Code (Text):
1. function rainbow(input: string, wrapAmount: number = -1, lightness: number = 0.5) :: string:
2.     set {_delta} to 360 / ({_wrapAmount} if {_wrapAmount} > 0, else length of {_input})
3.     set {_lightnessModifier} to (({_lightness}*2)-1) * 255
4.     set {_angle} to 90
5.     loop length of {_input} times:
6.         set {_character} to character at loop-value in {_input}
7.         if {_character} or (character at (loop-value - 1) in {_input}) is "§":
8.             if "abcdefklmnor0123456789" contains {_character}:
9.                 set {_format} to (join {_format} and "&%{_character}%") if {_character} is not "r", else ("")
10.             continue
11.         set {_output} to join {_output}, "<##%decimalToHexadecimal((0.5 * (sin({_angle}) + 1)) * 255 + {_lightnessModifier})%%decimalToHexadecimal((0.5 * (sin({_angle} + 120) + 1)) * 255 + {_lightnessModifier})%%decimalToHexadecimal((0.5 * (sin({_angle} + 240) + 1)) * 255 + {_lightnessModifier})%>", {_format} and {_character}
12.         remove {_delta} from {_angle}
13.     return colored {_output}
14.

Code (Text):
1. command example:
2.     trigger:
3.         send formatted rainbow("This is an example text, which will be rainbowified!")
4.         send formatted rainbow("This is an example text with a wrapAmount of 10", 10)
5.         send formatted rainbow("This is an example text with a lightness of 0.7", -1, 0.7)
6.         send formatted rainbow("This is an example text, &6&nWith&r Color &lCodes")

Return Value: String / Text

Description: Function that applies a gradient pattern over a given string
Parameter:
input: the string to be colored
color: list of colors in HEX (#000000 - #FFFFFF)

Code (Text):
1. function gradient(input: string, color: strings) :: string:
2.     loop {_color::*}:
3.         set {_rgb::%loop-index%::*} to HexToRGB(loop-value)
4.     set {_n} to size of {_color::*} -1
5.     set {_delta} to 180 / (length of uncolored {_input}/{_n} - 1)
6.     loop {_n} times:
7.         loop ceil(length of uncolored {_input}/{_n}) times:
8.             set {_y} to ((loop-value-1) -1)*(ceil((length of uncolored {_input})/{_n})) + (loop-value-2)
9.             while character at {_y}+{_x} in {_input} is "§":
10.                 if "abcdefklmnor0123456789" contains character at {_y}+{_x}+1 in {_input}:
11.                     set {_format} to (join {_format} and "&%character at {_y}+{_x}+1 in {_input}%") if character at {_y}+{_x}+1 in {_input} is not "r", else ("")
13.             set {_character} to character at {_y} in uncolored {_input}
14.             set {_startColorProportion} to (0.5 * (sin(({_delta}*((loop-value-2)-1))+90) + 1))
15.             set {_endColorProportion} to (0.5 * (sin(({_delta}*((loop-value-2)-1))+270) + 1))
16.             set {_output} to join {_output}, "<##%decimalToHexadecimal(({_rgb::%loop-value-1%::1} * {_startColorProportion}) + ({_rgb::%((loop-value-1) + 1)%::1} * {_endColorProportion}))%%decimalToHexadecimal(({_rgb::%loop-value-1%::2} * {_startColorProportion}) + ({_rgb::%((loop-value-1) + 1)%::2} * {_endColorProportion}))%%decimalToHexadecimal(({_rgb::%loop-value-1%::3} * {_startColorProportion}) + ({_rgb::%((loop-value-1) + 1)%::3} * {_endColorProportion}))%>", {_format} and {_character}
17.     return {_output}
18.

Code (Text):
1.
2. command example:
3.     trigger:
4.         send formatted gradient("This is an example text, which will have a gradient applied to", ("FF00FF", "00FF00"))
5.         send formatted gradient("This is an example text with 4 Colors", ("FF00FF", "00FF00","FF0000", "0000FF"))
6.         send formatted gradient("This is an example text &6&nwith&r color &lCodes", ("FF00FF", "00FF00","FF0000", "0000FF"))
7.

Code (Text):
1.
2. #    _____      _              _    _ _   _ _
3. #   / ____|    | |            | |  | | | (_) |
4. #  | |     ___ | | ___  _ __  | |  | | |_ _| |___ TM    | A colorful script brought to you by Sourbun et Mr. Darth :D
5. #  | |    / _ \| |/ _ \| '__| | |  | | __| | / __|      | Version 2.0
6. #  | |___| (_) | | (_) | |    | |__| | |_| | \__ \
7. #   \_____\___/|_|\___/|_|     \____/ \__|_|_|___/
8.
9. # A small set of functions designed to bring a new look to your server, giving it color :)
10. # Nota Bene: These functions are pretty fast, yet using them a lot of times in a short period of time will probably cause quite some lag.
11.
12. # A notable change since the previous version is that the HSL to RGB color converters have been removed.
13. # Avoiding these conversions made the functions a bit faster ;)
14.
16. # @description Function to convert decimal numbers in the range [0; 255] to their hexadecimal representation.
17. # @param number the number to be converted to hexadecimal
18. # @return the hexadecimal representation of the number
19. function decimalToHexadecimal(number: number) :: string:
20.     return ("00" if {_number} <= 0, else ("FF")) if {_number} is not between 0 and 255
21.     return join (character at (mod((floor({_number} / 16)), 16) + 1) in "0123456789ABCDEF") and (character at ((mod({_number}, 16)) + 1) in "0123456789ABCDEF")
22. #HexToRGB(hex: string)
23. # @description Function to convert a hex value to the 3 Color values
24. # @param: 6 letter hex string
25. # @return 3 numbers
26. function HexToRGB(hex: string) :: numbers:
27.     replace all "##" with "" in {_hex}
28.     set {_s::*} to split "0123456789abcdefklmnor" at ""
29.     loop 3 times:
30.         set {_n} to subtext of {_hex} from characters (loop-value -1)*2+1  to loop-value*2
31.         loop {_s::*}:
32.             add loop-index parsed as number-1 to {_rgb::%loop-value-1%} if 2nd element out of (split {_n} at "") = loop-value-2
33.             add (loop-index parsed as number-1)*16 to {_rgb::%loop-value-1%} if first element out of (split {_n} at "") = loop-value-2
34.     return {_rgb::*}
35. # rainbow(input: string, wrap: boolean = false, wrapAmount: number = 10, lightness: number = 0)
36. # @description Function that 'rainbowifies' a given string (id est colors the string in a rainbow pattern - ROYGBIV)
37. # @param input the string to be rainbowified
38. # @param wrapAmount if this value is positive, the rainbow pattern will start repeating approximately every wrapAmount characters
39. # @param lightness a number from 0 to 1 representing how light the color should be; a value of 0.5 means no change, value 0 is black and value 1 is white
40. # @return the rainbowified string
41. function rainbow(input: string, wrapAmount: number = -1, lightness: number = 0.5) :: string:
42.     set {_delta} to 360 / ({_wrapAmount} if {_wrapAmount} > 0, else length of {_input})
43.     set {_lightnessModifier} to (({_lightness}*2)-1) * 255
44.     set {_angle} to 90
45.     loop length of {_input} times:
46.         set {_character} to character at loop-value in {_input}
47.         if {_character} or (character at (loop-value - 1) in {_input}) is "§":
48.             if "abcdefklmnor0123456789" contains {_character}:
49.                 set {_format} to (join {_format} and "&%{_character}%") if {_character} is not "r", else ("")
50.             continue
51.         set {_output} to join {_output}, "<##%decimalToHexadecimal((0.5 * (sin({_angle}) + 1)) * 255 + {_lightnessModifier})%%decimalToHexadecimal((0.5 * (sin({_angle} + 120) + 1)) * 255 + {_lightnessModifier})%%decimalToHexadecimal((0.5 * (sin({_angle} + 240) + 1)) * 255 + {_lightnessModifier})%>", {_format} and {_character}
52.         remove {_delta} from {_angle}
53.     return colored {_output}
54.
55. # gradient(input: string, startColor: numbers, endColor: numbers)
56. # @description Function that applies a gradient pattern over a given string
57. # @param input the string to be colored
58. # @param color list of Hex colors
59. # @return the colored string in a gradient pattern
60. function gradient(input: string, color: strings) :: string:
61.     loop {_color::*}:
62.         set {_rgb::%loop-index%::*} to HexToRGB(loop-value)
63.     set {_n} to size of {_color::*} -1
64.     set {_delta} to 180 / (length of uncolored {_input}/{_n} - 1)
65.     loop {_n} times:
66.         loop ceil(length of uncolored {_input}/{_n}) times:
67.             set {_y} to ((loop-value-1) -1)*(ceil((length of uncolored {_input})/{_n})) + (loop-value-2)
68.             while character at {_y}+{_x} in {_input} is "§":
69.                 if "abcdefklmnor0123456789" contains character at {_y}+{_x}+1 in {_input}:
70.                     set {_format} to (join {_format} and "&%character at {_y}+{_x}+1 in {_input}%") if character at {_y}+{_x}+1 in {_input} is not "r", else ("")
72.             set {_character} to character at {_y} in uncolored {_input}
73.             set {_startColorProportion} to (0.5 * (sin(({_delta}*((loop-value-2)-1))+90) + 1))
74.             set {_endColorProportion} to (0.5 * (sin(({_delta}*((loop-value-2)-1))+270) + 1))
75.             set {_output} to join {_output}, "<##%decimalToHexadecimal(({_rgb::%loop-value-1%::1} * {_startColorProportion}) + ({_rgb::%((loop-value-1) + 1)%::1} * {_endColorProportion}))%%decimalToHexadecimal(({_rgb::%loop-value-1%::2} * {_startColorProportion}) + ({_rgb::%((loop-value-1) + 1)%::2} * {_endColorProportion}))%%decimalToHexadecimal(({_rgb::%loop-value-1%::3} * {_startColorProportion}) + ({_rgb::%((loop-value-1) + 1)%::3} * {_endColorProportion}))%>", {_format} and {_character}
76.     return {_output}
77.
78.
Code (Text):
1. command exampleRainbow:
2.     trigger:
3.         send formatted rainbow("This is an example text, which will be rainbowified!")
4.         send formatted rainbow("This is an example text with a wrapAmount of 10", 10)
5.         send formatted rainbow("This is an example text with a lightness of 0.7", -1, 0.7)
6.         send formatted rainbow("This is an example text, &6&nWith&r Color &lCodes")
7.
9.     trigger:
10.         send formatted gradient("This is an example text, which will have a gradient applied to", ("FF00FF", "00FF00"))
11.         send formatted gradient("This is an example text with 4 Colors", ("FF00FF", "00FF00","FF0000", "0000FF"))
12.         send formatted gradient("This is an example text &6&nwith&r color &lCodes", ("FF00FF", "00FF00","FF0000", "0000FF"))
13.
14. on chat:
15.     set message to rainbow(colored message, 20, 0.7)

Attached Files:

File size:
21 KB
Views:
412
File size:
16.6 KB
Views:
3,852
• ColorUtils.sk
File size:
5.4 KB
Views:
149
#1
Last edited: Jan 27, 2022 at 7:17 AM
• Like x 5
• Winner x 3
• Useful x 1
2. Bayest Member

Joined:
Sep 26, 2020
Messages:
2
0
THIS IS AMAZING.

#2
3. Ankoki Supporter

Joined:
Nov 5, 2020
Messages:
55
11
this deserves a pog champ c:

#3
• Like x 1
• Friendly x 1

Joined:
Dec 7, 2020
Messages:
1
0
Pog person

#4

Joined:
May 4, 2020
Messages:
2
0
Wow!

#5
6. StunningListen Member

Joined:
Aug 26, 2020
Messages:
14
0
is there a way to get items with custom colours

#6
7. Sorbon Supporter

Joined:
Jul 8, 2020
Messages:
4
5
Set name of player's tool to rainbow(name of player's tool)

#7
8. TruestLucario New Member

Joined:
Jun 5, 2021
Messages:
1
0
creator's a cool guy

#8

Joined:
Jul 8, 2020
Messages:
4
5

#9