001    /**
002     * Copyright (c) 2000-2013 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    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.awt.Color;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     * @author Ming-Gih Lam
026     * @author David Truong
027     */
028    public class ColorUtil {
029    
030            public static Color blend(Color color1, Color color2, double ratio) {
031                    int[] rgb1 = {color1.getRed(), color1.getGreen(), color1.getBlue()};
032                    int[] rgb2 = {color2.getRed(), color2.getGreen(), color2.getBlue()};
033    
034                    return blend(rgb1, rgb2, ratio);
035            }
036    
037            public static Color blend(int[] color1, int[] color2, double ratio) {
038                    Color blended = new Color(
039                            (int)(((color2[0]-color1[0]) * ratio) + color1[0]),
040                            (int)(((color2[1]-color1[1]) * ratio) + color1[1]),
041                            (int)(((color2[2]-color1[2]) * ratio) + color1[2]));
042    
043                    return blended;
044            }
045    
046            public static Color darker(int[] color, double ratio) {
047                    Color darkened = new Color(
048                            (int)(color[0] - (color[0] * ratio)),
049                            (int)(color[1] - (color[1] * ratio)),
050                            (int)(color[2] - (color[2] * ratio)));
051    
052                    return darkened;
053            }
054    
055            public static String getHex(int[] rgb) {
056                    StringBundler sb = new StringBundler(7);
057    
058                    sb.append("#");
059    
060                    sb.append(
061                            _KEY.substring(
062                                    (int)Math.floor(rgb[0] / 16),
063                                    (int)Math.floor(rgb[0] / 16) + 1));
064    
065                    sb.append(_KEY.substring(rgb[0] % 16, (rgb[0] % 16) + 1));
066    
067                    sb.append(
068                            _KEY.substring(
069                                    (int)Math.floor(rgb[1] / 16),
070                                    (int)Math.floor(rgb[1] / 16) + 1));
071    
072                    sb.append(_KEY.substring(rgb[1] % 16, (rgb[1] % 16) + 1));
073    
074                    sb.append(
075                            _KEY.substring(
076                                    (int)Math.floor(rgb[2] / 16),
077                                    (int)Math.floor(rgb[2] / 16) + 1));
078    
079                    sb.append(_KEY.substring(rgb[2] % 16, (rgb[2] % 16) + 1));
080    
081                    return sb.toString();
082            }
083    
084            public static int[] getRGB(String hex) {
085                    if (hex.startsWith("#")) {
086                            hex = StringUtil.toUpperCase(hex.substring(1, hex.length()));
087                    }
088                    else {
089                            hex = StringUtil.toUpperCase(hex);
090                    }
091    
092                    int[] hexArray = new int[6];
093    
094                    if (hex.length() == 6) {
095                            char[] c = hex.toCharArray();
096    
097                            for (int i = 0; i < hex.length(); i++) {
098                                    if (c[i] == 'A') {
099                                            hexArray[i] = 10;
100                                    }
101                                    else if (c[i] == 'B') {
102                                            hexArray[i] = 11;
103                                    }
104                                    else if (c[i] == 'C') {
105                                            hexArray[i] = 12;
106                                    }
107                                    else if (c[i] == 'D') {
108                                            hexArray[i] = 13;
109                                    }
110                                    else if (c[i] == 'E') {
111                                            hexArray[i] = 14;
112                                    }
113                                    else if (c[i] == 'F') {
114                                            hexArray[i] = 15;
115                                    }
116                                    else {
117                                            hexArray[i] = GetterUtil.getInteger(
118                                                    new Character(c[i]).toString());
119                                    }
120                            }
121                    }
122    
123                    int[] rgb = new int[3];
124                    rgb[0] = (hexArray[0] * 16) + hexArray[1];
125                    rgb[1] = (hexArray[2] * 16) + hexArray[3];
126                    rgb[2] = (hexArray[4] * 16) + hexArray[5];
127    
128                    return rgb;
129            }
130    
131            public static Color lighter(int[] color, double ratio) {
132                    Color lightened = new Color(
133                            (int)(((0xFF - color[0]) * ratio) + color[0]),
134                            (int)(((0xFF - color[1]) * ratio) + color[1]),
135                            (int)(((0xFF - color[2]) * ratio) + color[2]));
136    
137                    return lightened;
138            }
139    
140            private static final String _KEY = "0123456789ABCDEF";
141    
142    }