-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentGradient.java
More file actions
38 lines (30 loc) · 1.19 KB
/
Copy pathComponentGradient.java
File metadata and controls
38 lines (30 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package ru.cwcode.cwutils.text.component;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import ru.cwcode.cwutils.colors.ColorUtils;
/**
* Не работает с новыми версиями Adventure (с тех пор, как удалили TextColor.lerp)
*/
public class ComponentGradient {
public static Component of(String text, TextColor... colors) {
if (colors.length == 0) {
return Component.text(text);
}
if (colors.length == 1) {
return Component.text(text).color(colors[0]);
}
Component component = Component.empty();
if (colors.length == 2) {
for (int i = 0; i < text.length(); i++) {
component = component.append(Component.text(text.charAt(i)).color(TextColor.lerp((i + 1f) / text.length(), colors[0], colors[1])));
}
return component;
}
float sectionLen = text.length() / (0f + colors.length - 1);
for (int i = 0; i < text.length(); i++) {
component = component.append(Component.text(text.charAt(i))
.color(ColorUtils.lerpManyColors(i, sectionLen, colors)));
}
return component;
}
}