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 = Math .min ((int ) (pos * segments ), segments - 1 );
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+ }
0 commit comments