Advertisement
Guest User

ColorUtil.java

a guest
Feb 9th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.HashMap;
  3. import java.util.Map.Entry;
  4.  
  5. /**
  6.  *
  7.  * @author ECB2 (ECB2.biz)
  8.  *
  9.  */
  10.  
  11. public class ColorUtil {
  12.  
  13.     private static HashMap<Integer,Color> mapColors;
  14.    
  15.     public static void init()
  16.     {
  17.         final Color[] BaseMapColors = new Color[]
  18.                 {
  19.                         new Color(0, 0, 0, 0),
  20.                         new Color(127, 178, 56),
  21.                         new Color(247, 233, 163),
  22.                         new Color(167, 167, 167),
  23.                         new Color(255, 0, 0),
  24.                         new Color(160, 160, 255),
  25.                         new Color(167, 167, 167),
  26.                         new Color(0, 124, 0),
  27.                         new Color(255, 255, 255),
  28.                         new Color(164, 168, 184),
  29.                         new Color(183, 106, 47),
  30.                         new Color(112, 112, 112),
  31.                         new Color(64, 64, 255),
  32.                         new Color(104, 83, 50),
  33.                         new Color(255, 252, 245),
  34.                         new Color(216, 127, 51),
  35.                         new Color(178, 76, 216),
  36.                         new Color(102, 153, 216),
  37.                         new Color(229, 229, 51),
  38.                         new Color(127, 204, 25),
  39.                         new Color(242, 127, 165),
  40.                         new Color(76, 76, 76),
  41.                         new Color(153, 153, 153),
  42.                         new Color(76, 127, 153),
  43.                         new Color(127, 63, 178),
  44.                         new Color(51, 76, 178),
  45.                         new Color(102, 76, 51),
  46.                         new Color(102, 127, 51),
  47.                         new Color(153, 51, 51),
  48.                         new Color(25, 25, 25),
  49.                         new Color(250, 238, 77),
  50.                         new Color(92, 219, 213),
  51.                         new Color(74, 128, 255),
  52.                         new Color(0, 217, 58),
  53.                         new Color(21, 20, 31),
  54.                         new Color(112, 2, 0)
  55.                 };
  56.                 mapColors = new HashMap<Integer,Color>();
  57.                 for(int i = 0; i < BaseMapColors.length; ++i)
  58.                 {
  59.                         Color bc = BaseMapColors[i];
  60.                         mapColors.put(i*4 +0,new Color((int)(bc.getRed()*180.0/255.0+0.5), (int)(bc.getGreen()*180.0/255.0+0.5), (int)(bc.getBlue()*180.0/255.0+0.5), bc.getAlpha()));
  61.                         mapColors.put(i*4 +1,new Color((int)(bc.getRed()*220.0/255.0+0.5), (int)(bc.getGreen()*220.0/255.0+0.5), (int)(bc.getBlue()*220.0/255.0+0.5), bc.getAlpha()));
  62.                         mapColors.put(i*4 +2,bc);
  63.                         mapColors.put(i*4 +3,new Color((int)(bc.getRed()*135.0/255.0+0.5), (int)(bc.getGreen()*135.0/255.0+0.5), (int)(bc.getBlue()*135.0/255.0+0.5), bc.getAlpha()));
  64.                 }
  65.     }
  66.    
  67.     private static double colorDiff(Color c1, Color c2)
  68.     {
  69.         double r1 = c1.getRed();
  70.         double g1 = c1.getGreen();
  71.         double b1 = c1.getBlue();
  72.         double r2 = c2.getRed();
  73.         double g2 = c2.getGreen();
  74.         double b2 = c2.getBlue();
  75.         double distance = (r2 - r1)*(r2 - r1) + (g2 - g1)*(g2 - g1) + (b2 - b1)*(b2 - b1);
  76.         return distance;
  77.     }
  78.    
  79.     public static Color getClosestColor(Color c)
  80.     {
  81.         Color out = new Color(0,0,0);
  82.         double lastDistance = Double.MAX_VALUE;
  83.        
  84.         for(Entry<Integer,Color> e:mapColors.entrySet())
  85.         {
  86.             Color curColor = e.getValue();
  87.             double diff = colorDiff(curColor,c);
  88.             if(lastDistance > diff){
  89.                 lastDistance = diff;
  90.                 out = curColor;
  91.             }
  92.         }
  93.        
  94.         return out;
  95.     }
  96.     public static int findId(Color c)
  97.     {
  98.         Color closest = getClosestColor(c);
  99.         return getId(closest);
  100.     }
  101.    
  102.     public static int getId(Color c)
  103.     {
  104.         byte id = (byte)0;
  105.         for(Entry<Integer,Color> e:mapColors.entrySet())
  106.         {
  107.             if(e.getValue().equals(c)) id = (byte)(int)e.getKey();
  108.             //break;
  109.         }
  110.         int unsigned = id & (0xff);
  111.         return unsigned;
  112.     }
  113.    
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement