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

feat(color): 为ColorParser支持RGB渐变颜色代码。

This commit is contained in:
2022-11-27 22:53:33 +08:00
parent d2b3224b61
commit 479f4592d1
17 changed files with 130 additions and 89 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -149,7 +149,8 @@ public abstract class EasyPlugin extends JavaPlugin {
/**
* 在主线程执行操作,并支持获取其结果。
*
* @param <T> 结果类型
* @param <T> 结果类型
* @param action 需要执行的内容
* @return CompletableFuture
*/
public @NotNull <T> CompletableFuture<T> supplySync(@NotNull Supplier<T> action) {
@@ -161,7 +162,8 @@ public abstract class EasyPlugin extends JavaPlugin {
/**
* 在异步线程中执行一个操作,并获取操作的结果。
*
* @param <T> 事件类型
* @param <T> 事件类型
* @param action 需要执行的内容
* @return CompletableFuture
*/
public @NotNull <T> CompletableFuture<T> supplyAsync(@NotNull Supplier<T> action) {
@@ -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;
/**
* 颜色解析器。
* <br> 普通颜色 格式 {@code &+颜色代码 },如 {@literal &c} 、{@literal &a}
* <br> RGB颜色(版本需要≥1.14) 格式 {@code &(#XXXXXX) },如 {@literal &(#aaaaaa)}
* <br> 渐变RBG颜色(版本需要≥1.14) 格式 {@code &<#XXXXXX>FOOBAR&<#XXXXXX> }
* <p> 注意:当使用渐变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<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; // 本条消息的结束颜色
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<String> 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<String> 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;
}
}
+38
View File
@@ -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<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));
}
return builder.toString();
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</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.15</version>
<version>1.4.16</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging>
<version>1.4.15</version>
<version>1.4.16</version>
<modules>
<module>base/main</module>