1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringMaker;
27  
28  import java.awt.Color;
29  
30  /**
31   * <a href="ColorUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Ming-Gih Lam
35   * @author David Truong
36   *
37   */
38  public class ColorUtil {
39  
40      public static Color blend(int[] color1, int[] color2, double ratio) {
41          Color blended = new Color(
42              (int)(((color2[0]-color1[0]) * ratio) + color1[0]),
43              (int)(((color2[1]-color1[1]) * ratio) + color1[1]),
44              (int)(((color2[2]-color1[2]) * ratio) + color1[2]));
45  
46          return blended;
47      }
48  
49      public static Color blend (Color color1, Color color2, double ratio){
50          int[] rgb1 = {color1.getRed(), color1.getGreen(), color1.getBlue()};
51          int[] rgb2 = {color2.getRed(), color2.getGreen(), color2.getBlue()};
52  
53          return blend(rgb1, rgb2, ratio);
54      }
55  
56      public static Color darker(int[] color, double ratio) {
57          Color darkened = new Color(
58              (int)(color[0] - (color[0] * ratio)),
59              (int)(color[1] - (color[1] * ratio)),
60              (int)(color[2] - (color[2] * ratio)));
61  
62          return darkened;
63      }
64  
65      public static String getHex(int[] rgb) {
66          StringMaker sm = new StringMaker();
67  
68          sm.append("#");
69  
70          sm.append(
71              _KEY.substring(
72                  (int)Math.floor(rgb[0] / 16),
73                  (int)Math.floor(rgb[0] / 16) + 1));
74  
75          sm.append(_KEY.substring(rgb[0] % 16, (rgb[0] % 16) + 1));
76  
77          sm.append(
78              _KEY.substring(
79                  (int)Math.floor(rgb[1] / 16),
80                  (int)Math.floor(rgb[1] / 16) + 1));
81  
82          sm.append(_KEY.substring(rgb[1] % 16, (rgb[1] % 16) + 1));
83  
84          sm.append(
85              _KEY.substring(
86                  (int)Math.floor(rgb[2] / 16),
87                  (int)Math.floor(rgb[2] / 16) + 1));
88  
89          sm.append(_KEY.substring(rgb[2] % 16, (rgb[2] % 16) + 1));
90  
91          return sm.toString();
92      }
93  
94      public static int[] getRGB(String hex) {
95          if (hex.startsWith("#")) {
96              hex = hex.substring(1, hex.length()).toUpperCase();
97          }
98          else {
99              hex = hex.toUpperCase();
100         }
101 
102         int[] hexArray = new int[6];
103 
104         if (hex.length() == 6) {
105             char[] c = hex.toCharArray();
106 
107             for (int i = 0; i < hex.length(); i++) {
108                 if (c[i] == 'A') {
109                     hexArray[i] = 10;
110                 }
111                 else if (c[i] == 'B') {
112                     hexArray[i] = 11;
113                 }
114                 else if (c[i] == 'C') {
115                     hexArray[i] = 12;
116                 }
117                 else if (c[i] == 'D') {
118                     hexArray[i] = 13;
119                 }
120                 else if (c[i] == 'E') {
121                     hexArray[i] = 14;
122                 }
123                 else if (c[i] == 'F') {
124                     hexArray[i] = 15;
125                 }
126                 else {
127                     hexArray[i] =
128                         GetterUtil.getInteger(new Character(c[i]).toString());
129                 }
130             }
131         }
132 
133         int[] rgb = new int[3];
134         rgb[0] = (hexArray[0] * 16) + hexArray[1];
135         rgb[1] = (hexArray[2] * 16) + hexArray[3];
136         rgb[2] = (hexArray[4] * 16) + hexArray[5];
137 
138         return rgb;
139     }
140 
141     public static Color lighter(int[] color, double ratio) {
142         Color lightened = new Color(
143             (int)(((0xFF - color[0]) * ratio) + color[0]),
144             (int)(((0xFF - color[1]) * ratio) + color[1]),
145             (int)(((0xFF - color[2]) * ratio) + color[2]));
146 
147         return lightened;
148     }
149 
150     private static final String _KEY = "0123456789ABCDEF";
151 
152 }