新增渐变色特殊语法

This commit is contained in:
zhangyuheng 2024-08-12 15:46:30 +08:00
parent 9a4e93bf75
commit 323df4cd08
2 changed files with 87 additions and 3 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.lunadeer</groupId>
<artifactId>MinecraftPluginUtils</artifactId>
<version>1.3.4-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>

View File

@ -5,6 +5,7 @@ import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.ChatColor;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
@ -13,6 +14,7 @@ import java.util.regex.Pattern;
public class ColorParser {
public static TextComponent getComponentType(String text) {
text = parseGradient(text);
String[] parts = text.split("&#");
List<TextComponent> components = new ArrayList<>();
for (String part : parts) {
@ -21,7 +23,7 @@ public class ColorParser {
}
TextColor color = TextColor.fromHexString("#ffffff");
String content;
if (part.length() > 6 && part.substring(0, 6).matches("^[0-9a-fA-F]{6}$")) {
if (part.length() >= 6 && part.substring(0, 6).matches("^[0-9a-fA-F]{6}$")) {
String color_str = part.substring(0, 6);
color = TextColor.fromHexString("#" + color_str);
content = part.substring(6);
@ -45,6 +47,7 @@ public class ColorParser {
* @return String
*/
public static String getBukkitType(String text) {
text = parseGradient(text);
String title = "&f" + text + "&f";
title = title.replaceAll("&#", "#");
Pattern pattern = Pattern.compile("#[a-fA-F0-9]{6}");
@ -61,6 +64,7 @@ public class ColorParser {
}
public static String getPlainText(String text) {
text = parseGradient(text);
String[] parts = text.split("&#");
StringBuilder res = new StringBuilder();
for (String part : parts) {
@ -68,7 +72,7 @@ public class ColorParser {
continue;
}
String content;
if (part.length() > 6 && part.substring(0, 6).matches("^[0-9a-fA-F]{6}$")) {
if (part.length() >= 6 && part.substring(0, 6).matches("^[0-9a-fA-F]{6}$")) {
content = part.substring(6);
} else {
content = part;
@ -78,4 +82,84 @@ public class ColorParser {
return res.toString();
}
/**
* 解析渐变色语法
* <gradient:#2D8CF0:#19BE6B:#FF9900>这个是渐变色效果</gradient>
* 转换为 &#2d8cf0这&#279aca个&#22a9a4是&#1cb77e渐&#3ab95c变&#7cae3d色&#bda41f效&#ff9900果
*
* @param text 渐变色语法
* @return String
*/
private static String parseGradient(String text) {
if (!text.contains("<gradient:")) {
return text;
}
StringBuilder result = new StringBuilder();
int startIndex = 0;
while (startIndex < text.length()) {
int openTagIndex = text.indexOf("<gradient:", startIndex);
if (openTagIndex == -1) {
// 如果没有找到<gradient>标签直接追加剩余部分
result.append(text.substring(startIndex));
break;
}
// 追加标签前的普通文本
result.append(text, startIndex, openTagIndex);
int closeTagIndex = text.indexOf("</gradient>", openTagIndex);
if (closeTagIndex == -1) {
closeTagIndex = text.length();
text += "</gradient>";
}
// 提取颜色代码和内容
String gradientPart = text.substring(openTagIndex + 10, text.indexOf(">", openTagIndex));
String[] colors = gradientPart.split(":");
String content = text.substring(text.indexOf(">", openTagIndex) + 1, closeTagIndex);
result.append(applyGradient(content, colors));
startIndex = closeTagIndex + "</gradient>".length();
}
XLogger.debug("parseGradient %s", result.toString());
return result.toString();
}
private static String applyGradient(String content, String[] colors) {
StringBuilder gradientText = new StringBuilder();
int length = content.length();
for (int i = 0; i < length; i++) {
float ratio = (float) i / (length - 1);
Color color = interpolateColor(colors, ratio);
String hexColor = String.format("#%06x", color.getRGB() & 0xFFFFFF);
gradientText.append("&#").append(hexColor.substring(1)).append(content.charAt(i));
}
return gradientText.toString();
}
private static Color interpolateColor(String[] colors, float ratio) {
int segment = (int) (ratio * (colors.length - 1));
// 防止越界如果计算出的 segment 索引等于 colors.length - 1确保下一个颜色仍然在数组范围内
if (segment >= colors.length - 1) {
return Color.decode(colors[colors.length - 1]);
}
float segmentRatio = ratio * (colors.length - 1) - segment;
Color color1 = Color.decode(colors[segment]);
Color color2 = Color.decode(colors[segment + 1]);
int red = (int) (color1.getRed() * (1 - segmentRatio) + color2.getRed() * segmentRatio);
int green = (int) (color1.getGreen() * (1 - segmentRatio) + color2.getGreen() * segmentRatio);
int blue = (int) (color1.getBlue() * (1 - segmentRatio) + color2.getBlue() * segmentRatio);
return new Color(red, green, blue);
}
}