Skip to content

Commit 143e025

Browse files
committed
Fixing gradient applier.
1 parent 4e054ac commit 143e025

File tree

2 files changed

+70
-58
lines changed

2 files changed

+70
-58
lines changed

src/main/java/net/matrixcreations/libraries/utils/GradientUtils.java

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,45 +36,53 @@ private static String applyGradient(String text, String startColor, String endCo
3636
TextColor end = TextColor.fromHexString("#" + endColor);
3737

3838
StringBuilder result = new StringBuilder();
39+
int length = text.length();
3940

40-
boolean bold = false;
41-
boolean italic = false;
42-
boolean underlined = false;
43-
boolean strikethrough = false;
44-
boolean obfuscated = false;
41+
boolean isBold = false;
42+
boolean isItalic = false;
43+
boolean isUnderlined = false;
44+
boolean isStrikethrough = false;
45+
boolean isObfuscated = false;
4546

46-
int length = text.length();
47+
// Iterate through the text to check and apply formatting and gradient colors
4748
for (int i = 0; i < length; i++) {
48-
char currentChar = text.charAt(i);
49+
// Calculate the gradient color based on the position in the text
50+
float ratio = (float) i / (length - 1); // Smooth transition across entire text
51+
TextColor color = ColorInterpolater.interpolateColor(start, end, ratio);
4952

53+
// Check if any formatting should be applied for this part
54+
char currentChar = text.charAt(i);
5055
if (currentChar == '§') {
51-
i++;
52-
char formatCode = text.charAt(i);
53-
switch (formatCode) {
54-
case 'l': bold = true; break;
55-
case 'o': italic = true; break;
56-
case 'n': underlined = true; break;
57-
case 'm': strikethrough = true; break;
58-
case 'k': obfuscated = true; break;
59-
case 'r':
60-
bold = italic = underlined = strikethrough = obfuscated = false;
61-
break;
56+
if (i + 1 < length) {
57+
char formatCode = text.charAt(i + 1);
58+
switch (formatCode) {
59+
case 'l': isBold = true; break;
60+
case 'o': isItalic = true; break;
61+
case 'n': isUnderlined = true; break;
62+
case 'm': isStrikethrough = true; break;
63+
case 'k': isObfuscated = true; break;
64+
case 'r': // Reset formatting
65+
isBold = false;
66+
isItalic = false;
67+
isUnderlined = false;
68+
isStrikethrough = false;
69+
isObfuscated = false;
70+
break;
71+
}
72+
i++; // Skip the formatting code
73+
continue;
6274
}
63-
continue;
6475
}
6576

66-
float ratio = (float) i / length;
67-
TextColor color = ColorInterpolater.interpolateColor(start, end, ratio);
68-
6977
TextComponent.Builder component = Component.text()
7078
.content(String.valueOf(currentChar))
7179
.color(color);
7280

73-
if (bold) component.decorate(TextDecoration.BOLD);
74-
if (italic) component.decorate(TextDecoration.ITALIC);
75-
if (underlined) component.decorate(TextDecoration.UNDERLINED);
76-
if (strikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
77-
if (obfuscated) component.decorate(TextDecoration.OBFUSCATED);
81+
if (isBold) component.decorate(TextDecoration.BOLD);
82+
if (isItalic) component.decorate(TextDecoration.ITALIC);
83+
if (isUnderlined) component.decorate(TextDecoration.UNDERLINED);
84+
if (isStrikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
85+
if (isObfuscated) component.decorate(TextDecoration.OBFUSCATED);
7886

7987
result.append(LegacyComponentSerializer.legacySection().serialize(component.build()));
8088
}

src/main/java/net/matrixcreations/libraries/utils/WithoutHexUtils.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.kyori.adventure.text.format.TextColor;
66
import net.kyori.adventure.text.format.TextDecoration;
77
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
8+
import net.matrixcreations.libraries.interpolaters.ColorInterpolater;
89

910
import java.util.regex.Matcher;
1011
import java.util.regex.Pattern;
@@ -59,53 +60,56 @@ private static String processLegacyCodes(String text) {
5960
}
6061

6162
private static String applyGradient(String text, String startColor, String endColor) {
63+
// Use the hex color strings without the '#' prefix directly
6264
TextColor start = TextColor.fromHexString("#" + startColor);
6365
TextColor end = TextColor.fromHexString("#" + endColor);
6466

6567
StringBuilder result = new StringBuilder();
68+
int length = text.length();
6669

67-
// Initialize variables to track formatting changes
68-
boolean bold = false;
69-
boolean italic = false;
70-
boolean underlined = false;
71-
boolean strikethrough = false;
72-
boolean obfuscated = false;
70+
boolean isBold = false;
71+
boolean isItalic = false;
72+
boolean isUnderlined = false;
73+
boolean isStrikethrough = false;
74+
boolean isObfuscated = false;
7375

74-
int length = text.length();
7576
for (int i = 0; i < length; i++) {
76-
char currentChar = text.charAt(i);
77+
// Calculate the gradient color based on the position in the text
78+
float ratio = (float) i / (length - 1);
79+
TextColor color = ColorInterpolater.interpolateColor(start, end, ratio);
7780

78-
// Handle formatting codes
81+
char currentChar = text.charAt(i);
7982
if (currentChar == '§') {
80-
i++; // Move to next char which holds the format code
81-
char formatCode = text.charAt(i);
82-
switch (formatCode) {
83-
case 'l': bold = true; break;
84-
case 'o': italic = true; break;
85-
case 'n': underlined = true; break;
86-
case 'm': strikethrough = true; break;
87-
case 'k': obfuscated = true; break;
88-
case 'r': // Reset all formatting
89-
bold = italic = underlined = strikethrough = obfuscated = false;
90-
break;
83+
if (i + 1 < length) {
84+
char formatCode = text.charAt(i + 1);
85+
switch (formatCode) {
86+
case 'l': isBold = true; break;
87+
case 'o': isItalic = true; break;
88+
case 'n': isUnderlined = true; break;
89+
case 'm': isStrikethrough = true; break;
90+
case 'k': isObfuscated = true; break;
91+
case 'r': // Reset formatting
92+
isBold = false;
93+
isItalic = false;
94+
isUnderlined = false;
95+
isStrikethrough = false;
96+
isObfuscated = false;
97+
break;
98+
}
99+
i++;
100+
continue;
91101
}
92-
continue; // Skip processing this char
93102
}
94103

95-
// Calculate gradient color interpolation
96-
float ratio = (float) i / length;
97-
TextColor color = interpolateColor(start, end, ratio);
98-
99-
// Build text component with proper formatting
100104
TextComponent.Builder component = Component.text()
101105
.content(String.valueOf(currentChar))
102106
.color(color);
103107

104-
if (bold) component.decorate(TextDecoration.BOLD);
105-
if (italic) component.decorate(TextDecoration.ITALIC);
106-
if (underlined) component.decorate(TextDecoration.UNDERLINED);
107-
if (strikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
108-
if (obfuscated) component.decorate(TextDecoration.OBFUSCATED);
108+
if (isBold) component.decorate(TextDecoration.BOLD);
109+
if (isItalic) component.decorate(TextDecoration.ITALIC);
110+
if (isUnderlined) component.decorate(TextDecoration.UNDERLINED);
111+
if (isStrikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
112+
if (isObfuscated) component.decorate(TextDecoration.OBFUSCATED);
109113

110114
result.append(LegacyComponentSerializer.legacySection().serialize(component.build()));
111115
}

0 commit comments

Comments
 (0)