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