Skip to content

Commit

Permalink
Add 8-bit color to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Feb 20, 2021
1 parent de1330d commit e876a6a
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 74 deletions.
Expand Up @@ -117,7 +117,7 @@ public Collection<Map.Entry<K, ObjectMapValue<K>>> getEntries() {
* @return if that handle exists
*/
public boolean contains(K handle) {
return map.keySet().contains(handle);
return map.containsKey(handle);
}

/**
Expand Down
29 changes: 29 additions & 0 deletions GalaxiAPI/src/net/ME1312/Galaxi/Log/ConsoleTextElement.java
Expand Up @@ -61,11 +61,40 @@ public ConsoleTextElement strikethrough(boolean value) {
return (ConsoleTextElement) super.strikethrough(value);
}

/**
* Set the color of the text
*
* @param color 8-bit Text Color
* @return Text Element
*/
public ConsoleTextElement color(int color) {
if (Logger.writer != null) {
color(Logger.writer.parse256(color));
element.getMap("c").set("8", color);
}
return this;
}

@Override
public ConsoleTextElement color(Color color) {
return (ConsoleTextElement) super.color(color);
}


/**
* Set the background color of the text
*
* @param color 8-bit Background Color
* @return Text Element
*/
public ConsoleTextElement backgroundColor(int color) {
if (Logger.writer != null) {
backgroundColor(Logger.writer.parse256(color));
element.getMap("bc").set("8", color);
}
return this;
}

/**
* Set the background color of the text
*
Expand Down
113 changes: 79 additions & 34 deletions GalaxiAPI/src/net/ME1312/Galaxi/Log/LogStream.java
@@ -1,9 +1,13 @@
package net.ME1312.Galaxi.Log;

import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.Util;

import java.awt.*;
import java.io.*;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;

import static java.nio.charset.StandardCharsets.UTF_8;

Expand Down Expand Up @@ -38,6 +42,7 @@ public void flush() throws IOException {

interface MessageHandler {
void log(String message) throws IOException;
Color parse256(int color);
}

/**
Expand Down Expand Up @@ -91,52 +96,92 @@ private String convert(TextElement original) {
}

ConsoleTextElement element = new ConsoleTextElement(original.element);
if (element.bold()) message.append("\u001B[1m");
if (element.italic()) message.append("\u001B[3m");
if (element.underline()) message.append("\u001B[4m");
if (element.strikethrough()) message.append("\u001B[9m");
if (element.color() != null) {
int red = element.color().getRed();
int green = element.color().getGreen();
int blue = element.color().getBlue();
float alpha = element.color().getAlpha() / 255f;

red = Math.round(alpha * red);
green = Math.round(alpha * green);
blue = Math.round(alpha * blue);

message.append("\u001B[38;2;" + red + ";" + green + ";" + blue + "m");
LinkedList<String> style = new LinkedList<String>();
if (element.bold()) style.add("1");
if (element.italic()) style.add("3");
if (element.underline()) style.add("4");
if (element.strikethrough()) style.add("9");
if (element.element.contains("c")) {
ObjectMap<String> map = element.element.getMap("c");
int color = map.getInt("8", -1);
if (color != -1) {
if (color < 8) {
style.add(String.valueOf(30 + color));
} else if (color < 16) { // 90 + color - 8
style.add(String.valueOf(82 + color));
} else {
style.add("38;5;" + color);
}
} else {
int red = map.getInt("r");
int green = map.getInt("g");
int blue = map.getInt("b");
float alpha = map.getInt("a") / 255f;

red = Math.round(alpha * red);
green = Math.round(alpha * green);
blue = Math.round(alpha * blue);

style.add("38;2;" + red + ";" + green + ";" + blue);
}
}
if (element.element.contains("bc")) {
ObjectMap<String> map = element.element.getMap("bc");
int color = map.getInt("8", -1);
if (color != -1) {
if (color < 8) {
style.add(String.valueOf(40 + color));
} else if (color < 16) { // 100 + color - 8
style.add(String.valueOf(92 + color));
} else {
style.add("48;5;" + color);
}
} else {
int red = map.getInt("r");
int green = map.getInt("g");
int blue = map.getInt("b");
float alpha = map.getInt("a") / 255f;

red = Math.round(alpha * red);
green = Math.round(alpha * green);
blue = Math.round(alpha * blue);

style.add("48;2;" + red + ";" + green + ";" + blue);
}
}
if (element.backgroundColor() != null) {
int red = element.backgroundColor().getRed();
int green = element.backgroundColor().getGreen();
int blue = element.backgroundColor().getBlue();
float alpha = element.backgroundColor().getAlpha() / 255f;

red = Math.round(alpha * red);
green = Math.round(alpha * green);
blue = Math.round(alpha * blue);

message.append("\u001B[48;2;" + red + ";" + green + ";" + blue + "m");
boolean escaped = style.size() != 0;
if (escaped) {
message.append("\u001B[");
for (Iterator<String> i = style.iterator();;) {
message.append(i.next());
if (i.hasNext()) {
message.append(';');
} else {
message.append('m');
break;
}
}
}
if (element.onClick() != null) {
escaped = true;
message.append("\033]99900;" + element.onClick().toString() + "\007");
}
if (element.onClick() != null) message.append("\033]99900;" + element.onClick().toString() + "\007");
message.append(element.message());
message.append("\u001B[m");
if (escaped) message.append("\u001B[m");

try {
for (TextElement e : original.after) message.append(convert(e));
} catch (Throwable e) {
getLogger().error.println(e);
}

if (message.length() > 3 && message.substring(message.length() - 4).equals("\n\u001B[m")) {
return message.substring(0, message.length() - 3);
} else return message.toString();
} else {
message.append("null");
return "null";
}

// hack for formatting over newlines
int length = message.codePointCount(0, message.length());
if (length > 3 && message.codePointAt(length - 4) == '\n') {
return message.substring(0, message.length() - 3);
} else return message.toString();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion GalaxiAPI/src/net/ME1312/Galaxi/Log/Logger.java
Expand Up @@ -17,7 +17,7 @@
public final class Logger {
private static boolean color = false;
private static ExecutorService service = null;
private static LogStream.MessageHandler writer = null;
static LogStream.MessageHandler writer = null;

private static final LinkedList<LogFilter> gFilters = new LinkedList<LogFilter>();
private final LinkedList<LogFilter> lFilters = new LinkedList<LogFilter>();
Expand Down
Expand Up @@ -125,7 +125,7 @@ public void command(CommandSender sender, String handle, String[] args) {
} else {
return new String[0];
}
}).usage("[plugin]").description("Gets the version of the System, Engine, App, and the specified Plugin").help(
}).usage("[plugin]").description("Prints versioning information").help(
"This command will print what OS you're running, your OS version,",
"your Java version, the GalaxiEngine version, and the app version.",
"",
Expand All @@ -149,7 +149,7 @@ public void command(CommandSender sender, String handle, String[] args) {
sender.sendMessage("You do not have permission to access this command");
}
}
}.description("Reload the app settings").help(
}.description("Reloads app settings").help(
"This command will reload the configuration for the app",
"and any plugins that opt-in via the reload event.",
"",
Expand Down Expand Up @@ -193,13 +193,13 @@ public void command(CommandSender sender, String handle, String[] args) {
}

++label;
description += 2;
description += 3;
if (args.length == 0) {
boolean color = false;
Color a = new Color(48, 52, 54), b = new Color(53, 57, 59);
int a = 236, b = 237;
StringBuilder formatted = new StringBuilder("Command Listing");
StringBuilder blank = new StringBuilder(" ");
if (sender instanceof ConsoleCommandSender) for (int limit = label + description + 2, i = blank.length(); i < limit; ++i) {
if (sender instanceof ConsoleCommandSender) for (int limit = label + description + 3, i = blank.length(); i < limit; ++i) {
if (i == formatted.length()) formatted.append(' ');
blank.append(' ');
}
Expand All @@ -213,7 +213,7 @@ public void command(CommandSender sender, String handle, String[] args) {
formatted = new StringBuilder();
color = !color;

formatted.append(" ");
formatted.append(" ");
formatted.append(text);
if (sender instanceof ConsoleCommandSender || cmd.description() != null) {
for (int i = text.length(); i < label; ++i) {
Expand Down Expand Up @@ -261,7 +261,7 @@ public void command(CommandSender sender, String handle, String[] args) {
} else {
return new String[0];
}
}).usage("[command]").description("Prints a list of the commands and/or their descriptions").help(
}).usage("[command]").description("Prints help on using commands").help(
"This command will print a list of all currently registered commands and aliases,",
"along with their usage and a short description.",
"",
Expand Down
35 changes: 35 additions & 0 deletions GalaxiRT/Engine/src/net/ME1312/Galaxi/Engine/Runtime/Console.java
Expand Up @@ -14,6 +14,7 @@

import java.awt.*;
import java.io.IOError;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -89,6 +90,40 @@ protected void closeWindow(boolean destroy) {
}
}

static final Color[] ANSI_COLOR_MAP = new Color[] {
new Color( 0, 0, 0),
new Color(205, 0, 0),
new Color( 37, 188, 36),
new Color(215, 215, 0),
new Color( 0, 0, 195),
new Color(190, 0, 190),
new Color( 0, 165, 220),
new Color(204, 204, 204),
new Color(128, 128, 128),
new Color(255, 0, 0),
new Color( 49, 231, 34),
new Color(255, 255, 0),
new Color( 0, 0, 255),
new Color(255, 0, 255),
new Color( 0, 200, 255),
new Color(255, 255, 255),
};
static Color parse256(int color) {
if (color < 16) {
return ANSI_COLOR_MAP[color];
} else if (color < 232) {
int r = (int) (Math.floor((color - 16) / 36d) * (255 / 5));
int g = (int) (Math.floor(((color - 16) % 36d) / 6d) * (255 / 5));
int b = (int) (Math.floor(((color - 16) % 36d) % 6d) * (255 / 5));
return new Color(r, g, b);
} else if (color < 256) {
int gray = (int) ((255 / 25d) * (color - 232 + 1));
return new Color(gray, gray, gray);
} else {
throw new IllegalArgumentException("Invalid 8-bit color: " + color);
}
}

@Override
public List<String> complete(CommandSender sender, String command) {
if (Util.isNull(sender, command)) throw new NullPointerException();
Expand Down
Expand Up @@ -4,6 +4,7 @@
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Log.LogMessenger;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -108,4 +109,9 @@ void close() {
iwriter = null;
writer = null;
}

@Override
public Color parse256(int color) {
return Console.parse256(color);
}
}

0 comments on commit e876a6a

Please sign in to comment.