clan manager: Use Sprite IDs for rank badges

This commit adds `ImageUtil::resizeCanvas`, `ImageUtil::outlineImage`,
and `ImageUtil::fillImage`, and applies these functions to the clan
manager to replace the clan rank images with calls to `SpriteManager`.
This commit is contained in:
Jordan Atwood
2018-07-29 19:38:23 -07:00
parent 8fb36d3ea4
commit d6e465877e
14 changed files with 303 additions and 48 deletions

View File

@@ -28,16 +28,17 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.eventbus.Subscribe;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
@@ -46,26 +47,33 @@ import net.runelite.api.ClanMemberRank;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.IndexedSprite;
import net.runelite.api.SpriteID;
import net.runelite.api.events.ClanChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.Text;
@Singleton
@Slf4j
public class ClanManager
{
private static final String[] CLANCHAT_IMAGES =
private static final int[] CLANCHAT_IMAGES =
{
"Friend_clan_rank.png", "Recruit_clan_rank.png",
"Corporal_clan_rank.png", "Sergeant_clan_rank.png",
"Lieutenant_clan_rank.png", "Captain_clan_rank.png",
"General_clan_rank.png", "Owner_clan_rank.png",
"JMod_clan_rank.png"
SpriteID.CLAN_CHAT_RANK_SMILEY_FRIEND,
SpriteID.CLAN_CHAT_RANK_SINGLE_CHEVRON_RECRUIT,
SpriteID.CLAN_CHAT_RANK_DOUBLE_CHEVRON_CORPORAL,
SpriteID.CLAN_CHAT_RANK_TRIPLE_CHEVRON_SERGEANT,
SpriteID.CLAN_CHAT_RANK_BRONZE_STAR_LIEUTENANT,
SpriteID.CLAN_CHAT_RANK_SILVER_STAR_CAPTAIN,
SpriteID.CLAN_CHAT_RANK_GOLD_STAR_GENERAL,
SpriteID.CLAN_CHAT_RANK_KEY_CHANNEL_OWNER,
SpriteID.CLAN_CHAT_RANK_CROWN_JAGEX_MODERATOR,
};
private int modIconsLength;
private static final Dimension CLANCHAT_IMAGE_DIMENSION = new Dimension(11, 11);
private static final Color CLANCHAT_IMAGE_OUTLINE_COLOR = new Color(33, 33, 33);
private final Client client;
private final SpriteManager spriteManager;
private final BufferedImage[] clanChatImages = new BufferedImage[CLANCHAT_IMAGES.length];
private final LoadingCache<String, ClanMemberRank> clanRanksCache = CacheBuilder.newBuilder()
@@ -74,7 +82,7 @@ public class ClanManager
.build(new CacheLoader<String, ClanMemberRank>()
{
@Override
public ClanMemberRank load(String key) throws Exception
public ClanMemberRank load(@Nonnull String key)
{
final ClanMember[] clanMembersArr = client.getClanMembers();
@@ -92,27 +100,13 @@ public class ClanManager
}
});
private int modIconsLength;
@Inject
private ClanManager(Client client)
private ClanManager(Client client, SpriteManager spriteManager)
{
this.client = client;
int i = 0;
for (String resource : CLANCHAT_IMAGES)
{
try
{
final BufferedImage bufferedImage = rgbaToIndexedBufferedImage(ImageIO
.read(ClanManager.class.getResource(resource)));
clanChatImages[i] = bufferedImage;
}
catch (IOException e)
{
log.warn("unable to load clan image", e);
}
++i;
}
this.spriteManager = spriteManager;
}
public ClanMemberRank getRank(String playerName)
@@ -138,10 +132,9 @@ public class ClanManager
@Subscribe
public void onGameStateChanged(GameStateChanged gameStateChanged)
{
if (gameStateChanged.getGameState() == GameState.LOGIN_SCREEN
if (gameStateChanged.getGameState() == GameState.LOGGED_IN
&& modIconsLength == 0)
{
// this is after "Loading sprites" so we can modify modicons now
loadClanChatIcons();
}
}
@@ -154,25 +147,19 @@ public class ClanManager
private void loadClanChatIcons()
{
try
{
final IndexedSprite[] modIcons = client.getModIcons();
final IndexedSprite[] newModIcons = Arrays.copyOf(modIcons, modIcons.length + CLANCHAT_IMAGES.length);
int curPosition = newModIcons.length - CLANCHAT_IMAGES.length;
final IndexedSprite[] modIcons = client.getModIcons();
final IndexedSprite[] newModIcons = Arrays.copyOf(modIcons, modIcons.length + CLANCHAT_IMAGES.length);
int curPosition = newModIcons.length - CLANCHAT_IMAGES.length;
for (BufferedImage image : clanChatImages)
{
IndexedSprite sprite = createIndexedSprite(client, image);
newModIcons[curPosition++] = sprite;
}
client.setModIcons(newModIcons);
modIconsLength = newModIcons.length;
}
catch (IOException e)
for (int i = 0; i < CLANCHAT_IMAGES.length; i++, curPosition++)
{
log.warn("Failed loading of clan chat icons", e);
final int resource = CLANCHAT_IMAGES[i];
clanChatImages[i] = rgbaToIndexedBufferedImage(clanChatImageFromSprite(spriteManager.getSprite(resource, 0)));
newModIcons[curPosition] = createIndexedSprite(client, clanChatImages[i]);
}
client.setModIcons(newModIcons);
modIconsLength = newModIcons.length;
}
private static String sanitize(String lookup)
@@ -181,7 +168,7 @@ public class ClanManager
return cleaned.replace('\u00A0', ' ');
}
private static IndexedSprite createIndexedSprite(final Client client, final BufferedImage bufferedImage) throws IOException
private static IndexedSprite createIndexedSprite(final Client client, final BufferedImage bufferedImage)
{
final IndexColorModel indexedCM = (IndexColorModel) bufferedImage.getColorModel();
@@ -228,4 +215,10 @@ public class ClanManager
resultIndexedImage.getGraphics().drawImage(sourceBufferedImage, 0, 0, null);
return resultIndexedImage;
}
private static BufferedImage clanChatImageFromSprite(final BufferedImage clanSprite)
{
final BufferedImage clanChatCanvas = ImageUtil.resizeCanvas(clanSprite, CLANCHAT_IMAGE_DIMENSION.width, CLANCHAT_IMAGE_DIMENSION.height);
return ImageUtil.outlineImage(clanChatCanvas, CLANCHAT_IMAGE_OUTLINE_COLOR);
}
}

