/*
 * Copyright (C) 2004 NNL Technology AB
 * Visit www.infonode.net for information about InfoNode(R)
 * products and how to contact NNL Technology AB.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */
// $Id: ColorUtil.java,v 1.10 2005/02/16 11:28:14 jesper Exp $
package api.infonode.util;

import java.awt.*;

public final class ColorUtil {

	private ColorUtil() {
	}

	public static Color getOpposite(Color c) {
		return isDark(c) ? Color.WHITE : Color.BLACK;
	}

	public static Color shade(Color c, double amount) {
		return blend(c, getOpposite(c), amount);
	}

	public static final Color mult(Color c, double amount) {
		return c == null ? null : new Color(Math.min(255, (int) (c.getRed() * amount)),
				Math.min(255, (int) (c.getGreen() * amount)),
				Math.min(255, (int) (c.getBlue() * amount)),
				c.getAlpha());
	}

	public static Color setAlpha(Color c, int alpha) {
		return c == null ? null : new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);
	}

	public static final Color add(Color c1, Color c2) {
		return c1 == null ? c2
				: c2 == null ? c1
						: new Color(Math.min(255, c1.getRed() + c2.getRed()),
								Math.min(255, c1.getGreen() + c2.getGreen()),
								Math.min(255, c1.getBlue() + c2.getBlue()),
								c1.getAlpha());
	}

	public static Color blend(Color c1, Color c2, double v) {
		double v2 = 1 - v;
		return c1 == null ? (c2 == null ? null : c2)
				: c2 == null ? c1
						: new Color(Math.min(255, (int) (c1.getRed() * v2 + c2.getRed() * v)),
								Math.min(255, (int) (c1.getGreen() * v2 + c2.getGreen() * v)),
								Math.min(255, (int) (c1.getBlue() * v2 + c2.getBlue() * v)),
								Math.min(255, (int) (c1.getAlpha() * v2 + c2.getAlpha() * v)));
	}

	/**
	 * 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 ,g,b Color to check.
	 * @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;
	}

	/**
	 * 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);
	}

	/**
	 * Return the "distance" between two colors. The rgb entries are taken to be coordinates in a 3D space [0.0-1.0],
	 * and this method returnes the distance between the coordinates for the first and second color.
	 *
	 * @param r1 , g1, b1 First color.
	 * @param r2 , g2, b2 Second color.
	 * @return Distance bwetween colors.
	 */
	public static double colorDistance(double r1, double g1, double b1,
			double r2, double g2, double b2) {
		double a = r2 - r1;
		double b = g2 - g1;
		double c = b2 - b1;
		return Math.sqrt(a * a + b * b + c * c);
	}

	public static Color highlight(Color c) {
		return mult(c, isDark(c) ? 1.5F : 0.67F);
	}

	public static Color copy(Color c) {
		return c == null ? null : new Color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
	}

}
