01: /*
02: * Copyright (C) 2004 NNL Technology AB
03: * Visit www.infonode.net for information about InfoNode(R)
04: * products and how to contact NNL Technology AB.
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19: * MA 02111-1307, USA.
20: */
21:
22: // $Id: ColorUtil.java,v 1.10 2005/02/16 11:28:14 jesper Exp $
23: package net.infonode.util;
24:
25: import java.awt.*;
26:
27: public final class ColorUtil {
28: private ColorUtil() {
29: }
30:
31: public static Color getOpposite(Color c) {
32: return isDark(c) ? Color.WHITE : Color.BLACK;
33: }
34:
35: public static Color shade(Color c, double amount) {
36: return blend(c, getOpposite(c), amount);
37: }
38:
39: public static final Color mult(Color c, double amount) {
40: return c == null ? null : new Color(Math.min(255, (int) (c
41: .getRed() * amount)), Math.min(255,
42: (int) (c.getGreen() * amount)), Math.min(255, (int) (c
43: .getBlue() * amount)), c.getAlpha());
44: }
45:
46: public static Color setAlpha(Color c, int alpha) {
47: return c == null ? null : new Color(c.getRed(), c.getGreen(), c
48: .getBlue(), alpha);
49: }
50:
51: public static final Color add(Color c1, Color c2) {
52: return c1 == null ? c2 : c2 == null ? c1 : new Color(Math.min(
53: 255, c1.getRed() + c2.getRed()), Math.min(255, c1
54: .getGreen()
55: + c2.getGreen()), Math.min(255, c1.getBlue()
56: + c2.getBlue()), c1.getAlpha());
57: }
58:
59: public static Color blend(Color c1, Color c2, double v) {
60: double v2 = 1 - v;
61: return c1 == null ? (c2 == null ? null : c2) : c2 == null ? c1
62: : new Color(Math.min(255, (int) (c1.getRed() * v2 + c2
63: .getRed()
64: * v)),
65: Math.min(255, (int) (c1.getGreen() * v2 + c2
66: .getGreen()
67: * v)), Math.min(255, (int) (c1
68: .getBlue()
69: * v2 + c2.getBlue() * v)), Math.min(
70: 255, (int) (c1.getAlpha() * v2 + c2
71: .getAlpha()
72: * v)));
73: }
74:
75: public static boolean isDark(Color c) {
76: return c.getRed() + c.getGreen() + c.getBlue() < 3 * 180;
77: }
78:
79: public static Color highlight(Color c) {
80: return mult(c, isDark(c) ? 1.5F : 0.67F);
81: }
82:
83: public static Color copy(Color c) {
84: return c == null ? null : new Color(c.getRed(), c.getGreen(), c
85: .getBlue(), c.getAlpha());
86: }
87:
88: }
|