1
22
23 package com.liferay.util;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26
27 import java.awt.Color;
28
29
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 }