1 package edu.jiangxin.apktoolbox.convert.color;
2
3 import edu.jiangxin.apktoolbox.convert.color.colorspace.CielabColorSpace;
4 import edu.jiangxin.apktoolbox.convert.color.colorspace.CmykColorSpace;
5 import edu.jiangxin.apktoolbox.convert.color.colorspace.HsbColorSpace;
6 import edu.jiangxin.apktoolbox.convert.color.colorspace.HslColorSpace;
7
8 import java.awt.*;
9 import java.awt.color.ColorSpace;
10
11 public class ColorUtils {
12 public static Color hex2Color(String hexColor) {
13 return Color.decode(hexColor);
14 }
15
16 public static String color2Hex(Color color) {
17 return String.format("0x%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue());
18 }
19
20 public static Color hsb2Color(int hueI, int saturationI, int brightnessI) {
21 float[] hsbVal = hsbInt2Float(new int[]{hueI, saturationI, brightnessI});
22 ColorSpace colorSpace = HsbColorSpace.getInstance();
23 float[] rgbVal = colorSpace.toRGB(hsbVal);
24 return new Color(rgbVal[0], rgbVal[1], rgbVal[2]);
25 }
26
27 public static int[] color2Hsb(Color color) {
28 float[] rgbVal = color.getRGBColorComponents(null);
29 ColorSpace colorSpace = HsbColorSpace.getInstance();
30 float[] hsbVal = colorSpace.fromRGB(rgbVal);
31 return hsbFloat2Int(hsbVal);
32 }
33
34 public static Color hsl2Color(int hueI, int saturationI, int lightnessI) {
35 float[] hslVal = hslInt2Float(new int[]{hueI, saturationI, lightnessI});
36 ColorSpace colorSpace = HslColorSpace.getInstance();
37 float[] rgbVal = colorSpace.toRGB(hslVal);
38 return new Color(rgbVal[0], rgbVal[1], rgbVal[2]);
39 }
40
41 public static int[] color2Hsl(Color color) {
42 float[] rgbVal = color.getRGBColorComponents(null);
43 ColorSpace colorSpace = HslColorSpace.getInstance();
44 float[] hslVal = colorSpace.fromRGB(rgbVal);
45 return hslFloat2Int(hslVal);
46 }
47
48 public static Color cmyk2Color(int cyanI, int magentaI, int yellowI, int keyI) {
49 float[] cmykVal = cmykInt2Float(new int[]{cyanI, magentaI, yellowI, keyI});
50 ColorSpace colorSpace = CmykColorSpace.getInstance();
51 float[] rgbVal = colorSpace.toRGB(cmykVal);
52 return new Color(rgbVal[0], rgbVal[1], rgbVal[2]);
53 }
54
55 public static int[] color2Cmyk(Color color) {
56 float[] rgbVal = color.getRGBColorComponents(null);
57 ColorSpace colorSpace = CmykColorSpace.getInstance();
58 float[] cmykVal = colorSpace.fromRGB(rgbVal);
59 return cmykFloat2Int(cmykVal);
60 }
61
62 public static Color cielab2Color(int L, int a, int b) {
63 float[] cieLabVal = cielabInt2Float(new int[]{L, a, b});
64 ColorSpace colorSpace = CielabColorSpace.getInstance();
65 float[] rgbVal = colorSpace.toRGB(cieLabVal);
66 return new Color(rgbVal[0], rgbVal[1], rgbVal[2]);
67 }
68
69 public static int[] color2Cielab(Color color) {
70 float[] rgbVal = color.getRGBColorComponents(null);
71 ColorSpace colorSpace = CielabColorSpace.getInstance();
72 float[] cieLabVal = colorSpace.fromRGB(rgbVal);
73 return cielabFloat2Int(cieLabVal);
74 }
75
76 public static float[] rgbInt2Float(int[] value) {
77 float red = value[0] / 255f;
78 float green = value[1] / 255f;
79 float blue = value[2] / 255f;
80 return new float[]{red, green, blue};
81 }
82
83 public static int[] rgbFloat2Int(float[] value) {
84 int red = (int) (value[0] * 255);
85 int green = (int) (value[1] * 255);
86 int blue = (int) (value[2] * 255);
87 return new int[]{red, green, blue};
88 }
89
90 public static int[] hsbFloat2Int(float[] value) {
91 int hue = (int) (value[0] * 360);
92 int saturation = (int) (value[1] * 100);
93 int brightness = (int) (value[2] * 100);
94 return new int[]{hue, saturation, brightness};
95 }
96
97 public static float[] hsbInt2Float(int[] value) {
98 float hue = value[0] / 360f;
99 float saturation = value[1] / 100f;
100 float brightness = value[2] / 100f;
101 return new float[]{hue, saturation, brightness};
102 }
103
104 public static int[] hslFloat2Int(float[] value) {
105 int hue = (int) (value[0] * 360);
106 int saturation = (int) (value[1] * 100);
107 int lightness = (int) (value[2] * 100);
108 return new int[]{hue, saturation, lightness};
109 }
110
111 public static float[] hslInt2Float(int[] value) {
112 float hue = value[0] / 360f;
113 float saturation = value[1] / 100f;
114 float lightness = value[2] / 100f;
115 return new float[]{hue, saturation, lightness};
116 }
117
118
119 public static int[] cmykFloat2Int(float[] value) {
120 int cyan = (int) (value[0] * 100);
121 int magenta = (int) (value[1] * 100);
122 int yellow = (int) (value[2] * 100);
123 int key = (int) (value[3] * 100);
124 return new int[]{cyan, magenta, yellow, key};
125 }
126
127 public static float[] cmykInt2Float(int[] value) {
128 float cyan = value[0] / 100f;
129 float magenta = value[1] / 100f;
130 float yellow = value[2] / 100f;
131 float key = value[3] / 100f;
132 return new float[]{cyan, magenta, yellow, key};
133 }
134
135
136 public static int[] cielabFloat2Int(float[] value) {
137 int L = (int) value[0];
138 int a = (int) value[1];
139 int b = (int) value[2];
140 return new int[]{L, a, b};
141 }
142
143 public static float[] cielabInt2Float(int[] value) {
144 float L = (float) value[0];
145 float a = (float) value[1];
146 float b = (float) value[2];
147 return new float[]{L, a, b};
148 }
149 }