Debian Codegraph @

src/com/github/weisj/jsvg/util/ColorUtil.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2026 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.util;
23
24 import java.awt.*;
25
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 public final class ColorUtil {
30 private ColorUtil() {}
31
32 public static int div255(int x) {
33 // https://docs.google.com/document/d/1tNrMWShq55rfltcZxAx1N-6f82Dt7MWLDHm-5GQVEnE/edit
34 x += 128;
35 return (x + (x >> 8)) >> 8;
36 }
37
38 public static int computeLuminance(int r, int g, int b) {
39 return toRgbRange(0.2125 * r + 0.7164 * g + 0.0712 * b);
40 }
41
42 public static int toRgbRange(double value) {
43 return (int) Math.max(Math.min(Math.round(value), 255), 0);
44 }
45
46 public static Color withAlpha(@NotNull Color c, float alpha) {
47 int a = Math.max(Math.min(255, (int) (alpha * 255)), 0);
48 return new Color(c.getRed(), c.getGreen(), c.getBlue(), a);
49 }
50
51 public static String toString(@Nullable Color c) {
52 if (c == null) return "null";
53 return String.format("Color[%d,%d,%d,%d]", c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
54 }
55
56 public static void convertRGBPretoHSL(int r, int g, int b, int a, float @NotNull [] hsl) {
57 if (r < 0)
58 r = 0;
59 else if (r > 255) r = 255;
60 if (g < 0)
61 g = 0;
62 else if (g > 255) g = 255;
63 if (b < 0)
64 b = 0;
65 else if (b > 255) b = 255;
66
67 float componentR = r / (float) a;
68 float componentG = g / (float) a;
69 float componentB = b / (float) a;
70
71 float minComponent;
72 float maxComponent;
73
74 if (componentR > componentG) {
75 minComponent = componentG;
76 maxComponent = componentR;
77 } else {
78 minComponent = componentR;
79 maxComponent = componentG;
80 }
81 if (componentB > maxComponent) {
82 maxComponent = componentB;
83 }
84 if (componentB < minComponent) {
85 minComponent = componentB;
86 }
87
88 float deltaMax = maxComponent - minComponent;
89
90 float h;
91 float s;
92 float l;
93 l = (maxComponent + minComponent) / 2f;
94
95 if (deltaMax - 0.01f <= 0.0f) {
96 h = 0;
97 s = 0;
98 } else {
99 if (l < 0.5f) {
100 assert maxComponent + minComponent != 0;
101 s = deltaMax / (maxComponent + minComponent);
102 } else {
103 s = deltaMax / (2 - maxComponent - minComponent);
104 }
105
106 assert deltaMax > 0;
107 float deltaR = (((maxComponent - componentR) / 6f) + (deltaMax / 2f)) / deltaMax;
108 float deltaG = (((maxComponent - componentG) / 6f) + (deltaMax / 2f)) / deltaMax;
109 float deltaB = (((maxComponent - componentB) / 6f) + (deltaMax / 2f)) / deltaMax;
110
111 if (componentR == maxComponent) {
112 h = deltaB - deltaG;
113 } else if (componentG == maxComponent) {
114 h = (1 / 3f) + deltaR - deltaB;
115 } else {
116 h = (2 / 3f) + deltaG - deltaR;
117 }
118 if (h < 0) {
119 h += 1;
120 }
121 if (h > 1) {
122 h -= 1;
123 }
124 }
125
126 hsl[0] = h;
127 hsl[1] = s;
128 hsl[2] = l;
129 }
130
131 public static void convertHSLtoRGB(float h, float s, float l, int @NotNull [] rgb) {
132 if (h < 0)
133 h = 0.0f;
134 else if (h > 1.0f) h = 1.0f;
135 if (s < 0)
136 s = 0.0f;
137 else if (s > 1.0f) s = 1.0f;
138 if (l < 0)
139 l = 0.0f;
140 else if (l > 1.0f) l = 1.0f;
141
142 int r;
143 int g;
144 int b;
145
146 if (s - 0.01f <= 0.0f) {
147 r = (int) (l * 255.0f);
148 g = (int) (l * 255.0f);
149 b = (int) (l * 255.0f);
150 } else {
151 float x;
152 float y;
153 if (l < 0.5f) {
154 y = l * (1 + s);
155 } else {
156 y = (l + s) - (s * l);
157 }
158 x = 2 * l - y;
159
160 r = (int) (255.0f * hue2RGB(x, y, h + (1.0f / 3.0f)));
161 g = (int) (255.0f * hue2RGB(x, y, h));
162 b = (int) (255.0f * hue2RGB(x, y, h - (1.0f / 3.0f)));
163 }
164
165 rgb[0] = r;
166 rgb[1] = g;
167 rgb[2] = b;
168 }
169
170 private static float hue2RGB(float v1, float v2, float vH) {
171 if (vH < 0.0f) {
172 vH += 1.0f;
173 }
174 if (vH > 1.0f) {
175 vH -= 1.0f;
176 }
177 if ((6.0f * vH) < 1.0f) {
178 return (v1 + (v2 - v1) * 6.0f * vH);
179 }
180 if ((2.0f * vH) < 1.0f) {
181 return v2;
182 }
183 if ((3.0f * vH) < 2.0f) {
184 return (v1 + (v2 - v1) * ((2.0f / 3.0f) - vH) * 6.0f);
185 }
186 return v1;
187 }
188
189 /**
190 * Color space conversion lookup tables.
191 */
192 private static final int[][] SRGBtoLinearRGBPre = new int[256][256];
193 private static final int[] SRGBtoLinearRGB = new int[256];
194 private static final int[][] LinearRGBtoSRGBPre = new int[256][256];
195 private static final int[] LinearRGBtoSRGB = new int[256];
196
197 static {
198 // build the tables
199 for (int k = 0; k < 256; k++) {
200 SRGBtoLinearRGB[k] = convertSRGBtoLinearRGB(k, 1);
201 LinearRGBtoSRGB[k] = convertLinearRGBtoSRGB(k, 1);
202 }
203 SRGBtoLinearRGBPre[255] = SRGBtoLinearRGB;
204 LinearRGBtoSRGBPre[255] = LinearRGBtoSRGB;
205 for (int i = 0; i < 255; i++) {
206 int[] sRGBtoLinear = new int[256];
207 int[] linearTosRGB = new int[256];
208 float alpha = i / 255f;
209 for (int k = 0; k < 256; k++) {
210 sRGBtoLinear[k] = convertSRGBtoLinearRGB(k, alpha);
211 linearTosRGB[k] = convertLinearRGBtoSRGB(k, alpha);
212 }
213 SRGBtoLinearRGBPre[i] = sRGBtoLinear;
214 LinearRGBtoSRGBPre[i] = linearTosRGB;
215 }
216 }
217
218 public static void sRGBtoLinearRGBinPlace(int @NotNull [] argb) {
219 argb[0] = SRGBtoLinearRGB[argb[0]];
220 argb[1] = SRGBtoLinearRGB[argb[1]];
221 argb[2] = SRGBtoLinearRGB[argb[2]];
222 }
223
224 public static void linearRGBtoSRGBinPlace(int @NotNull [] argb) {
225 argb[0] = LinearRGBtoSRGB[argb[0]];
226 argb[1] = LinearRGBtoSRGB[argb[1]];
227 argb[2] = LinearRGBtoSRGB[argb[2]];
228 }
229
230 public static void sRGBtoLinearRGBPreInPlace(int @NotNull [] argb) {
231 int alpha = argb[3];
232 int[] table = SRGBtoLinearRGBPre[alpha];
233 argb[0] = table[argb[0]];
234 argb[1] = table[argb[1]];
235 argb[2] = table[argb[2]];
236 }
237
238 public static void linearRGBtoSRGBPreInPlace(int @NotNull [] argb) {
239 int alpha = argb[3];
240 int[] table = LinearRGBtoSRGBPre[alpha];
241 argb[0] = table[argb[0]];
242 argb[1] = table[argb[1]];
243 argb[2] = table[argb[2]];
244 }
245
246 public static int sRGBtoLinearRGBBand(int value) {
247 return SRGBtoLinearRGB[value];
248 }
249
250 public static int sRGBtoLinearRGB(int argb) {
251 int a = argb >>> 24;
252 int r = SRGBtoLinearRGB[(argb >> 16) & 0xff];
253 int g = SRGBtoLinearRGB[(argb >> 8) & 0xff];
254 int b = SRGBtoLinearRGB[argb & 0xff];
255 return ((a & 0xFF) << 24) |
256 ((r & 0xFF) << 16) |
257 ((g & 0xFF) << 8) |
258 (b & 0xFF);
259 }
260
261 public static int linearRGBtoSRGBBand(int value) {
262 return LinearRGBtoSRGB[value];
263 }
264
265 public static int linearRGBtoSRGB(int argb) {
266 int a = argb >>> 24;
267 int r = LinearRGBtoSRGB[(argb >> 16) & 0xff];
268 int g = LinearRGBtoSRGB[(argb >> 8) & 0xff];
269 int b = LinearRGBtoSRGB[argb & 0xff];
270 return ((a & 0xFF) << 24) |
271 ((r & 0xFF) << 16) |
272 ((g & 0xFF) << 8) |
273 (b & 0xFF);
274 }
275
276 /**
277 * Helper function to convert a color component in sRGB space to linear
278 * RGB space. Used to build a static lookup table.
279 */
280 private static int convertSRGBtoLinearRGB(int color, float alpha) {
281 float factor = (255f * alpha);
282 float input = color / factor;
283 float output;
284 if (input <= 0.04045f) {
285 output = input / 12.92f;
286 } else {
287 output = (float) Math.pow((input + 0.055) / 1.055, 2.4);
288 }
289
290 return Math.round(output * factor);
291 }
292
293 /**
294 * Helper function to convert a color component in linear RGB space to
295 * SRGB space. Used to build a static lookup table.
296 */
297 private static int convertLinearRGBtoSRGB(int color, float alpha) {
298 float factor = (255f * alpha);
299 float input = color / factor;
300 float output;
301 if (input <= 0.0031308) {
302 output = input * 12.92f;
303 } else {
304 output = (1.055f * ((float) Math.pow(input, (1.0 / 2.4)))) - 0.055f;
305 }
306
307 return Math.round(output * factor);
308 }
309
310 public static int clampColor(int v) {
311 return Math.max(Math.min(255, v), 0);
312 }
313 }