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.
This commit is contained in:
@@ -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 "<html><body style = 'color:" + SwingUtil.toHexColor(ColorScheme.LIGHT_GRAY_COLOR) + "'>Progress: <span style = 'color:white'>" + value + "</span></body></html>";
|
||||
return "<html><body style = 'color:" + ColorUtil.toHexColor(ColorScheme.LIGHT_GRAY_COLOR) + "'>Progress: <span style = 'color:white'>" + value + "</span></body></html>";
|
||||
}
|
||||
|
||||
private String htmlLabel(String key, String value)
|
||||
{
|
||||
return "<html><body style = 'color:white'>" + key + "<span style = 'color:" + SwingUtil.toHexColor(ColorScheme.LIGHT_GRAY_COLOR) + "'>" + value + "</span></body></html>";
|
||||
return "<html><body style = 'color:white'>" + key + "<span style = 'color:" + ColorUtil.toHexColor(ColorScheme.LIGHT_GRAY_COLOR) + "'>" + value + "</span></body></html>";
|
||||
}
|
||||
|
||||
private void switchPanel()
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Jordan Atwood <jordan.atwood423@gmail.com>
|
||||
* 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 = "<col=";
|
||||
private static final String OPENING_COLOR_TAG_END = ">";
|
||||
private static final String CLOSING_COLOR_TAG = "</col>";
|
||||
|
||||
/**
|
||||
* Creates a color tag from the given color.
|
||||
*
|
||||
* @param color The Color to create a tag from.
|
||||
* @return A string of the color tag for the given color.
|
||||
*/
|
||||
public static String colorTag(Color color)
|
||||
{
|
||||
return OPENING_COLOR_TAG_START + colorToHexCode(color) + OPENING_COLOR_TAG_END;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepends the given str with an opening color tag of the given color.
|
||||
*
|
||||
* @param str The string to be colorized.
|
||||
* @param color The color to be used in the color tag.
|
||||
* @return The passed str with a prepended color tag.
|
||||
*/
|
||||
public static String prependColorTag(final String str, final Color color)
|
||||
{
|
||||
return colorTag(color) + str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the given str with a color tag of the given color.
|
||||
*
|
||||
* @param str The string to be colorized.
|
||||
* @param color The color to be used in the color tag.
|
||||
* @return The passed str wrapped with opening and closing color tags.
|
||||
*/
|
||||
public static String wrapWithColorTag(final String str, final Color color)
|
||||
{
|
||||
return prependColorTag(str, color) + CLOSING_COLOR_TAG;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a given color to it's hexidecimal equivalent.
|
||||
*
|
||||
* @param color Color to get hexidecimal string from.
|
||||
* @return Hexidecimal string representing the given color, in the form "#abcdef".
|
||||
*/
|
||||
public static String toHexColor(final Color color)
|
||||
{
|
||||
return "#" + colorToHexCode(color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Linearly interpolates between colors a and b by t.
|
||||
*
|
||||
* @param a first color
|
||||
* @param b second color
|
||||
* @param t factor
|
||||
* @return interpolated color
|
||||
*/
|
||||
public static Color colorLerp(final Color a, final Color b, final double t)
|
||||
{
|
||||
final double r1 = a.getRed();
|
||||
final double r2 = b.getRed();
|
||||
final double g1 = a.getGreen();
|
||||
final double g2 = b.getGreen();
|
||||
final double b1 = a.getBlue();
|
||||
final double b2 = b.getBlue();
|
||||
|
||||
return new Color(
|
||||
(int) Math.round(r1 + (t * (r2 - r1))),
|
||||
(int) Math.round(g1 + (t * (g2 - g1))),
|
||||
(int) Math.round(b1 + (t * (b2 - b1)))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the RGB hex color code of the passed color.
|
||||
*
|
||||
* @param color The color to get a hex code from.
|
||||
* @return A lower-cased string of the RGB hex code of color.
|
||||
*/
|
||||
static String colorToHexCode(final Color color)
|
||||
{
|
||||
return String.format("%06x", color.getRGB() & 0xFFFFFF);
|
||||
}
|
||||
}
|
||||
@@ -140,14 +140,6 @@ public class SwingUtil
|
||||
return op.filter(image, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a given color to it's hexidecimal equivalent.
|
||||
*/
|
||||
public static String toHexColor(Color color)
|
||||
{
|
||||
return "#" + Integer.toHexString(color.getRGB()).substring(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely sets Swing theme
|
||||
*
|
||||
@@ -373,26 +365,4 @@ public class SwingUtil
|
||||
return SubstanceCoreUtilities.getTitlePaneComponent(frame) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Linearly interpolates between colors a and b by t.
|
||||
* @param a first color
|
||||
* @param b second color
|
||||
* @param t factor
|
||||
* @return interpolated color
|
||||
*/
|
||||
public static Color colorLerp(Color a, Color b, double t)
|
||||
{
|
||||
final double r1 = a.getRed();
|
||||
final double r2 = b.getRed();
|
||||
final double g1 = a.getGreen();
|
||||
final double g2 = b.getGreen();
|
||||
final double b1 = a.getBlue();
|
||||
final double b2 = b.getBlue();
|
||||
|
||||
return new Color(
|
||||
(int) Math.round(r1 + (t * (r2 - r1))),
|
||||
(int) Math.round(g1 + (t * (g2 - g1))),
|
||||
(int) Math.round(b1 + (t * (b2 - b1)))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Jordan Atwood <jordan.atwood423@gmail.com>
|
||||
* 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, String> COLOR_HEXSTRING_MAP = new HashMap<Color, String>()
|
||||
{{
|
||||
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("<col=" + hex + ">", ColorUtil.colorTag(color));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prependColorTag()
|
||||
{
|
||||
COLOR_HEXSTRING_MAP.forEach((color, hex) -> {
|
||||
assertEquals("<col=" + hex + ">test", ColorUtil.prependColorTag("test", color));
|
||||
assertEquals("<col=" + hex + ">", ColorUtil.prependColorTag("", color));
|
||||
});
|
||||
|
||||
assertEquals("<col=ff0000>94<col=ffffff>/99", ColorUtil.prependColorTag("94" + ColorUtil.prependColorTag("/99", Color.WHITE), Color.RED));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapWithColorTag()
|
||||
{
|
||||
COLOR_HEXSTRING_MAP.forEach((color, hex) -> {
|
||||
assertEquals("<col=" + hex + ">test</col>", ColorUtil.wrapWithColorTag("test", color));
|
||||
assertEquals("<col=" + hex + "></col>", 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));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user