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