From 2773079a09816a343cac55c3b73542817b037c68 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 26 Jan 2022 15:49:07 +0100 Subject: [PATCH] Uses the inverted color for a dyed sign's highlighting color --- .../stargate/container/SignData.java | 11 +++-- .../stargate/portal/PortalSignDrawer.java | 20 +------- .../stargate/utility/ColorHelper.java | 47 +++++++++++++++++++ 3 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 src/main/java/net/knarcraft/stargate/utility/ColorHelper.java diff --git a/src/main/java/net/knarcraft/stargate/container/SignData.java b/src/main/java/net/knarcraft/stargate/container/SignData.java index 9e64e96..38b3076 100644 --- a/src/main/java/net/knarcraft/stargate/container/SignData.java +++ b/src/main/java/net/knarcraft/stargate/container/SignData.java @@ -1,7 +1,7 @@ package net.knarcraft.stargate.container; +import net.knarcraft.stargate.utility.ColorHelper; import net.md_5.bungee.api.ChatColor; -import org.bukkit.Color; import org.bukkit.DyeColor; import org.bukkit.block.Sign; @@ -45,8 +45,7 @@ public class SignData { */ public ChatColor getMainSignColor() { if (dyedColor != DyeColor.BLACK) { - Color color = dyedColor.getColor(); - return ChatColor.of(String.format("#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue())); + return ColorHelper.fromColor(dyedColor.getColor()); } else { return mainSignColor; } @@ -58,7 +57,11 @@ public class SignData { * @return

The highlighting color of the sign

*/ public ChatColor getHighlightSignColor() { - return highlightSignColor; + if (dyedColor != DyeColor.BLACK) { + return ColorHelper.fromColor(ColorHelper.invert(dyedColor.getColor())); + } else { + return highlightSignColor; + } } } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java index b942d5d..299aad7 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java @@ -11,8 +11,8 @@ import org.bukkit.block.BlockState; import org.bukkit.block.Sign; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; + +import static net.knarcraft.stargate.utility.ColorHelper.translateAllColorCodes; /** * The portal sign drawer draws the sing of a given portal @@ -345,22 +345,6 @@ public class PortalSignDrawer { Stargate.logInfo(String.format("Gate layout on line %d does not exist [%s]", lineIndex, gateName)); } - /** - * Translates all found color codes to formatting in a string - * - * @param message

The string to search for color codes

- * @return

The message with color codes translated

- */ - public static String translateAllColorCodes(String message) { - message = ChatColor.translateAlternateColorCodes('&', message); - Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})"); - Matcher matcher = pattern.matcher(message); - while (matcher.find()) { - message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group())); - } - return message; - } - /** * Gets the main color to use for the given sign type * diff --git a/src/main/java/net/knarcraft/stargate/utility/ColorHelper.java b/src/main/java/net/knarcraft/stargate/utility/ColorHelper.java new file mode 100644 index 0000000..027bd66 --- /dev/null +++ b/src/main/java/net/knarcraft/stargate/utility/ColorHelper.java @@ -0,0 +1,47 @@ +package net.knarcraft.stargate.utility; + +import net.md_5.bungee.api.ChatColor; +import org.bukkit.Color; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ColorHelper { + + /** + * Inverts the given color + * + * @param color

The color to invert

+ * @return

The inverted color

+ */ + public static Color invert(Color color) { + return color.setRed(255 - color.getRed()).setGreen(255 - color.getGreen()).setBlue(255 - color.getBlue()); + } + + /** + * Gets the chat color corresponding to the given color + * + * @param color

The color to convert into a chat color

+ * @return

The resulting chat color

+ */ + public static ChatColor fromColor(Color color) { + return ChatColor.of(String.format("#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue())); + } + + /** + * Translates all found color codes to formatting in a string + * + * @param message

The string to search for color codes

+ * @return

The message with color codes translated

+ */ + public static String translateAllColorCodes(String message) { + message = ChatColor.translateAlternateColorCodes('&', message); + Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})"); + Matcher matcher = pattern.matcher(message); + while (matcher.find()) { + message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group())); + } + return message; + } + +}