refactor: adventure-ify colorutilities
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package me.chayapak1.chomens_bot.plugins;
|
||||
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.util.ColorUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.util.HSVLike;
|
||||
|
||||
public class BruhifyPlugin extends TickPlugin.Listener {
|
||||
private final Bot bot;
|
||||
@@ -12,7 +12,7 @@ public class BruhifyPlugin extends TickPlugin.Listener {
|
||||
|
||||
private int startHue = 0;
|
||||
|
||||
public BruhifyPlugin (Bot bot) {
|
||||
public BruhifyPlugin(Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.tick.addListener(this);
|
||||
@@ -29,8 +29,8 @@ public class BruhifyPlugin extends TickPlugin.Listener {
|
||||
Component component = Component.empty();
|
||||
|
||||
for (char character : displayName.toCharArray()) {
|
||||
String color = String.format("#%06x", ColorUtilities.hsvToRgb(hue, 100, 100));
|
||||
component = component.append(Component.text(character).color(TextColor.fromHexString(color)));
|
||||
component = component.append(Component.text(character)
|
||||
.color(TextColor.color(HSVLike.hsvLike(hue / 360.0f, 1, 1))));
|
||||
hue = (hue + increment) % 360;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,17 @@ package me.chayapak1.chomens_bot.util;
|
||||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.format.TextFormat;
|
||||
import net.kyori.adventure.text.serializer.legacy.CharacterAndFormat;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ColorUtilities {
|
||||
private static final Map<TextFormat, Character> formatToLegacyMap = CharacterAndFormat.defaults().stream()
|
||||
.collect(Collectors.toUnmodifiableMap(CharacterAndFormat::format, CharacterAndFormat::character));
|
||||
private static final Map<Integer, String> ansiToIrcMap = new HashMap<>();
|
||||
private static final Map<Integer, String> ansiStyleToIrcMap = new HashMap<>();
|
||||
|
||||
@@ -16,80 +21,15 @@ public class ColorUtilities {
|
||||
|
||||
if (color.startsWith("#")) return TextColor.fromHexString(color);
|
||||
else {
|
||||
// am i reinventing the wheel here?
|
||||
return switch (color) {
|
||||
case "black" -> NamedTextColor.BLACK;
|
||||
case "dark_blue" -> NamedTextColor.DARK_BLUE;
|
||||
case "dark_green" -> NamedTextColor.DARK_GREEN;
|
||||
case "dark_aqua" -> NamedTextColor.DARK_AQUA;
|
||||
case "dark_red" -> NamedTextColor.DARK_RED;
|
||||
case "dark_purple" -> NamedTextColor.DARK_PURPLE;
|
||||
case "gold" -> NamedTextColor.GOLD;
|
||||
case "gray" -> NamedTextColor.GRAY;
|
||||
case "dark_gray" -> NamedTextColor.DARK_GRAY;
|
||||
case "blue" -> NamedTextColor.BLUE;
|
||||
case "green" -> NamedTextColor.GREEN;
|
||||
case "aqua" -> NamedTextColor.AQUA;
|
||||
case "red" -> NamedTextColor.RED;
|
||||
case "light_purple" -> NamedTextColor.LIGHT_PURPLE;
|
||||
case "yellow" -> NamedTextColor.YELLOW;
|
||||
default -> NamedTextColor.WHITE;
|
||||
};
|
||||
// am i reinventing the wheel here? Yes.
|
||||
return Optional.ofNullable(NamedTextColor.NAMES.value(color))
|
||||
.orElse(NamedTextColor.WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
// Author: ChatGPT
|
||||
public static int hsvToRgb (int hue, int saturation, int value) {
|
||||
Color color = Color.getHSBColor(hue / 360.0f, saturation / 100.0f, value / 100.0f);
|
||||
return color.getRGB() & 0xFFFFFF;
|
||||
}
|
||||
|
||||
private static final ChatColor[] COLORS = {
|
||||
new ChatColor("0", 0x000000),
|
||||
new ChatColor("1", 0x0000aa),
|
||||
new ChatColor("2", 0x00aa00),
|
||||
new ChatColor("3", 0x00aaaa),
|
||||
new ChatColor("4", 0xaa0000),
|
||||
new ChatColor("5", 0xaa00aa),
|
||||
new ChatColor("6", 0xffaa00),
|
||||
new ChatColor("7", 0xaaaaaa),
|
||||
new ChatColor("8", 0x555555),
|
||||
new ChatColor("9", 0x5555ff),
|
||||
new ChatColor("a", 0x55ff55),
|
||||
new ChatColor("b", 0x55ffff),
|
||||
new ChatColor("c", 0xff5555),
|
||||
new ChatColor("d", 0xff55ff),
|
||||
new ChatColor("e", 0xffff55),
|
||||
new ChatColor("f", 0xffffff)
|
||||
};
|
||||
|
||||
public static String getClosestChatColor(int rgb) {
|
||||
int r = (rgb >> 16) & 0xFF;
|
||||
int g = (rgb >> 8) & 0xFF;
|
||||
int b = rgb & 0xFF;
|
||||
|
||||
ChatColor closest = null;
|
||||
int smallestDiff = 0;
|
||||
|
||||
for (ChatColor color : COLORS) {
|
||||
if (color.rgb == rgb) {
|
||||
return color.colorName;
|
||||
}
|
||||
|
||||
// Check by the greatest diff of the 3 values
|
||||
int rAverage = (color.r + r) / 2;
|
||||
int rDiff = color.r - r;
|
||||
int gDiff = color.g - g;
|
||||
int bDiff = color.b - b;
|
||||
int diff = ((2 + (rAverage >> 8)) * rDiff * rDiff)
|
||||
+ (4 * gDiff * gDiff)
|
||||
+ ((2 + ((255 - rAverage) >> 8)) * bDiff * bDiff);
|
||||
if (closest == null || diff < smallestDiff) {
|
||||
closest = color;
|
||||
smallestDiff = diff;
|
||||
}
|
||||
}
|
||||
return closest.colorName;
|
||||
public static char getClosestChatColor(int rgb) {
|
||||
final NamedTextColor closestNamed = NamedTextColor.nearestTo(TextColor.color(rgb));
|
||||
return formatToLegacyMap.get(closestNamed);
|
||||
}
|
||||
|
||||
// Author: ChatGPT
|
||||
@@ -168,19 +108,4 @@ public class ColorUtilities {
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static final class ChatColor {
|
||||
|
||||
private final String colorName;
|
||||
private final int rgb;
|
||||
private final int r, g, b;
|
||||
|
||||
public ChatColor(String colorName, int rgb) {
|
||||
this.colorName = colorName;
|
||||
this.rgb = rgb;
|
||||
r = (rgb >> 16) & 0xFF;
|
||||
g = (rgb >> 8) & 0xFF;
|
||||
b = rgb & 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,8 +216,7 @@ public class ComponentUtilities {
|
||||
// gets the closest color to the hex
|
||||
|
||||
final int rgb = Integer.parseInt(code.substring(1), 16);
|
||||
|
||||
final String chatColor = ColorUtilities.getClosestChatColor(rgb);
|
||||
final String chatColor = Character.toString(ColorUtilities.getClosestChatColor(rgb));
|
||||
|
||||
ansiCode = ansiMap.get(chatColor);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user