action) {
diff --git a/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java b/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java
index 805f68d..70528ad 100644
--- a/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java
+++ b/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java
@@ -1,19 +1,28 @@
package cc.carm.lib.easyplugin.utils;
-import net.md_5.bungee.api.ChatColor;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+/**
+ * 颜色解析器。
+ *
普通颜色 格式 {@code &+颜色代码 },如 {@literal &c} 、{@literal &a}
+ *
RGB颜色(版本需要≥1.14) 格式 {@code &(#XXXXXX) },如 {@literal &(#aaaaaa)}
+ *
渐变RBG颜色(版本需要≥1.14) 格式 {@code &<#XXXXXX>FOOBAR&<#XXXXXX> }
+ * 注意:当使用渐变RGB颜色时,普通颜色代码与RGB颜色代码将失效。
+ */
public class ColorParser {
public static final Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([\\da-fA-F]{6})\\)");
+ public static final Pattern GRADIENT_PATTERN = Pattern.compile("&<&?#([\\da-fA-F]{6})>");
public static String parse(String text) {
return parseBaseColor(parseHexColor(text));
@@ -40,78 +49,70 @@ public class ColorParser {
return text;
}
- private static String buildHexColor(String hexCode) {
- return Arrays.stream(hexCode.split(""))
- .map(s -> '§' + s)
+ public static String parseGradientColor(String text) {
+ List colors = new ArrayList<>();
+
+ Matcher matcher = ColorParser.GRADIENT_PATTERN.matcher(text);
+ while (matcher.find()) colors.add(matcher.group(1));
+
+ if (colors.isEmpty()) return text; // 无渐变颜色,直接跳出
+
+ String[] parts = ColorParser.GRADIENT_PATTERN.split(text);
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < parts.length; i++) {
+ String startHex = i - 1 >= 0 && colors.size() > i - 1 ? colors.get(i - 1) : null; // 本条消息的起始颜色
+ String endHex = colors.size() > i ? colors.get(i) : null; // 本条消息的结束颜色
+ builder.append(gradientText(parts[i], startHex, endHex));
+ }
+
+ return builder.toString();
+ }
+
+ public static String gradientText(@NotNull String text, @Nullable String startHex, @Nullable String endHex) {
+ if (startHex == null || endHex == null) return gradientText(text, (Color) null, null);
+ return gradientText(text, Color.decode("0x" + startHex), Color.decode("0x" + endHex));
+ }
+
+ public static String gradientText(@NotNull String text, @Nullable Color startColor, @Nullable Color endColor) {
+ if (startColor == null || endColor == null || text.isEmpty()) return text; // 有任一为空,则不进行渐变上色
+ if (text.length() == 1) return buildHexColor(colorToHex(startColor)) + text;// 只有一个字符,无需渐变
+
+ String[] characters = text.split("");
+ int step = characters.length; // 变换次数
+
+ int[] directions = new int[]{
+ startColor.getRed() < endColor.getRed() ? 1 : -1,
+ startColor.getGreen() < endColor.getGreen() ? 1 : -1,
+ startColor.getBlue() < endColor.getBlue() ? 1 : -1
+ }; // 决定每种颜色变换的方向
+
+ int rStep = Math.abs(startColor.getRed() - endColor.getRed()) / (step - 1);
+ int gStep = Math.abs(startColor.getGreen() - endColor.getGreen()) / (step - 1);
+ int bStep = Math.abs(startColor.getBlue() - endColor.getBlue()) / (step - 1);
+
+ String[] hexes = IntStream.range(0, step).mapToObj(i -> colorToHex(
+ startColor.getRed() + rStep * i * directions[0],
+ startColor.getGreen() + gStep * i * directions[1],
+ startColor.getBlue() + bStep * i * directions[2]
+ )).toArray(String[]::new);
+
+ return IntStream.range(0, characters.length)
+ .mapToObj(i -> buildHexColor(hexes[i]) + characters[i])
+ .collect(Collectors.joining());
+ }
+
+ protected static String colorToHex(Color color) {
+ return colorToHex(color.getRed(), color.getGreen(), color.getBlue());
+ }
+
+ protected static String colorToHex(int r, int g, int b) {
+ // 将R、G、B转换为16进制(若非2位则补0)输出
+ return String.format("%02X%02X%02X", r, g, b);
+ }
+
+ protected static String buildHexColor(String hexCode) {
+ return Arrays.stream(hexCode.split("")).map(s -> '§' + s)
.collect(Collectors.joining("", '§' + "x", ""));
}
-
- private static final String RAW_GRADIENT_HEX_REGEX = "<\\$#[A-Fa-f0-9]{6}>";
-
- public static String gradient(String legacyMsg) {
- List hexes = new ArrayList<>();
- Matcher matcher = Pattern.compile(RAW_GRADIENT_HEX_REGEX).matcher(legacyMsg);
- while (matcher.find()) {
- hexes.add(matcher.group().replace("<$", "").replace(">", ""));
- }
- int hexIndex = 0;
- List texts = new LinkedList<>(Arrays.asList(legacyMsg.split(RAW_GRADIENT_HEX_REGEX)));
- StringBuilder finalMsg = new StringBuilder();
- for (String text : texts) {
- if (texts.get(0).equalsIgnoreCase(text)) {
- finalMsg.append(text);
- continue;
- }
- if (text.length() == 0) continue;
- if (hexIndex + 1 >= hexes.size()) {
- if (!finalMsg.toString().contains(text)) finalMsg.append(text);
- continue;
- }
- String fromHex = hexes.get(hexIndex);
- String toHex = hexes.get(hexIndex + 1);
- finalMsg.append(insertFades(text, fromHex, toHex, text.contains("&l"), text.contains("&o"), text.contains("&n"), text.contains("&m"), text.contains("&k")));
- hexIndex++;
- }
- return finalMsg.toString().replace("&","§");
- }
-
- private static String insertFades(String msg, String fromHex, String toHex, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean magic) {
- msg = msg.replaceAll("&k", "");
- msg = msg.replaceAll("&l", "");
- msg = msg.replaceAll("&m", "");
- msg = msg.replaceAll("&n", "");
- msg = msg.replaceAll("&o", "");
- int length = msg.length();
- Color fromRGB = Color.decode(fromHex);
- Color toRGB = Color.decode(toHex);
- double rStep = Math.abs((double) (fromRGB.getRed() - toRGB.getRed()) / length);
- double gStep = Math.abs((double) (fromRGB.getGreen() - toRGB.getGreen()) / length);
- double bStep = Math.abs((double) (fromRGB.getBlue() - toRGB.getBlue()) / length);
- if (fromRGB.getRed() > toRGB.getRed()) rStep = -rStep; //200, 100
- if (fromRGB.getGreen() > toRGB.getGreen()) gStep = -gStep; //200, 100
- if (fromRGB.getBlue() > toRGB.getBlue()) bStep = -bStep; //200, 100
- Color finalColor = new Color(fromRGB.getRGB());
- msg = msg.replaceAll(RAW_GRADIENT_HEX_REGEX, "");
- msg = msg.replace("", "<$>");
- for (int index = 0; index <= length; index++) {
- int red = (int) Math.round(finalColor.getRed() + rStep);
- int green = (int) Math.round(finalColor.getGreen() + gStep);
- int blue = (int) Math.round(finalColor.getBlue() + bStep);
- if (red > 255) red = 255; if (red < 0) red = 0;
- if (green > 255) green = 255; if (green < 0) green = 0;
- if (blue > 255) blue = 255; if (blue < 0) blue = 0;
- finalColor = new Color(red, green, blue);
- String hex = "#" + Integer.toHexString(finalColor.getRGB()).substring(2);
- String formats = "";
- if (bold) formats += "l";
- if (italic) formats += "o";
- if (underlined) formats += "n";
- if (strikethrough) formats += "m";
- if (magic) formats += "k";
- // Todo ChatColor 能替换掉就好了
- msg = msg.replaceFirst("<\\$>", "" + ChatColor.stripColor(hex) + formats);
- }
- return msg;
- }
}
\ No newline at end of file
diff --git a/base/main/src/test/java/GradientTest.java b/base/main/src/test/java/GradientTest.java
new file mode 100644
index 0000000..862ea8a
--- /dev/null
+++ b/base/main/src/test/java/GradientTest.java
@@ -0,0 +1,38 @@
+import cc.carm.lib.easyplugin.utils.ColorParser;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+
+public class GradientTest {
+
+
+ @Test
+ public void test() {
+ System.out.println(" ");
+ System.out.println(parseGradientColor("&<#AAAAAA>我真的&<#BBBBBB>爱死&<#111111>你&<#FFFFFF>!"));
+ }
+
+
+ public static String parseGradientColor(String text) {
+ List colors = new ArrayList<>();
+
+ Matcher matcher = ColorParser.GRADIENT_PATTERN.matcher(text);
+ while (matcher.find()) colors.add(matcher.group(1));
+
+ if (colors.isEmpty()) return text; // 无渐变颜色,直接跳出
+
+ String[] parts = ColorParser.GRADIENT_PATTERN.split(text);
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < parts.length; i++) {
+ String startHex = i - 1 >= 0 && colors.size() > i - 1 ? colors.get(i - 1) : null; // 本条消息的起始颜色
+ String endHex = colors.size() > i ? colors.get(i) : null; // 本条消息的结束颜色
+ System.out.println("[" + startHex + "]" + parts[i] + "[" + endHex + "]");
+ builder.append(ColorParser.gradientText(parts[i], startHex, endHex));
+ }
+
+ return builder.toString();
+ }
+
+}
diff --git a/base/storage/pom.xml b/base/storage/pom.xml
index 5337151..9486fca 100644
--- a/base/storage/pom.xml
+++ b/base/storage/pom.xml
@@ -5,7 +5,7 @@
easyplugin-parent
cc.carm.lib
- 1.4.15
+ 1.4.16
../../pom.xml
4.0.0
diff --git a/collection/all/pom.xml b/collection/all/pom.xml
index d79d336..568b31f 100644
--- a/collection/all/pom.xml
+++ b/collection/all/pom.xml
@@ -5,7 +5,7 @@
easyplugin-parent
cc.carm.lib
- 1.4.15
+ 1.4.16
../../pom.xml
4.0.0
diff --git a/collection/bom/pom.xml b/collection/bom/pom.xml
index bfe8339..fbb84c9 100644
--- a/collection/bom/pom.xml
+++ b/collection/bom/pom.xml
@@ -5,7 +5,7 @@
easyplugin-parent
cc.carm.lib
- 1.4.15
+ 1.4.16
../../pom.xml
4.0.0
diff --git a/collection/common/pom.xml b/collection/common/pom.xml
index 00b3a18..458d9fc 100644
--- a/collection/common/pom.xml
+++ b/collection/common/pom.xml
@@ -5,7 +5,7 @@
easyplugin-parent
cc.carm.lib
- 1.4.15
+ 1.4.16
../../pom.xml
4.0.0
diff --git a/extension/gh-checker/pom.xml b/extension/gh-checker/pom.xml
index 417ee50..a1ccd47 100644
--- a/extension/gh-checker/pom.xml
+++ b/extension/gh-checker/pom.xml
@@ -5,7 +5,7 @@
easyplugin-parent
cc.carm.lib
- 1.4.15
+ 1.4.16
../../pom.xml
4.0.0
diff --git a/extension/papi/pom.xml b/extension/papi/pom.xml
index 5f317b0..e3fd573 100644
--- a/extension/papi/pom.xml
+++ b/extension/papi/pom.xml
@@ -5,7 +5,7 @@
easyplugin-parent
cc.carm.lib
- 1.4.15
+ 1.4.16
../../pom.xml
4.0.0
diff --git a/extension/vault/pom.xml b/extension/vault/pom.xml
index 3d959f5..af15064 100644
--- a/extension/vault/pom.xml
+++ b/extension/vault/pom.xml
@@ -5,7 +5,7 @@
easyplugin-parent
cc.carm.lib
- 1.4.15
+ 1.4.16
../../pom.xml
4.0.0
diff --git a/pom.xml b/pom.xml
index 1ad3fd9..a546ef6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -15,7 +15,7 @@
cc.carm.lib
easyplugin-parent
pom
- 1.4.15
+ 1.4.16
base/main