1   /**
2    * Copyright (c) 2000-2009 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  
27  import java.awt.Color;
28  
29  /**
30   * <a href="ColorUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   * @author Ming-Gih Lam
34   * @author David Truong
35   *
36   */
37  public class ColorUtil {
38  
39      public static Color blend(int[] color1, int[] color2, double ratio) {
40          Color blended = new Color(
41              (int)(((color2[0]-color1[0]) * ratio) + color1[0]),
42              (int)(((color2[1]-color1[1]) * ratio) + color1[1]),
43              (int)(((color2[2]-color1[2]) * ratio) + color1[2]));
44  
45          return blended;
46      }
47  
48      public static Color blend (Color color1, Color color2, double ratio) {
49          int[] rgb1 = {color1.getRed(), color1.getGreen(), color1.getBlue()};
50          int[] rgb2 = {color2.getRed(), color2.getGreen(), color2.getBlue()};
51  
52          return blend(rgb1, rgb2, ratio);
53      }
54  
55      public static Color darker(int[] color, double ratio) {
56          Color darkened = new Color(
57              (int)(color[0] - (color[0] * ratio)),
58              (int)(color[1] - (color[1] * ratio)),
59              (int)(color[2] - (color[2] * ratio)));
60  
61          return darkened;
62      }
63  
64      public static String getHex(int[] rgb) {
65          StringBuilder sb = new StringBuilder();
66  
67          sb.append("#");
68  
69          sb.append(
70              _KEY.substring(
71                  (int)Math.floor(rgb[0] / 16),
72                  (int)Math.floor(rgb[0] / 16) + 1));
73  
74          sb.append(_KEY.substring(rgb[0] % 16, (rgb[0] % 16) + 1));
75  
76          sb.append(
77              _KEY.substring(
78                  (int)Math.floor(rgb[1] / 16),
79                  (int)Math.floor(rgb[1] / 16) + 1));
80  
81          sb.append(_KEY.substring(rgb[1] % 16, (rgb[1] % 16) + 1));
82  
83          sb.append(
84              _KEY.substring(
85                  (int)Math.floor(rgb[2] / 16),
86                  (int)Math.floor(rgb[2] / 16) + 1));
87  
88          sb.append(_KEY.substring(rgb[2] % 16, (rgb[2] % 16) + 1));
89  
90          return sb.toString();
91      }
92  
93      public static int[] getRGB(String hex) {
94          if (hex.startsWith("#")) {
95              hex = hex.substring(1, hex.length()).toUpperCase();
96          }
97          else {
98              hex = hex.toUpperCase();
99          }
100 
101         int[] hexArray = new int[6];
102 
103         if (hex.length() == 6) {
104             char[] c = hex.toCharArray();
105 
106             for (int i = 0; i < hex.length(); i++) {
107                 if (c[i] == 'A') {
108                     hexArray[i] = 10;
109                 }
110                 else if (c[i] == 'B') {
111                     hexArray[i] = 11;
112                 }
113                 else if (c[i] == 'C') {
114                     hexArray[i] = 12;
115                 }
116                 else if (c[i] == 'D') {
117                     hexArray[i] = 13;
118                 }
119                 else if (c[i] == 'E') {
120                     hexArray[i] = 14;
121                 }
122                 else if (c[i] == 'F') {
123                     hexArray[i] = 15;
124                 }
125                 else {
126                     hexArray[i] =
127                         GetterUtil.getInteger(new Character(c[i]).toString());
128                 }
129             }
130         }
131 
132         int[] rgb = new int[3];
133         rgb[0] = (hexArray[0] * 16) + hexArray[1];
134         rgb[1] = (hexArray[2] * 16) + hexArray[3];
135         rgb[2] = (hexArray[4] * 16) + hexArray[5];
136 
137         return rgb;
138     }
139 
140     public static Color lighter(int[] color, double ratio) {
141         Color lightened = new Color(
142             (int)(((0xFF - color[0]) * ratio) + color[0]),
143             (int)(((0xFF - color[1]) * ratio) + color[1]),
144             (int)(((0xFF - color[2]) * ratio) + color[2]));
145 
146         return lightened;
147     }
148 
149     private static final String _KEY = "0123456789ABCDEF";
150 
151 }