Skip to content

Commit 46ddcd1

Browse files
committed
Extract gradient code to util file
1 parent 245ccaf commit 46ddcd1

File tree

2 files changed

+92
-49
lines changed

2 files changed

+92
-49
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package golden.scnicknamer;
2+
3+
import net.minecraft.text.MutableText;
4+
import net.minecraft.text.Style;
5+
import net.minecraft.text.Text;
6+
7+
import java.util.List;
8+
9+
/**
10+
* Utility class for applying gradient colors to text.
11+
*/
12+
public class GradientUtil {
13+
14+
/**
15+
* Applies a gradient to the given text using the specified colors.
16+
* Expects the number of colors to be less than the number of characters in the text.
17+
*
18+
* @param text The text to apply the gradient to.
19+
* @param colors An array of colors in integer format (0xRRGGBB).
20+
* @return A new Text object with the gradient applied.
21+
*/
22+
public static Text applyGradient(Text text, int[] colors) {
23+
String str = text.getString();
24+
Style style = text.getStyle();
25+
int length = str.length();
26+
27+
if (length < 1 || colors == null || colors.length < 1) { // No text or no colors
28+
return text;
29+
}
30+
if (length == 1 || colors.length == 1) { // Single character or single color
31+
return Text.literal(str).setStyle(style.withColor(colors[0]));
32+
}
33+
34+
MutableText result = Text.empty();
35+
int segments = colors.length - 1;
36+
37+
for (int i = 0; i < length; i++) {
38+
float pos = (float) i / (length - 1);
39+
int seg = (int) (pos * segments);
40+
float localRatio = pos * segments - seg;
41+
42+
int color = interpolateColours(colors[seg], colors[seg + 1], localRatio);
43+
result.append(Text.literal(String.valueOf(str.charAt(i))).setStyle(style.withColor(color)));
44+
}
45+
return result;
46+
}
47+
48+
/**
49+
* Applies a gradient to the given text using the specified hex color strings.
50+
*
51+
* @param text The text to apply the gradient to.
52+
* @param hexColors A list of colors in hex string format.
53+
* @return A new Text object with the gradient applied.
54+
* @see #applyGradient(Text, int[])
55+
*/
56+
public static Text applyGradient(Text text, List<String> hexColors) {
57+
int[] colorInts = hexColors.stream()
58+
.mapToInt(c -> Integer.parseInt(c, 16))
59+
.toArray();
60+
61+
return applyGradient(text, colorInts);
62+
}
63+
64+
/**
65+
* Helper method to interpolate between two colors.
66+
* @param start Start color in 0xRRGGBB format
67+
* @param end End color in 0xRRGGBB format
68+
* @param ratio Ratio between 0 and 1
69+
* @return Interpolated color in 0xRRGGBB format
70+
*/
71+
private static int interpolateColours(int start, int end, float ratio) {
72+
int r = interpolate(start >> 16 & 0xFF, end >> 16 & 0xFF, ratio);
73+
int g = interpolate(start >> 8 & 0xFF, end >> 8 & 0xFF, ratio);
74+
int b = interpolate(start & 0xFF, end & 0xFF, ratio);
75+
76+
return (r << 16) | (g << 8) | b;
77+
}
78+
79+
/**
80+
* Helper method to interpolate between two integer values.
81+
* @param start The start value
82+
* @param end The end value
83+
* @param ratio Ratio between 0 and 1
84+
* @return Interpolated integer value
85+
*/
86+
private static int interpolate(int start, int end, float ratio) {
87+
return (int) (start + (end - start) * ratio);
88+
}
89+
}

src/client/java/golden/scnicknamer/SCNicknamerClient.java

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import java.util.*;
1717

18+
import static golden.scnicknamer.GradientUtil.applyGradient;
19+
1820
/**
1921
* Client-side mod initializer for the Minecraft mod "Spooncraft Name Link".
2022
*/
@@ -89,7 +91,7 @@ static MutableText applyMapping(Text message, DisplayMapping mapping,
8991
MutableText newText = (MutableText) Text.of(replacedText);
9092
newText.setStyle(replacedStyle);
9193
if (true) {
92-
newText = (MutableText) getGradientText(newText, new String[]{mapping.colour, "a9c9ff", "ffbbec", "ffc3a0"});
94+
newText = (MutableText) applyGradient(newText, Arrays.asList(mapping.colour, "a9c9ff", "ffbbec", "ffc3a0"));
9395
}
9496

9597
outputMessage.append(newText);
@@ -175,54 +177,6 @@ public static Text getStyledChat(Text message, boolean replaceName, boolean repl
175177
return outputMessage;
176178
}
177179

178-
/**
179-
* Applies a gradient color effect to the given text using the specified colors.
180-
* @param text The text to which the gradient effect will be applied.
181-
* @param colors An array of color hex strings (without the '#' prefix) defining the gradient stops.
182-
* @return A new Text object with the gradient effect applied.
183-
*/
184-
private static Text getGradientText(Text text, String[] colors) {
185-
String str = text.getString();
186-
Style style = text.getStyle();
187-
int length = str.length();
188-
189-
if (length <= 1 || colors == null || colors.length < 2) {
190-
return text;
191-
}
192-
193-
int[] colorInts = Arrays.stream(colors)
194-
.mapToInt(c -> Integer.parseInt(c, 16))
195-
.toArray();
196-
197-
MutableText result = Text.empty();
198-
int segments = colorInts.length - 1;
199-
200-
for (int i = 0; i < length; i++) {
201-
float pos = (float) i / (length - 1);
202-
int seg = Math.min((int) (pos * segments), segments - 1);
203-
float localRatio = (pos * segments) - seg;
204-
205-
int start = colorInts[seg];
206-
int end = colorInts[seg + 1];
207-
208-
int r = interpolate((start >> 16) & 0xFF, (end >> 16) & 0xFF, localRatio);
209-
int g = interpolate((start >> 8) & 0xFF, (end >> 8) & 0xFF, localRatio);
210-
int b = interpolate(start & 0xFF, end & 0xFF, localRatio);
211-
212-
int color = (r << 16) | (g << 8) | b;
213-
214-
MutableText newChar = Text.literal(String.valueOf(str.charAt(i)))
215-
.setStyle(style.withColor(color));
216-
result.append(newChar);
217-
}
218-
219-
return result;
220-
}
221-
222-
private static int interpolate(int start, int end, float ratio) {
223-
return (int) (start + (end - start) * ratio);
224-
}
225-
226180
/**
227181
* Retrieves the mappings from the specified source URL or the default URL if none is provided.
228182
* If the mod is disabled in the configuration, it disables the mod and logs a warning.

0 commit comments

Comments
 (0)