Finding block most matched to RGB Color in Minecraft

Discussion in 'Plugin Development' started by Hoolean, Apr 3, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Hoolean

    I am going to make a plugin that can convert images to pixel art - does anyone know the simplest way of matching an RGB Colour to a block in Minecraft?
     
  2. Play with it, didn't test it at all :p

    Code:java
    1. private Map<String, Byte> colorMap = new HashMap<String, Byte>();
    2.  
    3. @Override
    4. public void onEnable()
    5. {
    6. // Put in here your color codes and item data for wool
    7. colorMap.put("FFFFFF", (byte) 15);
    8. }
    9.  
    10. public byte getClosestColorData(String rgb)
    11. {
    12. return getClosestColorData(Integer.parseInt(rgb.substring(0, 2)), Integer.parseInt(rgb.substring(2, 4)), Integer.parseInt(rgb.substring(4, 6)));
    13. }
    14.  
    15. public byte getClosestColorData(int r1, int g1, int b1)
    16. {
    17. int r2, g2, b2;
    18. double difference = 0;
    19. String closestRGB = null;
    20.  
    21. for (String rawRGB : colorMap.keySet())
    22. {
    23. r2 = Integer.parseInt(rawRGB.substring(0, 2));
    24. g2 = Integer.parseInt(rawRGB.substring(2, 4));
    25. b2 = Integer.parseInt(rawRGB.substring(4, 6));
    26.  
    27. double diff = Math.sqrt((r2 - r1) ^ 2 + (g2 - g1) ^ 2 + (b2 - b1) ^ 2);
    28.  
    29. if (closestRGB == null)
    30. {
    31. closestRGB = rawRGB;
    32. difference = diff;
    33. continue;
    34. }
    35.  
    36. if (diff < difference)
    37. {
    38. closestRGB = rawRGB;
    39. difference = diff;
    40. }
    41. }
    42.  
    43. return colorMap.get(closestRGB);
    44. }[syntax][/syntax]
     
  3. Offline

    JazzaG

  4. Offline

    Hoolean

    Using this code...

    Code:
    public DyeColor getClosestDyeColor(int r1, int g1, int b1)
        {
            int r2, g2, b2;
            double difference = 0;
            DyeColor closestRGB = null;
     
            for (DyeColor dyeColor : DyeColor.values())
            {
                r2 = dyeColor.getColor().getRed();
                g2 = dyeColor.getColor().getGreen();
                b2 = dyeColor.getColor().getBlue();
     
                double diff = Math.sqrt((r2 - r1) ^ 2 + (g2 - g1) ^ 2 + (b2 - b1) ^ 2);
     
                if (closestRGB == null)
                {
                    closestRGB = dyeColor;
                    difference = diff;
                    continue;
                }
     
                if (diff < difference)
                {
                    closestRGB = dyeColor;
                    difference = diff;
                }
            }
     
            return closestRGB;
        }
    Pure red translates as white and orange as red as well as a few other little niggles :(

    See what has happened with this screenshot of np98765 and md_5 's avatars D:

    Aside from that though, it works great so thanks for your help :D 2013-04-03_15.06.31.png
     
  5. Offline

    JazzaG

    Ooh, looks more like Hellsing 's though :p

    I reckon it'll be better to map ranges to colors, there's only 15 of them after all :)
     
Thread Status:
Not open for further replies.

Share This Page