1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-04 08:38:17 +08:00

feat(color): 继续完善渐变色解析。

This commit is contained in:
2022-11-28 15:52:04 +08:00
parent 3d52d5db15
commit 62e7370622
16 changed files with 98 additions and 45 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.16</version>
<version>1.4.17</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.16</version>
<version>1.4.17</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.16</version>
<version>1.4.17</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.16</version>
<version>1.4.17</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.16</version>
<version>1.4.17</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.16</version>
<version>1.4.17</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -4,9 +4,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -23,9 +22,11 @@ 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可用的格式化代码
public static String parse(String text) {
return parseBaseColor(parseHexColor(parseGradientColor(text)));
return parseBaseColor(parseGradientColor(parseHexColor(text)));
}
public static String[] parse(String... texts) {
@@ -68,7 +69,7 @@ public class ColorParser {
* @param text 消息内容
* @return RGB渐变处理后的消息
*/
public static String parseGradientColor(String text) {
public static @NotNull String parseGradientColor(@NotNull String text) {
List<String> colors = new ArrayList<>();
Matcher matcher = ColorParser.GRADIENT_PATTERN.matcher(text);
@@ -87,14 +88,29 @@ public class ColorParser {
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 @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;
}
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;// 只有一个字符,无需渐变
// 用于记录消息中特殊格式的位置
// 在渐变中,允许使用格式字符与颜色字符来改变其中某个字的颜色/格式,以支持更多形式内容。
LinkedHashMap<Integer, String> 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; // 变换次数
@@ -104,6 +120,7 @@ public class ColorParser {
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);
@@ -115,10 +132,34 @@ public class ColorParser {
)).toArray(String[]::new);
return IntStream.range(0, characters.length)
.mapToObj(i -> buildHexColor(hexes[i]) + characters[i])
.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());
}
+31 -19
View File
@@ -1,10 +1,12 @@
import cc.carm.lib.easyplugin.utils.ColorParser;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import static cc.carm.lib.easyplugin.utils.ColorParser.parse;
import static cc.carm.lib.easyplugin.utils.ColorParser.parseGradientColor;
public class GradientTest {
@@ -12,27 +14,37 @@ public class GradientTest {
public void test() {
System.out.println(" ");
System.out.println(parseGradientColor("&<#AAAAAA>我真的&<#BBBBBB>爱死&<#111111>你&<#FFFFFF>"));
// 测试穿插
System.out.println(parse("&<#AAAAAA>&l我&r真&b的&<#BBBBBB>&o爱死&<#111111>你&<#FFFFFF>了&r"));
System.out.println(parse("&<#AAAAAA>&l我&r真&(#666666)的&<#BBBBBB>&o爱死&<#111111>你&<#FFFFFF>了&r"));
System.out.println(parse("&r正常的颜色理应&c&l不受影响&r。"));
}
public static String parseGradientColor(String text) {
List<String> 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));
@Test
public void formatReadTest() {
LinkedHashMap<Integer, String> formats = new LinkedHashMap<>();
String text = "&k&l &m&1我&k爱你爱你爱你&o吗?";
Matcher matcher = ColorParser.FORMAT_PATTERN.matcher(text);
while (matcher.find()) {
String code = matcher.group();
formats.put(matcher.start(), code);
text = matcher.replaceFirst("");
matcher.reset(text);
}
return builder.toString();
formats.forEach((index, code) -> System.out.println(index + " -> " + code));
String[] parts = text.split("");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
String format = formats.get(i);
if (format != null) builder.append(ColorParser.parseBaseColor(format));
builder.append(parts[i]);
}
System.out.println(builder);
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.16</version>
<version>1.4.17</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>