ColorUtils 1.16+

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

Tags:
1. Sorbon VIPSupporter

Joined:
Jul 8, 2020
Messages:
7
6
Color Utils
By Mr. Darth and Sorbon

12.11.2022 Bugfix MC 1.19.2 / Skript 2.6.3
Skript broky, here fixy
You only need to update the Rainbow function

03.02.2022 Update
Decreased parsing time, increased performance by a little
Please note that the script currently doesn't work with 2.6.1, should be fixed in 2.6.2

What is Color Utils?

With update 1.16 a new feature was added, which allows us to use hex codes for colors.
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:

Patter: RGBToHex(number: numbers)
Return Value: String / Text

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

Code (Text):
1. function RGBToHex(number: numbers) :: string:
2.     loop {_number::*}:
3.         set {_r} to join {_r}, (character at (mod((floor(min(max(loop-value, 0), 255) / 16)), 16) + 1) in "0123456789ABCDEF") and (character at ((mod(min(max(loop-value, 0), 255), 16)) + 1) in "0123456789ABCDEF")
4.     return colored "<##%{_r}%>"
5.

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 in lower case
6.         add (index of first element out of (split {_n} at "") in "0123456789abcdefklmnor" -1)* 16 to {_rgb::%loop-value-1%}
7.         add index of 2nd element out of (split {_n} at "") in "0123456789abcdefklmnor"-1  to {_rgb::%loop-value-1%}
8.     return {_rgb::*}
9.

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

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.             loop 3 times:
17.                 set {_n::%loop-value-3%} to ({_rgb::%loop-value-1%::%loop-value-3%} * {_startColorProportion}) + ({_rgb::%((loop-value-1) + 1)%::%loop-value-3%} * {_endColorProportion})
18.             set {_output} to join {_output}, RGBToHex({_n::*}), {_format} and {_character}
19.     return colored {_output}
20.

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.3
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. # Update 2.3: Decreased Parsing time and increased performance
13.
14. # RGBToHex(number: numbers)
15. # @description Function converts RGB Value to the HEX representation
16. # @param number: list of numbers between 0 and 255
17. # @return the hexadecimal representation of the numbers
18. function RGBToHex(number: numbers) :: string:
19.     loop {_number::*}:
20.         set {_r} to join {_r}, (character at (mod((floor(min(max(loop-value, 0), 255) / 16)), 16) + 1) in "0123456789ABCDEF") and (character at ((mod(min(max(loop-value, 0), 255), 16)) + 1) in "0123456789ABCDEF")
21.     return colored "<##%{_r}%>"
22.
23. #HexToRGB(hex: string)
24. # @description Function to convert a hex value to the 3 Color values
25. # @param: 6 letter hex string
26. # @return 3 numbers
27. function HexToRGB(hex: string) :: numbers:
28.     replace all "##" with "" in {_hex}
29.     set {_s::*} to split "0123456789abcdefklmnor" at ""
30.     loop 3 times:
31.         set {_n} to subtext of {_hex} from characters (loop-value -1)*2+1 to loop-value*2 in lower case
32.         add (index of first element out of (split {_n} at "") in "0123456789abcdefklmnor" -1)* 16 to {_rgb::%loop-value-1%}
33.         add index of 2nd element out of (split {_n} at "") in "0123456789abcdefklmnor"-1  to {_rgb::%loop-value-1%}
34.     return {_rgb::*}
35.
36. # rainbow(input: string, wrap: boolean = false, wrapAmount: number = 10, lightness: number = 0)
37. # @description Function that 'rainbowifies' a given string (id est colors the string in a rainbow pattern - ROYGBIV)
38. # @param input the string to be rainbowified
39. # @param wrapAmount if this value is positive, the rainbow pattern will start repeating approximately every wrapAmount characters
40. # @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
41. # @return the rainbowified string
42. function rainbow(input: string, wrapAmount: number = -1, lightness: number = 0.5) :: string:
43.     set {_delta} to 360 / ({_wrapAmount} if {_wrapAmount} > 0, else length of {_input})
44.     set {_lightnessModifier} to (({_lightness}*2)-1) * 255
45.     set {_angle} to 90
46.     loop length of {_input} times:
47.         set {_character} to character at loop-value in {_input}
48.         if {_character} or (character at (loop-value - 1) in {_input}) is "§":
49.             if "abcdefklmnor0123456789" contains {_character}:
50.                 set {_format} to (join {_format} and "&%{_character}%") if {_character} is not "r", else ("")
51.             continue
52.         loop 3 times:
53.             set {_n::%loop-value-2%} to (0.5 * (sin(({_angle} + (loop-number-2*120-120))) + 1)) * 255 + {_lightnessModifier}
54.         set {_output} to join {_output}, RGBToHex({_n::*}), {_format} and {_character}
55.         remove {_delta} from {_angle}
56.     return colored {_output}
57.
58. # gradient(input: string, startColor: numbers, endColor: numbers)
59. # @description Function that applies a gradient pattern over a given string
60. # @param input the string to be colored
61. # @param color list of Hex colors
62. # @return the colored string in a gradient pattern
63. function gradient(input: string, color: strings) :: string:
64.     loop {_color::*}:
65.         set {_rgb::%loop-index%::*} to HexToRGB(loop-value)
66.     set {_n} to size of {_color::*} -1
67.     set {_delta} to 180 / (length of uncolored {_input}/{_n} - 1)
68.     loop {_n} times:
69.         loop ceil(length of uncolored {_input}/{_n}) times:
70.             set {_y} to ((loop-value-1) -1)*(ceil((length of uncolored {_input})/{_n})) + (loop-value-2)
71.             while character at {_y}+{_x} in {_input} is "§":
72.                 if "abcdefklmnor0123456789" contains character at {_y}+{_x}+1 in {_input}:
73.                     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 ("")
75.             set {_character} to character at {_y} in uncolored {_input}
76.             set {_startColorProportion} to (0.5 * (sin(({_delta}*((loop-value-2)-1))+90) + 1))
77.             set {_endColorProportion} to (0.5 * (sin(({_delta}*((loop-value-2)-1))+270) + 1))
78.             loop 3 times:
79.                 set {_n::%loop-value-3%} to ({_rgb::%loop-value-1%::%loop-value-3%} * {_startColorProportion}) + ({_rgb::%((loop-value-1) + 1)%::%loop-value-3%} * {_endColorProportion})
80.             set {_output} to join {_output}, RGBToHex({_n::*}), {_format} and {_character}
81.     return colored {_output}
82.
83.
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)

#1
Last edited: Nov 12, 2022
Jande, tcrnado, Ankoki and 2 others like this.
2. Bayest Member

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

#2
3. Ankoki ModeratorSupporter

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

#3
Mr. Darth likes this.

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 VIPSupporter

Joined:
Jul 8, 2020
Messages:
7
6
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:
7
6

#9