Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper RGB color conversion for CLI #1289

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/com/laytonsmith/PureUtilities/TermColors.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ public static void cls() {
@TermColor
public static String RESET = special("reset");

private static final byte[] RGB2ANSI_TABLE = {
0, 4, 4, 4, 2, 8, 12, 12, 2, 2, 6, 6, 10, 10, 10, 14,
1, 5, 4, 4, 8, 8, 12, 12, 2, 2, 6, 7, 10, 10, 10, 14,
1, 9, 5, 12, 1, 9, 5, 12, 11, 7, 7, 7, 10, 10, 14, 14,
1, 9, 5, 13, 1, 9, 5, 13, 3, 3, 7, 13, 11, 11, 11, 15
};

private static final Map<String, String> DEFAULTS = new HashMap<String, String>();
private static List<Field> fields = null;

Expand Down Expand Up @@ -285,6 +292,25 @@ private static String color(Color c, boolean bright, boolean foreground, boolean
return "\033[" + (resetCurrent ? "0;" : "") + color + ";" + (bright ? "1" : "22") + "m";
}

/**
* Converts a RGB color to its (perceptually) closest equivalent.
* @param hex A 6-characters wide hexadecimal-formatted string containing the RRGGBB pattern.
* @return The ANSI string representing such color; {@code null} if an invalid pattern was passed to {@code hex}.
*/
public static String fromRGB(String hex) {
String ret = null;
if(hex.matches("(?i)^[a-z0-9]{6}$")) {
int hexValue = Integer.parseInt(hex, 16);
byte code = RGB2ANSI_TABLE[
16 * Math.round(((hexValue >> 16) & 0xFF) / (float) 0x55)
+ 4 * Math.round(((hexValue >> 8) & 0xFF) / (float) 0x55)
+ Math.round((hexValue & 0xFF) / (float) 0x55)
];
ret = "\033[" + (30 + (code & 7)) + ";" + ((code & 0x8) != 0 ? "1" : "22") + "m";
}
return ret;
}

public static void p(CharSequence c) {
StreamUtils.GetSystemOut().print(c);
StreamUtils.GetSystemOut().flush();
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/laytonsmith/core/Static.java
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,11 @@ public static String MCToANSIColors(String mes) {
if(mes == null) {
return null;
}
if(mes.length() > 13) {
mes = Pattern.compile("(?i)§x(§[0-9A-F]){6}").matcher(mes).replaceAll(match -> {
return TermColors.fromRGB(match.group(0).substring(2).replaceAll("§", ""));
});
}
return mes
.replaceAll("§0", TermColors.BLACK)
.replaceAll("§1", TermColors.BLUE)
Expand All @@ -1203,9 +1208,7 @@ public static String MCToANSIColors(String mes) {
.replaceAll("§m", TermColors.STRIKE)
.replaceAll("§n", TermColors.UNDERLINE)
.replaceAll("§o", TermColors.ITALIC)
.replaceAll("§r", TermColors.RESET)
.replaceAll("(?i)§x(§[a-f0-9]){6}", "");

.replaceAll("§r", TermColors.RESET);
}

public static MCCommandSender GetInjectedPlayer(String name) {
Expand Down