diff options
| author | OnyxDarkKnight <sor1n.iliutza16@gmail.com> | 2015-02-03 21:09:09 +0000 |
|---|---|---|
| committer | OnyxDarkKnight <sor1n.iliutza16@gmail.com> | 2015-02-03 21:09:09 +0000 |
| commit | 3f4c717de5ebc9b942d65ae45ac87c43bdf8a31b (patch) | |
| tree | 15c68c8b61dbd0b7f44937a5c790c4b844e50f58 /java/darkknight/jewelrycraft/item/ItemMoltenMetal.java | |
| parent | 73ca377dc01f859dabd7b07738cb7aeb762272b1 (diff) | |
Made a few modifications to some classes, added earrings and bracelets. Improved coloring algorithm and balanced the Thieving Glove
Diffstat (limited to 'java/darkknight/jewelrycraft/item/ItemMoltenMetal.java')
| -rw-r--r-- | java/darkknight/jewelrycraft/item/ItemMoltenMetal.java | 75 |
1 files changed, 54 insertions, 21 deletions
diff --git a/java/darkknight/jewelrycraft/item/ItemMoltenMetal.java b/java/darkknight/jewelrycraft/item/ItemMoltenMetal.java index 53f1926..ef098f5 100644 --- a/java/darkknight/jewelrycraft/item/ItemMoltenMetal.java +++ b/java/darkknight/jewelrycraft/item/ItemMoltenMetal.java @@ -1,8 +1,13 @@ package darkknight.jewelrycraft.item; import java.awt.image.BufferedImage; -import java.io.File; import java.io.IOException; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import javax.imageio.ImageIO; @@ -53,7 +58,6 @@ public class ItemMoltenMetal extends Item String domain = "", texture; IResourceManager rm = Minecraft.getMinecraft().getResourceManager(); BufferedImage icon; - int x = 0, y = 0, ok = 0, red, green, blue; if (stack != null && JewelryNBT.ingot(stack) != null && Item.getIdFromItem(JewelryNBT.ingot(stack).getItem()) > 0 && JewelryNBT.ingot(stack).getIconIndex() != null && JewelryNBT.ingotColor(stack) == 16777215) { IIcon itemIcon = JewelryNBT.ingot(stack).getItem().getIcon(JewelryNBT.ingot(stack), 0); @@ -70,32 +74,61 @@ public class ItemMoltenMetal extends Item else ingot = new ResourceLocation(domain.toLowerCase(), "textures/blocks/" + texture); icon = ImageIO.read(rm.getResource(ingot).getInputStream()); - while (ok == 0) - { - red = (icon.getRGB(x, y) >> 16) & 0xFF; - green = (icon.getRGB(x, y) >> 8) & 0xFF; - blue = icon.getRGB(x, y) & 0xFF; - if (!isColorPretty(red, green, blue)) + int height = icon.getHeight(); + int width = icon.getWidth(); + + Map m = new HashMap(); + for (int i = 0; i < width; i++) + for (int j = 0; j < height; j++) { - if (x < icon.getTileWidth() - 1) x++; - if (x >= icon.getTileWidth() - 1 && y < icon.getTileWidth() - 1) - { - x = 0; - y++; - } - if (x == icon.getTileWidth() - 1 && y == icon.getTileWidth() - 1) ok = 1; + int rgb = icon.getRGB(i, j); + int red = (rgb >> 16) & 0xff; + int green = (rgb >> 8) & 0xff; + int blue = (rgb) & 0xff; + int[] rgbArr = {red, green, blue}; + int Cmax = Math.max(red, Math.max(green, blue)); + int Cmin = Math.min(red, Math.min(green, blue)); + if (!isGray(rgbArr)) m.put(rgb, (Cmax + Cmin)/2); } - else ok = 1; + try + { + int color = getMostCommonColour(m); + if (JewelryNBT.ingot(stack) != null && JewelryNBT.ingot(stack).getItem().getColorFromItemStack(JewelryNBT.ingot(stack), 1) != 16777215) JewelryNBT.addIngotColor(stack, JewelryNBT.ingot(stack).getItem().getColorFromItemStack(JewelryNBT.ingot(stack), 1)); + else JewelryNBT.addIngotColor(stack, color); + } + catch (Exception e) + { + JewelryNBT.addIngotColor(stack, 16777215); } - JewelryNBT.addIngotColor(stack, icon.getRGB(x, y)); } if (JewelryNBT.ingot(stack) != null) return JewelryNBT.ingotColor(stack); - return 16777215; + return 0; + } + + public static int getMostCommonColour(Map map) + { + List list = new LinkedList(map.entrySet()); + Collections.sort(list, new Comparator() + { + public int compare(Object o1, Object o2) + { + return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); + } + }); + Map.Entry me = (Map.Entry) list.get(list.size() - 1); + for (int i = 0; i < list.size(); i++) + { + float alpha = Float.valueOf(list.get(i).toString().split("=")[1]); + if (alpha < 180) me = (Map.Entry) list.get(i); + } + int rgb = (Integer) me.getKey(); + return rgb; } - public static boolean isColorPretty(int r, int g, int b) + public static boolean isGray(int[] rgbArr) { - if ((r >= 100 && g >= 100 && b >= 100 && r < 230 && b < 230 && g < 230) || ((r >= 100 && (g < 100 || b < 100)) || (g >= 100 && (r < 100 || b < 100)) || (b >= 100 && (g < 100 || r < 100)))) return true; - else return false; + int rgbSum = rgbArr[0] + rgbArr[1] + rgbArr[2]; + if (rgbSum > 0 && rgbSum < 256 * 3) { return false; } + return true; } } |
