From 29777c0b1fc348f2e7253d86adf2b36d908d995e Mon Sep 17 00:00:00 2001 From: Jordan Atwood Date: Tue, 17 Jul 2018 10:20:26 -0700 Subject: [PATCH] Add color utility class In addition to creating `colorTag`, `prependColorTag`, and `wrapWithColorTag` functions, this commit moves `SwingUtil::toHexColor` and `SwingUtil::colorLerp` to this new class. --- .../grandexchange/GrandExchangeOfferSlot.java | 6 +- .../plugins/prayer/PrayerDoseOverlay.java | 4 +- .../client/plugins/xptracker/XpInfoBox.java | 4 +- .../net/runelite/client/util/ColorUtil.java | 115 ++++++++++++++++++ .../net/runelite/client/util/SwingUtil.java | 30 ----- .../runelite/client/util/ColorUtilTest.java | 99 +++++++++++++++ 6 files changed, 221 insertions(+), 37 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/util/ColorUtil.java create mode 100644 runelite-client/src/test/java/net/runelite/client/util/ColorUtilTest.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangeOfferSlot.java b/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangeOfferSlot.java index d252c86029..356d6f843c 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangeOfferSlot.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangeOfferSlot.java @@ -53,8 +53,8 @@ import net.runelite.api.ItemComposition; import net.runelite.client.ui.ColorScheme; import net.runelite.client.ui.FontManager; import net.runelite.client.ui.components.ThinProgressBar; +import net.runelite.client.util.ColorUtil; import net.runelite.client.util.StackFormatter; -import net.runelite.client.util.SwingUtil; @Slf4j public class GrandExchangeOfferSlot extends JPanel @@ -263,12 +263,12 @@ public class GrandExchangeOfferSlot extends JPanel private String htmlTooltip(String value) { - return "Progress: " + value + ""; + return "Progress: " + value + ""; } private String htmlLabel(String key, String value) { - return "" + key + "" + value + ""; + return "" + key + "" + value + ""; } private void switchPanel() diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/prayer/PrayerDoseOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/prayer/PrayerDoseOverlay.java index fd97daf0ce..46b12d5446 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/prayer/PrayerDoseOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/prayer/PrayerDoseOverlay.java @@ -45,7 +45,7 @@ import net.runelite.client.ui.overlay.OverlayLayer; import net.runelite.client.ui.overlay.OverlayPosition; import net.runelite.client.ui.overlay.tooltip.Tooltip; import net.runelite.client.ui.overlay.tooltip.TooltipManager; -import net.runelite.client.util.SwingUtil; +import net.runelite.client.util.ColorUtil; import org.apache.commons.lang3.StringUtils; class PrayerDoseOverlay extends Overlay @@ -160,7 +160,7 @@ class PrayerDoseOverlay extends Overlay final float tickProgress = Math.min(timeSinceLastTick / PULSE_TIME, 1); // Cap between 0 and 1 final double t = tickProgress * Math.PI; // Convert to 0 - pi - graphics.setColor(SwingUtil.colorLerp(START_COLOR, END_COLOR, Math.sin(t))); + graphics.setColor(ColorUtil.colorLerp(START_COLOR, END_COLOR, Math.sin(t))); graphics.setStroke(new BasicStroke(2)); graphics.drawOval(orbInnerX, orbInnerY, orbInnerSize, orbInnerSize); diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/xptracker/XpInfoBox.java b/runelite-client/src/main/java/net/runelite/client/plugins/xptracker/XpInfoBox.java index e44ee342be..c32bda3e06 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/xptracker/XpInfoBox.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/xptracker/XpInfoBox.java @@ -48,9 +48,9 @@ import net.runelite.client.ui.ColorScheme; import net.runelite.client.ui.DynamicGridLayout; import net.runelite.client.ui.FontManager; import net.runelite.client.ui.components.ProgressBar; +import net.runelite.client.util.ColorUtil; import net.runelite.client.util.LinkBrowser; import net.runelite.client.util.StackFormatter; -import net.runelite.client.util.SwingUtil; @Slf4j class XpInfoBox extends JPanel @@ -239,6 +239,6 @@ class XpInfoBox extends JPanel static String htmlLabel(String key, int value) { String valueStr = StackFormatter.quantityToRSDecimalStack(value); - return String.format(HTML_LABEL_TEMPLATE, SwingUtil.toHexColor(ColorScheme.LIGHT_GRAY_COLOR), key, valueStr); + return String.format(HTML_LABEL_TEMPLATE, ColorUtil.toHexColor(ColorScheme.LIGHT_GRAY_COLOR), key, valueStr); } } diff --git a/runelite-client/src/main/java/net/runelite/client/util/ColorUtil.java b/runelite-client/src/main/java/net/runelite/client/util/ColorUtil.java new file mode 100644 index 0000000000..f09cac21c9 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/util/ColorUtil.java @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2018, Jordan Atwood + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.util; + +import java.awt.Color; + +public class ColorUtil +{ + private static final String OPENING_COLOR_TAG_START = " + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.util; + +import java.awt.Color; +import java.util.HashMap; +import java.util.Map; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class ColorUtilTest +{ + private static final Map COLOR_HEXSTRING_MAP = new HashMap() + {{ + put(Color.BLACK, "000000"); + put(new Color(0x1), "000001"); + put(new Color(0x100000), "100000"); + put(Color.RED, "ff0000"); + put(Color.GREEN, "00ff00"); + put(Color.BLUE, "0000ff"); + put(new Color(0xA1B2C3), "a1b2c3"); + put(Color.WHITE, "ffffff"); + }}; + + @Test + public void colorTag() + { + COLOR_HEXSTRING_MAP.forEach((color, hex) -> { + assertEquals("", ColorUtil.colorTag(color)); + }); + } + + @Test + public void prependColorTag() + { + COLOR_HEXSTRING_MAP.forEach((color, hex) -> { + assertEquals("test", ColorUtil.prependColorTag("test", color)); + assertEquals("", ColorUtil.prependColorTag("", color)); + }); + + assertEquals("94/99", ColorUtil.prependColorTag("94" + ColorUtil.prependColorTag("/99", Color.WHITE), Color.RED)); + } + + @Test + public void wrapWithColorTag() + { + COLOR_HEXSTRING_MAP.forEach((color, hex) -> { + assertEquals("test", ColorUtil.wrapWithColorTag("test", color)); + assertEquals("", ColorUtil.wrapWithColorTag("", color)); + }); + } + + @Test + public void toHexColor() + { + COLOR_HEXSTRING_MAP.forEach((color, hex) -> { + assertEquals("#" + hex, ColorUtil.toHexColor(color)); + }); + } + + @Test + public void colorLerp() + { + assertEquals(Color.WHITE, ColorUtil.colorLerp(Color.WHITE, Color.WHITE, 0.9)); + assertEquals(new Color(128, 128, 128), ColorUtil.colorLerp(Color.BLACK, Color.WHITE, 0.5)); + assertEquals(Color.BLACK, ColorUtil.colorLerp(Color.BLACK, Color.CYAN, 0)); + assertEquals(Color.CYAN, ColorUtil.colorLerp(Color.BLACK, Color.CYAN, 1)); + } + + @Test + public void colorToHexCode() + { + COLOR_HEXSTRING_MAP.forEach((color, hex) -> { + assertEquals(hex, ColorUtil.colorToHexCode(color)); + }); + } +}