001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    
020    import java.awt.Color;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @author Ming-Gih Lam
025     * @author David Truong
026     */
027    public class ColorUtil {
028    
029            public static Color blend(int[] color1, int[] color2, double ratio) {
030                    Color blended = new Color(
031                            (int)(((color2[0]-color1[0]) * ratio) + color1[0]),
032                            (int)(((color2[1]-color1[1]) * ratio) + color1[1]),
033                            (int)(((color2[2]-color1[2]) * ratio) + color1[2]));
034    
035                    return blended;
036            }
037    
038            public static Color blend (Color color1, Color color2, double ratio) {
039                    int[] rgb1 = {color1.getRed(), color1.getGreen(), color1.getBlue()};
040                    int[] rgb2 = {color2.getRed(), color2.getGreen(), color2.getBlue()};
041    
042                    return blend(rgb1, rgb2, ratio);
043            }
044    
045            public static Color darker(int[] color, double ratio) {
046                    Color darkened = new Color(
047                            (int)(color[0] - (color[0] * ratio)),
048                            (int)(color[1] - (color[1] * ratio)),
049                            (int)(color[2] - (color[2] * ratio)));
050    
051                    return darkened;
052            }
053    
054            public static String getHex(int[] rgb) {
055                    StringBundler sb = new StringBundler(7);
056    
057                    sb.append("#");
058    
059                    sb.append(
060                            _KEY.substring(
061                                    (int)Math.floor(rgb[0] / 16),
062                                    (int)Math.floor(rgb[0] / 16) + 1));
063    
064                    sb.append(_KEY.substring(rgb[0] % 16, (rgb[0] % 16) + 1));
065    
066                    sb.append(
067                            _KEY.substring(
068                                    (int)Math.floor(rgb[1] / 16),
069                                    (int)Math.floor(rgb[1] / 16) + 1));
070    
071                    sb.append(_KEY.substring(rgb[1] % 16, (rgb[1] % 16) + 1));
072    
073                    sb.append(
074                            _KEY.substring(
075                                    (int)Math.floor(rgb[2] / 16),
076                                    (int)Math.floor(rgb[2] / 16) + 1));
077    
078                    sb.append(_KEY.substring(rgb[2] % 16, (rgb[2] % 16) + 1));
079    
080                    return sb.toString();
081            }
082    
083            public static int[] getRGB(String hex) {
084                    if (hex.startsWith("#")) {
085                            hex = hex.substring(1, hex.length()).toUpperCase();
086                    }
087                    else {
088                            hex = hex.toUpperCase();
089                    }
090    
091                    int[] hexArray = new int[6];
092    
093                    if (hex.length() == 6) {
094                            char[] c = hex.toCharArray();
095    
096                            for (int i = 0; i < hex.length(); i++) {
097                                    if (c[i] == 'A') {
098                                            hexArray[i] = 10;
099                                    }
100                                    else if (c[i] == 'B') {
101                                            hexArray[i] = 11;
102                                    }
103                                    else if (c[i] == 'C') {
104                                            hexArray[i] = 12;
105                                    }
106                                    else if (c[i] == 'D') {
107                                            hexArray[i] = 13;
108                                    }
109                                    else if (c[i] == 'E') {
110                                            hexArray[i] = 14;
111                                    }
112                                    else if (c[i] == 'F') {
113                                            hexArray[i] = 15;
114                                    }
115                                    else {
116                                            hexArray[i] =
117                                                    GetterUtil.getInteger(new Character(c[i]).toString());
118                                    }
119                            }
120                    }
121    
122                    int[] rgb = new int[3];
123                    rgb[0] = (hexArray[0] * 16) + hexArray[1];
124                    rgb[1] = (hexArray[2] * 16) + hexArray[3];
125                    rgb[2] = (hexArray[4] * 16) + hexArray[5];
126    
127                    return rgb;
128            }
129    
130            public static Color lighter(int[] color, double ratio) {
131                    Color lightened = new Color(
132                            (int)(((0xFF - color[0]) * ratio) + color[0]),
133                            (int)(((0xFF - color[1]) * ratio) + color[1]),
134                            (int)(((0xFF - color[2]) * ratio) + color[2]));
135    
136                    return lightened;
137            }
138    
139            private static final String _KEY = "0123456789ABCDEF";
140    
141    }