/** * Make an even blend between two colors. * * @param color1 First color to blend. * @param color2 Second color to blend. * @return Blended color. */ public static Color blend(Color color1, Color color2) { return ColorUtil.blend(color1, color2, 0.5); }
/** * 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 color Color to check. * @return True if this is a "dark" color, false otherwise. */ public static boolean isDark(Color color) { float r = color.getRed() / 255.0f; float g = color.getGreen() / 255.0f; float b = color.getBlue() / 255.0f; return isDark(r, g, b); } }
public void paintComponent(Graphics g) { if (backgroundImage != null) { double scaleX = getWidth() / (double)backgroundImage.getWidth(null); double scaleY = getHeight() / (double)backgroundImage.getHeight(null); AffineTransform xform = AffineTransform.getScaleInstance(scaleX, scaleY); ((Graphics2D)g).drawImage(backgroundImage, xform, this); return; } Color stopColor = endColor; Color starterColor = startColor; Color customStartColor = (Color)UIManager.get("CollapsiblePane.startColor"); Color customEndColor = (Color)UIManager.get("CollapsiblePane.endColor"); if (customEndColor != null) { stopColor = customEndColor; } if (customStartColor != null) { starterColor = customStartColor; } if (isSubPane()) { stopColor = ColorUtil.lighter(stopColor, 0.05); } Graphics2D g2 = (Graphics2D)g; int w = getWidth(); int h = getHeight(); GradientPaint gradient = new GradientPaint(0, 0, starterColor, w, h, stopColor, true); g2.setPaint(gradient); g2.fillRect(0, 0, w, h); }
/** * 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]); }