From 01250a65d13660ca7f08bf1447abb560f5d3bb10 Mon Sep 17 00:00:00 2001 From: carm Date: Sat, 18 Feb 2023 19:06:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor(color):=20=E7=8B=AC=E7=AB=8B=E9=A2=9C?= =?UTF-8?q?=E8=89=B2=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/color/pom.xml | 50 +++++ .../lib/easyplugin/utils/ColorParser.java | 0 .../src/test/java/ColorParseTest.java | 0 base/command/pom.xml | 2 +- base/conf/pom.xml | 2 +- base/database/pom.xml | 2 +- base/gui/pom.xml | 2 +- base/listener/pom.xml | 2 +- base/main/pom.xml | 2 +- .../lib/easyplugin/utils/ColorParser.java | 208 ++++++++++++++++++ base/storage/pom.xml | 2 +- base/utils/pom.xml | 11 +- collection/all/pom.xml | 2 +- collection/bom/pom.xml | 2 +- collection/common/pom.xml | 2 +- extension/gh-checker/pom.xml | 2 +- extension/papi/pom.xml | 2 +- extension/vault/pom.xml | 2 +- pom.xml | 3 +- 19 files changed, 283 insertions(+), 15 deletions(-) create mode 100644 base/color/pom.xml rename base/{utils => color}/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java (100%) rename base/{utils => color}/src/test/java/ColorParseTest.java (100%) create mode 100644 base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java diff --git a/base/color/pom.xml b/base/color/pom.xml new file mode 100644 index 0000000..ec863a0 --- /dev/null +++ b/base/color/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + cc.carm.lib + easyplugin-parent + 1.4.20 + ../../pom.xml + + + ${project.jdk.version} + ${project.jdk.version} + UTF-8 + UTF-8 + + easyplugin-color + + EasyPlugin-Color + 轻松插件颜色模块,支持简单便捷的颜色解析器,包括基本颜色、RGB颜色与RGB渐变颜色。 + https://github.com/CarmJos/EasyPlugin + + + + CarmJos + Carm Jos + carm@carm.cc + https://www.carm.cc + + + + + + The MIT License + https://opensource.org/licenses/MIT + + + + + GitHub Issues + https://github.com/CarmJos/EasyPlugin/issues + + + + GitHub Actions + https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml + + + \ No newline at end of file diff --git a/base/utils/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java b/base/color/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java similarity index 100% rename from base/utils/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java rename to base/color/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java diff --git a/base/utils/src/test/java/ColorParseTest.java b/base/color/src/test/java/ColorParseTest.java similarity index 100% rename from base/utils/src/test/java/ColorParseTest.java rename to base/color/src/test/java/ColorParseTest.java diff --git a/base/command/pom.xml b/base/command/pom.xml index 2627d27..5acbd45 100644 --- a/base/command/pom.xml +++ b/base/command/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/base/conf/pom.xml b/base/conf/pom.xml index 2aff869..3357341 100644 --- a/base/conf/pom.xml +++ b/base/conf/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/base/database/pom.xml b/base/database/pom.xml index 1f34b0a..db45da4 100644 --- a/base/database/pom.xml +++ b/base/database/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/base/gui/pom.xml b/base/gui/pom.xml index 3f54529..d719f77 100644 --- a/base/gui/pom.xml +++ b/base/gui/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/base/listener/pom.xml b/base/listener/pom.xml index 95b23b1..582bb2c 100644 --- a/base/listener/pom.xml +++ b/base/listener/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/base/main/pom.xml b/base/main/pom.xml index afd44f5..77253df 100644 --- a/base/main/pom.xml +++ b/base/main/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 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 new file mode 100644 index 0000000..1b7867f --- /dev/null +++ b/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java @@ -0,0 +1,208 @@ +package cc.carm.lib.easyplugin.utils; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.awt.*; +import java.util.List; +import java.util.*; +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 final Pattern COLOR_PATTERN = Pattern.compile("([&§][0-9a-fA-FrRxX])+"); // 会影响颜色的代码 + public static final Pattern FORMAT_PATTERN = Pattern.compile("([&§][0-9a-fA-Fk-oK-OrRxX])+"); // MC可用的格式化代码 + + /** + * 清除一条消息中的全部颜色代码 (包括RGB颜色代码与渐变颜色代码) + * + * @param text 源消息内容 + * @return 清理颜色后的消息 + */ + public static @NotNull String clear(@NotNull String text) { + text = HEX_PATTERN.matcher(text).replaceAll(""); + text = GRADIENT_PATTERN.matcher(text).replaceAll(""); + text = COLOR_PATTERN.matcher(text).replaceAll(""); + return text; + } + + /** + * 对一条消息进行颜色解析,包括普通颜色代码、RGB颜色代码与RBG渐变代码。 + * + * @param text 源消息内容 + * @return 解析后的消息 + */ + public static @NotNull String parse(@NotNull String text) { + return parseBaseColor(parseGradientColor(parseHexColor(text))); + } + + /** + * 对多条消息进行颜色解析,包括普通颜色代码、RGB颜色代码与RBG渐变代码。 + * + * @param texts 源消息内容 + * @return 解析后的消息 + */ + public static @NotNull String[] parse(@NotNull String... texts) { + return parse(Arrays.asList(texts)).toArray(new String[0]); + } + + /** + * 对多条消息进行颜色解析,包括普通颜色代码、RGB颜色代码与RBG渐变代码。 + * + * @param texts 源消息内容 + * @return 解析后的消息 + */ + public static @NotNull List parse(@NotNull Collection texts) { + return texts.stream().map(ColorParser::parse).collect(Collectors.toList()); + } + + /** + * 解析消息中的基本颜色代码格式 {@code &+颜色代码 },如 {@literal &c} 、{@literal &a} + * + * @param text 消息内容 + * @return RGB处理后的消息 + * @see net.md_5.bungee.api.ChatColor + */ + public static String parseBaseColor(final String text) { + return text.replaceAll("&", "§").replace("§§", "&"); + } + + /** + * 解析消息中的RGB颜色代码(版本需要≥1.14) 格式 {@code &(#XXXXXX) },如 {@literal &(#aaaaaa)} + * + * @param text 消息内容 + * @return RGB处理后的消息 + */ + public static String parseHexColor(String text) { + Matcher matcher = HEX_PATTERN.matcher(text); + while (matcher.find()) { + text = matcher.replaceFirst(buildHexColor(matcher.group(1)).toLowerCase()); + matcher.reset(text); + } + return text; + } + + /** + * 对一条消息进行RGB渐变处理(版本需要≥1.14),格式 {@code &<#XXXXXX>FOOBAR&<#XXXXXX> }。 + * + * @param text 消息内容 + * @return RGB渐变处理后的消息 + */ + public static @NotNull String parseGradientColor(@NotNull 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 @NotNull String gradientText(@NotNull String text, + @Nullable Color startColor, @Nullable Color endColor) { + Objects.requireNonNull(text, "Text to be gradient should not be null!"); + if (startColor == null || endColor == null || text.isEmpty()) { + // 起始颜色有任一为空,则不进行渐变上色。 + // 若有起始颜色,则代表其跟在某个渐变之后,应当添加"&r"阻断前面的渐变。 + return (startColor != null ? "&r" : "") + text; + } + + // 用于记录消息中特殊格式的位置 + // 在渐变中,允许使用格式字符与颜色字符来改变其中某个字的颜色/格式,以支持更多形式内容。 + LinkedHashMap extraFormats = new LinkedHashMap<>(); + Matcher matcher = ColorParser.FORMAT_PATTERN.matcher(text); + while (matcher.find()) { + extraFormats.put(matcher.start(), matcher.group()); + text = matcher.replaceFirst(""); + matcher.reset(text); + } + + if (text.length() == 1) { + // 当只有一个实际字符时,无需进行渐变计算,直接返回 中间颜色+起始格式(如果有)+消息 即可。 + return colorText(text, extraFormats.get(0), buildHexColor(mediumHex(startColor, endColor))); + } + + String[] characters = text.split(""); + int step = characters.length; // 变换次数 + + // 决定每种颜色变换的方向 + int rDirection = startColor.getRed() < endColor.getRed() ? 1 : -1; + int gDirection = startColor.getGreen() < endColor.getGreen() ? 1 : -1; + int bDirection = 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 * rDirection, + startColor.getGreen() + gStep * i * gDirection, + startColor.getBlue() + bStep * i * bDirection + )).toArray(String[]::new); + + return IntStream.range(0, characters.length) + .mapToObj(i -> colorText(characters[i], extraFormats.get(i), buildHexColor(hexes[i]))) + .collect(Collectors.joining()); + } + + protected static String gradientText(@NotNull String text, @Nullable String startHex, @Nullable String endHex) { + return gradientText(text, + startHex == null ? null : Color.decode("0x" + startHex), + endHex == null ? null : Color.decode("0x" + endHex) + ); + } + + private static String mediumHex(@NotNull Color start, @NotNull Color end) { + return colorToHex( + Math.abs(start.getRed() - end.getRed()) / 2, + Math.abs(start.getGreen() - end.getGreen()) / 2, + Math.abs(start.getBlue() - end.getBlue()) / 2 + ); + } + + private static String colorText(String message, @Nullable String format, @Nullable String color) { + if (format != null && COLOR_PATTERN.matcher(format).find()) { + // format中存在影响颜色的内容,则当前消息的颜色会被覆盖。 + // 为了减少最终消息的长度,故直接返回键入的FORMAT和对应消息的内容。 + return format + message; + } + return (color == null ? "" : color) + (format == null ? "" : parseBaseColor(format)) + message; + } + + 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", "")); + } + +} \ No newline at end of file diff --git a/base/storage/pom.xml b/base/storage/pom.xml index eb91941..345784f 100644 --- a/base/storage/pom.xml +++ b/base/storage/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/base/utils/pom.xml b/base/utils/pom.xml index e127164..84f6268 100644 --- a/base/utils/pom.xml +++ b/base/utils/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 @@ -22,6 +22,15 @@ 轻松插件工具类模块,该模块中的内容支持在Bungee、Bukkit使用。 https://github.com/CarmJos/EasyPlugin + + + ${project.groupId} + easyplugin-color + ${project.version} + compile + + + CarmJos diff --git a/collection/all/pom.xml b/collection/all/pom.xml index 67b020b..e4783f9 100644 --- a/collection/all/pom.xml +++ b/collection/all/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/collection/bom/pom.xml b/collection/bom/pom.xml index 30e901f..9557b8b 100644 --- a/collection/bom/pom.xml +++ b/collection/bom/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/collection/common/pom.xml b/collection/common/pom.xml index 03df911..fb81e08 100644 --- a/collection/common/pom.xml +++ b/collection/common/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/extension/gh-checker/pom.xml b/extension/gh-checker/pom.xml index 80ceb98..38d7db6 100644 --- a/extension/gh-checker/pom.xml +++ b/extension/gh-checker/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/extension/papi/pom.xml b/extension/papi/pom.xml index 5d41741..2765946 100644 --- a/extension/papi/pom.xml +++ b/extension/papi/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/extension/vault/pom.xml b/extension/vault/pom.xml index b16cc43..8d27610 100644 --- a/extension/vault/pom.xml +++ b/extension/vault/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.19 + 1.4.20 ../../pom.xml 4.0.0 diff --git a/pom.xml b/pom.xml index 6fc297a..28008a8 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ cc.carm.lib easyplugin-parent pom - 1.4.19 + 1.4.20 base/utils @@ -34,6 +34,7 @@ collection/all collection/bom collection/common + base/color