/** * Return the "distance" between two colors. * * @param color1 First color [r,g,b]. * @param color2 Second color [r,g,b]. * @return Distance bwetween colors. */ public static double colorDistance(double[] color1, double[] color2) { return ColorUtil.colorDistance(color1[0], color1[1], color1[2], color2[0], color2[1], color2[2]); }
/** * Check if a color is more dark than light. Useful if an entity of * this color is to be labeled: Use white label on a "dark" color and * black label on a "light" color. * * @param r Color to check (red part). * @param g Color to check (green part). * @param b Color to check (blue part). * @return True if this is a "dark" color, false otherwise. */ public static boolean isDark(double r, double g, double b) { // Measure distance to white and black respectively double dWhite = ColorUtil.colorDistance(r, g, b, 1.0, 1.0, 1.0); double dBlack = ColorUtil.colorDistance(r, g, b, 0.0, 0.0, 0.0); return dBlack < dWhite; }
/** * Return the "distance" between two colors. * * @param color1 First color. * @param color2 Second color. * @return Distance between colors. */ public static double colorDistance(Color color1, Color color2) { float rgb1[] = new float[3]; float rgb2[] = new float[3]; color1.getColorComponents(rgb1); color2.getColorComponents(rgb2); return ColorUtil.colorDistance(rgb1[0], rgb1[1], rgb1[2], rgb2[0], rgb2[1], rgb2[2]); }