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