View File

@@ -112,4 +112,14 @@ public class ColorUtil
{
return String.format("%06x", color.getRGB() & 0xFFFFFF);
}
static boolean isFullyTransparent(final Color color)
{
return color.getAlpha() == 0;
}
static boolean isNotFullyTransparent(final Color color)
{
return !isFullyTransparent(color);
}
}

View File

@@ -24,6 +24,7 @@
*/
package net.runelite.client.util;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
@@ -32,6 +33,7 @@ import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.util.Arrays;
import javax.swing.GrayFilter;
import java.util.function.Predicate;
/**
* Various Image/BufferedImage utilities.
@@ -178,6 +180,26 @@ public class ImageUtil
return ImageUtil.bufferedImageFromImage(resized);
}
/**
* Re-size a BufferedImage's canvas to the given dimensions.
*
* @param image The image whose canvas should be re-sized.
* @param newWidth The width to set the BufferedImage to.
* @param newHeight The height to set the BufferedImage to.
* @return The BufferedImage centered within canvas of given dimensions.
*/
public static BufferedImage resizeCanvas(final BufferedImage image, final int newWidth, final int newHeight)
{
final BufferedImage dimg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
final int centeredX = newWidth / 2 - image.getWidth() / 2;
final int centeredY = newHeight / 2 - image.getHeight() / 2;
final Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(image, centeredX, centeredY, null);
g2d.dispose();
return dimg;
}
/**
* Rotates an image around its center by a given number of radians.
*
@@ -229,6 +251,124 @@ public class ImageUtil
return out;
}
/**
* Outlines non-transparent pixels of a BufferedImage with the given color.
*
* @param image The image to be outlined.
* @param color The color to use for the outline.
* @return The BufferedImage with its edges outlined with the given color.
*/
public static BufferedImage outlineImage(final BufferedImage image, final Color color)
{
return outlineImage(image, color, ColorUtil::isNotFullyTransparent, false);
}
/**
* Outlines pixels of a BufferedImage with the given color, using a given predicate to colorize
* the given image for outlining.
*
* @param image The image to be outlined.
* @param color The color to use for the outline.
* @param fillCondition The predicate to be consumed by {@link #fillImage(BufferedImage, Color, Predicate) fillImage(BufferedImage, Color, Predicate)}
* @return The BufferedImage with its edges outlined with the given color.
*/
public static BufferedImage outlineImage(final BufferedImage image, final Color color, final Predicate<Color> fillCondition)
{
return outlineImage(image, color, fillCondition, false);
}
/**
* Outlines non-transparent pixels of a BufferedImage with the given color. Optionally outlines
* corners in addition to edges.
*
* @param image The image to be outlined.
* @param color The color to use for the outline.
* @param outlineCorners Whether to draw an outline around corners, or only around edges.
* @return The BufferedImage with its edges--and optionally, corners--outlined
* with the given color.
*/
public static BufferedImage outlineImage(final BufferedImage image, final Color color, final Boolean outlineCorners)
{
return outlineImage(image, color, ColorUtil::isNotFullyTransparent, outlineCorners);
}
/**
* Outlines pixels of a BufferedImage with the given color, using a given predicate to colorize
* the given image for outlining. Optionally outlines corners in addition to edges.
*
* @param image The image to be outlined.
* @param color The color to use for the outline.
* @param fillCondition The predicate to be consumed by {@link #fillImage(BufferedImage, Color, Predicate) fillImage(BufferedImage, Color, Predicate)}
* @param outlineCorners Whether to draw an outline around corners, or only around edges.
* @return The BufferedImage with its edges--and optionally, corners--outlined
* with the given color.
*/
public static BufferedImage outlineImage(final BufferedImage image, final Color color, final Predicate<Color> fillCondition, final Boolean outlineCorners)
{
final BufferedImage filledImage = fillImage(image, color, fillCondition);
final BufferedImage outlinedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = outlinedImage.createGraphics();
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if ((x == 0 && y == 0)
|| (!outlineCorners && Math.abs(x) + Math.abs(y) != 1))
{
continue;
}
g2d.drawImage(filledImage, x, y, null);
}
}
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return outlinedImage;
}
/**
* Fills all non-transparent pixels of the given image with the given color.
*
* @param image The image which should have its non-transparent pixels filled.
* @param color The color with which to fill pixels.
* @return The given image with all non-transparent pixels set to the given color.
*/
static BufferedImage fillImage(final BufferedImage image, final Color color)
{
return fillImage(image, color, ColorUtil::isNotFullyTransparent);
}
/**
* Fills pixels of the given image with the given color based on a given fill condition
* predicate.
*
* @param image The image which should have its non-transparent pixels filled.
* @param color The color with which to fill pixels.
* @param fillCondition The condition on which to fill pixels with the given color.
* @return The given image with all pixels fulfilling the fill condition predicate
* set to the given color.
*/
static BufferedImage fillImage(final BufferedImage image, final Color color, final Predicate<Color> fillCondition)
{
final BufferedImage filledImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < filledImage.getWidth(); x++)
{
for (int y = 0; y < filledImage.getHeight(); y++)
{
final Color pixelColor = new Color(image.getRGB(x, y), true);
if (!fillCondition.test(pixelColor))
{
continue;
}
filledImage.setRGB(x, y, color.getRGB());
}
}
return filledImage;
}
/**
* Performs a rescale operation on the image's color components.
*

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 205 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

View File

@@ -96,4 +96,26 @@ public class ColorUtilTest
assertEquals(hex, ColorUtil.colorToHexCode(color));
});
}
@Test
public void isFullyTransparent()
{
for (Color color : COLOR_HEXSTRING_MAP.keySet())
{
assert(!ColorUtil.isFullyTransparent(color));
}
assert(ColorUtil.isFullyTransparent(new Color(0, 0, 0, 0)));
assert(!ColorUtil.isFullyTransparent(new Color(0, 0, 0, 1)));
}
@Test
public void isNotFullyTransparent()
{
for (Color color : COLOR_HEXSTRING_MAP.keySet())
{
assert(ColorUtil.isNotFullyTransparent(color));
}
assert(!ColorUtil.isNotFullyTransparent(new Color(0, 0, 0, 0)));
assert(ColorUtil.isNotFullyTransparent(new Color(0, 0, 0, 1)));
}
}

View File

@@ -35,6 +35,7 @@ import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.util.Arrays;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.ArrayUtils;
import static org.junit.Assert.assertEquals;
@@ -193,6 +194,22 @@ public class ImageUtilTest
}
}
@Test
public void resizeCanvas()
{
assert(bufferedImagesEqual(centeredPixel(BLACK), ImageUtil.resizeCanvas(oneByOne(BLACK), 3, 3)));
assert(bufferedImagesEqual(oneByOne(BLACK), ImageUtil.resizeCanvas(oneByOne(BLACK), 1, 1)));
assert(bufferedImagesEqual(oneByOne(BLACK), ImageUtil.resizeCanvas(centeredPixel(BLACK), 1, 1)));
BufferedImage expected = new BufferedImage(2, 1, BufferedImage.TYPE_INT_ARGB);
expected.setRGB(1, 0, BLACK.getRGB());
assert(bufferedImagesEqual(expected, ImageUtil.resizeCanvas(oneByOne(BLACK), 2, 1)));
expected = new BufferedImage(1, 2, BufferedImage.TYPE_INT_ARGB);
expected.setRGB(0, 1, BLACK.getRGB());
assert(bufferedImagesEqual(expected, ImageUtil.resizeCanvas(oneByOne(BLACK), 1, 2)));
}
@Test
public void rotateImage()
{
@@ -230,6 +247,79 @@ public class ImageUtilTest
assert(bufferedImagesEqual(BLACK_PIXEL_BOTTOM_RIGHT, ImageUtil.flipImage(BLACK_PIXEL_TOP_LEFT, true, true)));
}
@Test
public void fillImage()
{
// fillImage(BufferedImage image, Color color)
assert(bufferedImagesEqual(centeredPixel(GRAY), ImageUtil.fillImage(centeredPixel(BLACK), GRAY)));
assert(bufferedImagesEqual(solidColor(3, 3, GREEN), ImageUtil.fillImage(solidColor(3, 3, BLACK), GREEN)));
assert(bufferedImagesEqual(oneByOne(BLACK_TRANSPARENT), ImageUtil.fillImage(oneByOne(BLACK_TRANSPARENT), WHITE)));
// fillImage(BufferedImage image, Color color, Predicate<Color> fillCondition)
BufferedImage expected = solidColor(CORNER_SIZE, CORNER_SIZE, WHITE);
expected.setRGB(0, 0, new Color(0, true).getRGB());
assert(bufferedImagesEqual(expected, ImageUtil.fillImage(BLACK_PIXEL_TOP_LEFT, WHITE, ColorUtil::isFullyTransparent)));
}
@Test
public void outlineImage()
{
// outlineImage(BufferedImage image, Color color)
BufferedImage expected = new BufferedImage(CENTERED_SIZE, CENTERED_SIZE, BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < expected.getWidth(); x++)
{
for (int y = 0; y < expected.getHeight(); y++)
{
if (x != 1 && y != 1)
{
continue;
}
expected.setRGB(x, y, BLACK.getRGB());
}
}
assert(bufferedImagesEqual(expected, ImageUtil.outlineImage(centeredPixel(BLACK), BLACK)));
expected.setRGB(1, 1, WHITE.getRGB());
assert(bufferedImagesEqual(expected, ImageUtil.outlineImage(centeredPixel(WHITE), BLACK)));
expected = solidColor(CORNER_SIZE, CORNER_SIZE, WHITE);
expected.setRGB(0, 0, BLACK.getRGB());
expected.setRGB(1, 1, new Color(0, true).getRGB());
assert(bufferedImagesEqual(expected, ImageUtil.outlineImage(BLACK_PIXEL_TOP_LEFT, WHITE)));
// outlineImage(BufferedImage image, Color color, Predicate<Color> fillCondition)
BufferedImage test = new BufferedImage(CORNER_SIZE, CORNER_SIZE, BufferedImage.TYPE_INT_ARGB);
test.setRGB(0, 0, BLACK.getRGB());
test.setRGB(1, 0, GRAY.getRGB());
expected = test;
expected.setRGB(0, 1, BLUE.getRGB());
assert(bufferedImagesEqual(expected, ImageUtil.outlineImage(test, BLUE, (color -> color.equals(BLACK)))));
// outlineImage(BufferedImage image, Color color, Boolean outlineCorners)
expected = solidColor(CORNER_SIZE, CORNER_SIZE, WHITE);
expected.setRGB(0, 0, BLACK.getRGB());
assert(bufferedImagesEqual(expected, ImageUtil.outlineImage(BLACK_PIXEL_TOP_LEFT, WHITE, true)));
assert(bufferedImagesEqual(solidColor(3, 3, BLACK), ImageUtil.outlineImage(centeredPixel(BLACK), BLACK, true)));
// outlineImage(BufferedImage image, Color color, Predicate<Color> fillCondition, Boolean outlineCorners)
test = new BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB);
test.setRGB(2, 2, BLACK.getRGB());
test.setRGB(1,2, new Color(50, 50, 50).getRGB());
test.setRGB(3, 2, new Color(100, 100, 100).getRGB());
test.setRGB(2, 3, new Color(150, 150, 150).getRGB());
expected = test;
expected.setRGB(2, 1, RED.getRGB());
expected.setRGB(3, 1, RED.getRGB());
expected.setRGB(4, 1, RED.getRGB());
expected.setRGB(4, 2, RED.getRGB());
expected.setRGB(1, 3, RED.getRGB());
expected.setRGB(3, 3, RED.getRGB());
expected.setRGB(4, 3, RED.getRGB());
expected.setRGB(1, 4, RED.getRGB());
expected.setRGB(2, 4, RED.getRGB());
expected.setRGB(3, 4, RED.getRGB());
Predicate<Color> testPredicate = (color -> ColorUtil.isNotFullyTransparent(color) && color.getRed() > 75 && color.getGreen() > 75 && color.getBlue() > 75);
assert(bufferedImagesEqual(expected, ImageUtil.outlineImage(test, RED, testPredicate, true)));
}
/**
* Compares whether two {@link BufferedImage}s are equal in data.
*