Skip to content

Commit

Permalink
feat(color): 渐变色支持在其中添加额外格式代码
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Sep 4, 2023
1 parent 19f25db commit 2efdc5f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ public static String parseHexColor(String text) {
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());
StringBuilder sb = new StringBuilder();
String extra = null;
for (int i = 0; i < characters.length; i++) {
extra = buildExtraFormat(extra, extraFormats.get(i));
String s = colorText(characters[i], extra, buildHexColor(hexes[i]));
sb.append(s);
}
return sb.toString();
}

protected static String gradientText(@NotNull String text, @Nullable String startHex, @Nullable String endHex) {
Expand Down Expand Up @@ -205,4 +210,13 @@ protected static String buildHexColor(String hexCode) {
.collect(Collectors.joining("", '§' + "x", ""));
}

protected static String buildExtraFormat(String current, String extra) {
if (extra != null) current = (current == null ? "" : current) + extra;
return isResetCode(current) ? null : current;
}

protected static boolean isResetCode(String input) {
return input != null && (input.toLowerCase().endsWith("&r") || input.toLowerCase().endsWith("§r"));
}

}
2 changes: 1 addition & 1 deletion base/color/src/test/java/ColorParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void test() {
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&m我真的&o尊的&r真的&<#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。"));

Expand Down

0 comments on commit 2efdc5f

Please sign in to comment.