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