ARGB .java

net.minecraft.util.ARGB (class, 454 lines): members: setVector4fFromARGB32, LINEAR_CHANNEL_DEPTH, linearToSrgbChannel, srgbToLinearChannel, LINEAR_TO_SRGB.

net.minecraft.utilclass454 linescommon15.2 KB

API Surface (61)

float srgbToLinearChannel(final int srgb)
int linearToSrgbChannel(final float linear)
int meanLinear(final int srgb1, final int srgb2, final int srgb3, final int srgb4)
int alpha(final int color)
int red(final int color)
int green(final int color)
int blue(final int color)
int color(final int alpha, final int red, final int green, final int blue)
int color(final int red, final int green, final int blue)
int color(final Vec3 vec)
int multiply(final int lhs, final int rhs)
Vector3fc multiply(final Vector3fc lhs, final Vector3fc rhs)
Vector4fc multiply(final Vector4fc lhs, final Vector4fc rhs)
int addRgb(final int lhs, final int rhs)
Vector3fc addRgb(final Vector3fc lhs, final Vector3fc rhs)
Vector4fc addRgb(final Vector4fc lhs, final Vector3fc rhs)
Integer subtractRgb(final Integer lhs, final Integer rhs)
Vector3fc subtractRgb(final Vector3fc lhs, final Vector3fc rhs)
Vector4fc subtractRgb(final Vector4fc lhs, final Vector3fc rhs)
int multiplyAlpha(final int color, final float alphaMultiplier)
int scaleRGB(final int color, final float scale)
int scaleRGB(final int color, final float scaleR, final float scaleG, final float scaleB)
Vector3fc scaleRGB(final Vector3fc color, final float scale)
Vector4fc scaleRGB(final Vector4fc color, final float scale)
Vector3fc scaleRGB(final Vector3fc color, final float scaleR, final float scaleG, final float scaleB)
Vector4fc scaleRGB(final Vector4fc color, final float scaleR, final float scaleG, final float scaleB)
int scaleRGB(final int color, final int scale)
int greyscale(final int color)
Vector3fc greyscale(final Vector3fc color)
Vector4fc greyscale(final Vector4fc color)
int alphaBlend(final int destination, final int source)
Vector3fc alphaBlend(final Vector3fc destination, final Vector4fc source)
Vector4fc alphaBlend(final Vector4fc destination, final Vector4fc source)
int srgbLerp(final float alpha, final int p0, final int p1)
Vector3fc srgbLerp(final float alpha, final Vector3fc p0, final Vector3fc p1)
Vector4fc srgbLerp(final float alpha, final Vector4fc p0, final Vector4fc p1)
int linearLerp(final float alpha, final int p0, final int p1)
int opaque(final int color)
int transparent(final int color)
int color(final int alpha, final int rgb)
int color(final float alpha, final int rgb)
int white(final float alpha)
int white(final int alpha)
int black(final float alpha)
int black(final int alpha)
int gray(final float brightness)
int colorFromFloat(final float alpha, final float red, final float green, final float blue)
int colorFromVector3f(final Vector3fc color)
int colorFromVector4f(final Vector4fc color)
Vector3f vector3fFromRGB24(final int color)
Vector4f vector4fFromARGB32(final int color)
Vector4f setVector4fFromARGB32(final Vector4f dest, final int color)
int average(final int lhs, final int rhs)
int as8BitChannel(final float value)
float alphaFloat(final int color)
float redFloat(final int color)
float greenFloat(final int color)
float blueFloat(final int color)
int toABGR(final int color)
int fromABGR(final int color)
int setBrightness(final int color, final float brightness)

Fields & Constants (3)

LINEAR_CHANNEL_DEPTH
SRGB_TO_LINEAR
LINEAR_TO_SRGB

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

ARGB.java454 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import net.minecraft.world.phys.Vec3;
4
import org.joml.Vector3f;
5
import org.joml.Vector3fc;
6
import org.joml.Vector4f;
7
import org.joml.Vector4fc;
8
9
public class ARGB {
10
	private static final int LINEAR_CHANNEL_DEPTH = 1024;
11
	private static final short[] SRGB_TO_LINEAR = Util.make(new short[256], lookup -> {
12
		for (int i = 0; i < lookup.length; i++) {
13
			float channel = i / 255.0F;
14
			lookup[i] = (short)Math.round(computeSrgbToLinear(channel) * 1023.0F);
15
		}
16
	});
17
	private static final byte[] LINEAR_TO_SRGB = Util.make(new byte[1024], lookup -> {
18
		for (int i = 0; i < lookup.length; i++) {
19
			float channel = i / 1023.0F;
20
			lookup[i] = (byte)Math.round(computeLinearToSrgb(channel) * 255.0F);
21
		}
22
	});
23
24
	private static float computeSrgbToLinear(final float x) {
25
		return x >= 0.04045F ? (float)Math.pow((x + 0.055) / 1.055, 2.4) : x / 12.92F;
26
	}
27
28
	private static float computeLinearToSrgb(final float x) {
29
		return x >= 0.0031308F ? (float)(1.055 * Math.pow(x, 0.4166666666666667) - 0.055) : 12.92F * x;
30
	}
31
32
	public static float srgbToLinearChannel(final int srgb) {
33
		return SRGB_TO_LINEAR[srgb] / 1023.0F;
34
	}
35
36
	public static int linearToSrgbChannel(final float linear) {
37
		return LINEAR_TO_SRGB[Mth.floor(linear * 1023.0F)] & 0xFF;
38
	}
39
40
	public static int meanLinear(final int srgb1, final int srgb2, final int srgb3, final int srgb4) {
41
		return color(
42
			(alpha(srgb1) + alpha(srgb2) + alpha(srgb3) + alpha(srgb4)) / 4,
43
			linearChannelMean(red(srgb1), red(srgb2), red(srgb3), red(srgb4)),
44
			linearChannelMean(green(srgb1), green(srgb2), green(srgb3), green(srgb4)),
45
			linearChannelMean(blue(srgb1), blue(srgb2), blue(srgb3), blue(srgb4))
46
		);
47
	}
48
49
	private static int linearChannelMean(final int c1, final int c2, final int c3, final int c4) {
50
		int linear = (SRGB_TO_LINEAR[c1] + SRGB_TO_LINEAR[c2] + SRGB_TO_LINEAR[c3] + SRGB_TO_LINEAR[c4]) / 4;
51
		return LINEAR_TO_SRGB[linear] & 0xFF;
52
	}
53
54
	public static int alpha(final int color) {
55
		return color >>> 24;
56
	}
57
58
	public static int red(final int color) {
59
		return color >> 16 & 0xFF;
60
	}
61
62
	public static int green(final int color) {
63
		return color >> 8 & 0xFF;
64
	}
65
66
	public static int blue(final int color) {
67
		return color & 0xFF;
68
	}
69
70
	public static int color(final int alpha, final int red, final int green, final int blue) {
71
		return (alpha & 0xFF) << 24 | (red & 0xFF) << 16 | (green & 0xFF) << 8 | blue & 0xFF;
72
	}
73
74
	public static int color(final int red, final int green, final int blue) {
75
		return color(255, red, green, blue);
76
	}
77
78
	public static int color(final Vec3 vec) {
79
		return color(as8BitChannel((float)vec.x()), as8BitChannel((float)vec.y()), as8BitChannel((float)vec.z()));
80
	}
81
82
	public static int multiply(final int lhs, final int rhs) {
83
		if (lhs == -1) {
84
			return rhs;
85
		} else {
86
			return rhs == -1 ? lhs : color(alpha(lhs) * alpha(rhs) / 255, red(lhs) * red(rhs) / 255, green(lhs) * green(rhs) / 255, blue(lhs) * blue(rhs) / 255);
87
		}
88
	}
89
90
	public static Vector3fc multiply(final Vector3fc lhs, final Vector3fc rhs) {
91
		if (lhs.x() == 1.0F && lhs.y() == 1.0F && lhs.z() == 1.0F) {
92
			return rhs;
93
		} else {
94
			return rhs.x() == 1.0F && rhs.y() == 1.0F && rhs.z() == 1.0F ? lhs : lhs.mul(rhs, new Vector3f());
95
		}
96
	}
97
98
	public static Vector4fc multiply(final Vector4fc lhs, final Vector4fc rhs) {
99
		if (lhs.x() == 1.0F && lhs.y() == 1.0F && lhs.z() == 1.0F && lhs.w() == 1.0F) {
100
			return rhs;
101
		} else {
102
			return rhs.x() == 1.0F && rhs.y() == 1.0F && rhs.z() == 1.0F && rhs.w() == 1.0F ? lhs : lhs.mul(rhs, new Vector4f());
103
		}
104
	}
105
106
	public static int addRgb(final int lhs, final int rhs) {
107
		return color(alpha(lhs), Math.min(red(lhs) + red(rhs), 255), Math.min(green(lhs) + green(rhs), 255), Math.min(blue(lhs) + blue(rhs), 255));
108
	}
109
110
	public static Vector3fc addRgb(final Vector3fc lhs, final Vector3fc rhs) {
111
		return new Vector3f(Math.min(lhs.x() + rhs.x(), 1.0F), Math.min(lhs.y() + rhs.y(), 1.0F), Math.min(lhs.z() + rhs.z(), 1.0F));
112
	}
113
114
	public static Vector4fc addRgb(final Vector4fc lhs, final Vector3fc rhs) {
115
		return new Vector4f(Math.min(lhs.x() + rhs.x(), 1.0F), Math.min(lhs.y() + rhs.y(), 1.0F), Math.min(lhs.z() + rhs.z(), 1.0F), lhs.w());
116
	}
117
118
	public static Integer subtractRgb(final Integer lhs, final Integer rhs) {
119
		return color(alpha(lhs), Math.max(red(lhs) - red(rhs), 0), Math.max(green(lhs) - green(rhs), 0), Math.max(blue(lhs) - blue(rhs), 0));
120
	}
121
122
	public static Vector3fc subtractRgb(final Vector3fc lhs, final Vector3fc rhs) {
123
		return new Vector3f(Math.max(lhs.x() - rhs.x(), 0.0F), Math.max(lhs.y() - rhs.y(), 0.0F), Math.max(lhs.z() - rhs.z(), 0.0F));
124
	}
125
126
	public static Vector4fc subtractRgb(final Vector4fc lhs, final Vector3fc rhs) {
127
		return new Vector4f(Math.max(lhs.x() - rhs.x(), 0.0F), Math.max(lhs.y() - rhs.y(), 0.0F), Math.max(lhs.z() - rhs.z(), 0.0F), lhs.w());
128
	}
129
130
	public static int multiplyAlpha(final int color, final float alphaMultiplier) {
131
		if (color == 0 || alphaMultiplier <= 0.0F) {
132
			return 0;
133
		} else {
134
			return alphaMultiplier >= 1.0F ? color : color(alphaFloat(color) * alphaMultiplier, color);
135
		}
136
	}
137
138
	public static int scaleRGB(final int color, final float scale) {
139
		return scaleRGB(color, scale, scale, scale);
140
	}
141
142
	public static int scaleRGB(final int color, final float scaleR, final float scaleG, final float scaleB) {
143
		return color(
144
			alpha(color),
145
			Math.clamp((int)(red(color) * scaleR), 0, 255),
146
			Math.clamp((int)(green(color) * scaleG), 0, 255),
147
			Math.clamp((int)(blue(color) * scaleB), 0, 255)
148
		);
149
	}
150
151
	public static Vector3fc scaleRGB(final Vector3fc color, final float scale) {
152
		return scaleRGB(color, scale, scale, scale);
153
	}
154
155
	public static Vector4fc scaleRGB(final Vector4fc color, final float scale) {
156
		return scaleRGB(color, scale, scale, scale);
157
	}
158
159
	public static Vector3fc scaleRGB(final Vector3fc color, final float scaleR, final float scaleG, final float scaleB) {
160
		return new Vector3f(Math.clamp(color.x() * scaleR, 0.0F, 1.0F), Math.clamp(color.y() * scaleG, 0.0F, 1.0F), Math.clamp(color.z() * scaleB, 0.0F, 1.0F));
161
	}
162
163
	public static Vector4fc scaleRGB(final Vector4fc color, final float scaleR, final float scaleG, final float scaleB) {
164
		return new Vector4f(
165
			Math.clamp(color.x() * scaleR, 0.0F, 1.0F), Math.clamp(color.y() * scaleG, 0.0F, 1.0F), Math.clamp(color.z() * scaleB, 0.0F, 1.0F), color.w()
166
		);
167
	}
168
169
	public static int scaleRGB(final int color, final int scale) {
170
		return color(
171
			alpha(color),
172
			Math.clamp((long)red(color) * scale / 255L, 0, 255),
173
			Math.clamp((long)green(color) * scale / 255L, 0, 255),
174
			Math.clamp((long)blue(color) * scale / 255L, 0, 255)
175
		);
176
	}
177
178
	public static int greyscale(final int color) {
179
		int greyscale = (int)(red(color) * 0.3F + green(color) * 0.59F + blue(color) * 0.11F);
180
		return color(alpha(color), greyscale, greyscale, greyscale);
181
	}
182
183
	public static Vector3fc greyscale(final Vector3fc color) {
184
		float greyscale = color.x() * 0.3F + color.y() * 0.59F + color.z() * 0.11F;
185
		return new Vector3f(greyscale, greyscale, greyscale);
186
	}
187
188
	public static Vector4fc greyscale(final Vector4fc color) {
189
		float greyscale = color.x() * 0.3F + color.y() * 0.59F + color.z() * 0.11F;
190
		return new Vector4f(greyscale, greyscale, greyscale, color.w());
191
	}
192
193
	public static int alphaBlend(final int destination, final int source) {
194
		int destinationAlpha = alpha(destination);
195
		int sourceAlpha = alpha(source);
196
		if (sourceAlpha == 255) {
197
			return source;
198
		}
199
200
		if (sourceAlpha == 0) {
201
			return destination;
202
		}
203
204
		int alpha = sourceAlpha + destinationAlpha * (255 - sourceAlpha) / 255;
205
		return color(
206
			alpha,
207
			alphaBlendChannel(alpha, sourceAlpha, red(destination), red(source)),
208
			alphaBlendChannel(alpha, sourceAlpha, green(destination), green(source)),
209
			alphaBlendChannel(alpha, sourceAlpha, blue(destination), blue(source))
210
		);
211
	}
212
213
	public static Vector3fc alphaBlend(final Vector3fc destination, final Vector4fc source) {
214
		float sourceAlpha = source.w();
215
		if (sourceAlpha == 0.0F) {
216
			return destination;
217
		}
218
219
		Vector3f sourceRgb = new Vector3f().set(source);
220
		return destination.lerp(sourceRgb, sourceAlpha, sourceRgb);
221
	}
222
223
	public static Vector4fc alphaBlend(final Vector4fc destination, final Vector4fc source) {
224
		float destinationAlpha = destination.w();
225
		float sourceAlpha = source.w();
226
		if (sourceAlpha == 1.0F) {
227
			return source;
228
		}
229
230
		if (sourceAlpha == 0.0F) {
231
			return destination;
232
		}
233
234
		float alpha = sourceAlpha + destinationAlpha * (1.0F - sourceAlpha);
235
		return new Vector4f(
236
			alphaBlendChannel(alpha, sourceAlpha, destination.x(), source.x()),
237
			alphaBlendChannel(alpha, sourceAlpha, destination.y(), source.y()),
238
			alphaBlendChannel(alpha, sourceAlpha, destination.z(), source.z()),
239
			alpha
240
		);
241
	}
242
243
	private static int alphaBlendChannel(final int resultAlpha, final int sourceAlpha, final int destination, final int source) {
244
		return (source * sourceAlpha + destination * (resultAlpha - sourceAlpha)) / resultAlpha;
245
	}
246
247
	private static float alphaBlendChannel(final float resultAlpha, final float sourceAlpha, final float destination, final float source) {
248
		return (source * sourceAlpha + destination * (resultAlpha - sourceAlpha)) / resultAlpha;
249
	}
250
251
	public static int srgbLerp(final float alpha, final int p0, final int p1) {
252
		int a = Mth.lerpInt(alpha, alpha(p0), alpha(p1));
253
		int red = Mth.lerpInt(alpha, red(p0), red(p1));
254
		int green = Mth.lerpInt(alpha, green(p0), green(p1));
255
		int blue = Mth.lerpInt(alpha, blue(p0), blue(p1));
256
		return color(a, red, green, blue);
257
	}
258
259
	public static Vector3fc srgbLerp(final float alpha, final Vector3fc p0, final Vector3fc p1) {
260
		return p0.lerp(p1, alpha, new Vector3f());
261
	}
262
263
	public static Vector4fc srgbLerp(final float alpha, final Vector4fc p0, final Vector4fc p1) {
264
		return p0.lerp(p1, alpha, new Vector4f());
265
	}
266
267
	public static int linearLerp(final float alpha, final int p0, final int p1) {
268
		return color(
269
			Mth.lerpInt(alpha, alpha(p0), alpha(p1)),
270
			LINEAR_TO_SRGB[Mth.lerpInt(alpha, SRGB_TO_LINEAR[red(p0)], SRGB_TO_LINEAR[red(p1)])] & 0xFF,
271
			LINEAR_TO_SRGB[Mth.lerpInt(alpha, SRGB_TO_LINEAR[green(p0)], SRGB_TO_LINEAR[green(p1)])] & 0xFF,
272
			LINEAR_TO_SRGB[Mth.lerpInt(alpha, SRGB_TO_LINEAR[blue(p0)], SRGB_TO_LINEAR[blue(p1)])] & 0xFF
273
		);
274
	}
275
276
	public static int opaque(final int color) {
277
		return color | 0xFF000000;
278
	}
279
280
	public static int transparent(final int color) {
281
		return color & 16777215;
282
	}
283
284
	public static int color(final int alpha, final int rgb) {
285
		return alpha << 24 | rgb & 16777215;
286
	}
287
288
	public static int color(final float alpha, final int rgb) {
289
		return as8BitChannel(alpha) << 24 | rgb & 16777215;
290
	}
291
292
	public static int white(final float alpha) {
293
		return as8BitChannel(alpha) << 24 | 16777215;
294
	}
295
296
	public static int white(final int alpha) {
297
		return alpha << 24 | 16777215;
298
	}
299
300
	public static int black(final float alpha) {
301
		return as8BitChannel(alpha) << 24;
302
	}
303
304
	public static int black(final int alpha) {
305
		return alpha << 24;
306
	}
307
308
	public static int gray(final float brightness) {
309
		int channel = as8BitChannel(brightness);
310
		return color(channel, channel, channel);
311
	}
312
313
	public static int colorFromFloat(final float alpha, final float red, final float green, final float blue) {
314
		return color(as8BitChannel(alpha), as8BitChannel(red), as8BitChannel(green), as8BitChannel(blue));
315
	}
316
317
	public static int colorFromVector3f(final Vector3fc color) {
318
		return color(255, as8BitChannel(color.x()), as8BitChannel(color.y()), as8BitChannel(color.z()));
319
	}
320
321
	public static int colorFromVector4f(final Vector4fc color) {
322
		return color(as8BitChannel(color.w()), as8BitChannel(color.x()), as8BitChannel(color.y()), as8BitChannel(color.z()));
323
	}
324
325
	public static Vector3f vector3fFromRGB24(final int color) {
326
		return new Vector3f(redFloat(color), greenFloat(color), blueFloat(color));
327
	}
328
329
	public static Vector4f vector4fFromARGB32(final int color) {
330
		return new Vector4f(redFloat(color), greenFloat(color), blueFloat(color), alphaFloat(color));
331
	}
332
333
	public static Vector4f setVector4fFromARGB32(final Vector4f dest, final int color) {
334
		return dest.set(redFloat(color), greenFloat(color), blueFloat(color), alphaFloat(color));
335
	}
336
337
	public static int average(final int lhs, final int rhs) {
338
		return color((alpha(lhs) + alpha(rhs)) / 2, (red(lhs) + red(rhs)) / 2, (green(lhs) + green(rhs)) / 2, (blue(lhs) + blue(rhs)) / 2);
339
	}
340
341
	public static int as8BitChannel(final float value) {
342
		return Mth.floor(value * 255.0F);
343
	}
344
345
	public static float alphaFloat(final int color) {
346
		return from8BitChannel(alpha(color));
347
	}
348
349
	public static float redFloat(final int color) {
350
		return from8BitChannel(red(color));
351
	}
352
353
	public static float greenFloat(final int color) {
354
		return from8BitChannel(green(color));
355
	}
356
357
	public static float blueFloat(final int color) {
358
		return from8BitChannel(blue(color));
359
	}
360
361
	private static float from8BitChannel(final int value) {
362
		return value / 255.0F;
363
	}
364
365
	public static int toABGR(final int color) {
366
		return color & -16711936 | (color & 0xFF0000) >> 16 | (color & 0xFF) << 16;
367
	}
368
369
	public static int fromABGR(final int color) {
370
		return toABGR(color);
371
	}
372
373
	public static int setBrightness(final int color, final float brightness) {
374
		int red = red(color);
375
		int green = green(color);
376
		int blue = blue(color);
377
		int alpha = alpha(color);
378
		int rgbMax = Math.max(Math.max(red, green), blue);
379
		int rgbMin = Math.min(Math.min(red, green), blue);
380
		float rgbConstantRange = rgbMax - rgbMin;
381
		float saturation;
382
		if (rgbMax != 0) {
383
			saturation = rgbConstantRange / rgbMax;
384
		} else {
385
			saturation = 0.0F;
386
		}
387
388
		float hue;
389
		if (saturation == 0.0F) {
390
			hue = 0.0F;
391
		} else {
392
			float constantRed = (rgbMax - red) / rgbConstantRange;
393
			float constantGreen = (rgbMax - green) / rgbConstantRange;
394
			float constantBlue = (rgbMax - blue) / rgbConstantRange;
395
			if (red == rgbMax) {
396
				hue = constantBlue - constantGreen;
397
			} else if (green == rgbMax) {
398
				hue = 2.0F + constantRed - constantBlue;
399
			} else {
400
				hue = 4.0F + constantGreen - constantRed;
401
			}
402
403
			hue /= 6.0F;
404
			if (hue < 0.0F) {
405
				hue++;
406
			}
407
		}
408
409
		if (saturation == 0.0F) {
410
			red = green = blue = Math.round(brightness * 255.0F);
411
			return color(alpha, red, green, blue);
412
		}
413
414
		float colorWheelSegment = (hue - (float)Math.floor(hue)) * 6.0F;
415
		float colorWheelOffset = colorWheelSegment - (float)Math.floor(colorWheelSegment);
416
		float primaryColor = brightness * (1.0F - saturation);
417
		float secondaryColor = brightness * (1.0F - saturation * colorWheelOffset);
418
		float tertiaryColor = brightness * (1.0F - saturation * (1.0F - colorWheelOffset));
419
		switch ((int)colorWheelSegment) {
420
			case 0:
421
				red = Math.round(brightness * 255.0F);
422
				green = Math.round(tertiaryColor * 255.0F);
423
				blue = Math.round(primaryColor * 255.0F);
424
				break;
425
			case 1:
426
				red = Math.round(secondaryColor * 255.0F);
427
				green = Math.round(brightness * 255.0F);
428
				blue = Math.round(primaryColor * 255.0F);
429
				break;
430
			case 2:
431
				red = Math.round(primaryColor * 255.0F);
432
				green = Math.round(brightness * 255.0F);
433
				blue = Math.round(tertiaryColor * 255.0F);
434
				break;
435
			case 3:
436
				red = Math.round(primaryColor * 255.0F);
437
				green = Math.round(secondaryColor * 255.0F);
438
				blue = Math.round(brightness * 255.0F);
439
				break;
440
			case 4:
441
				red = Math.round(tertiaryColor * 255.0F);
442
				green = Math.round(primaryColor * 255.0F);
443
				blue = Math.round(brightness * 255.0F);
444
				break;
445
			case 5:
446
				red = Math.round(brightness * 255.0F);
447
				green = Math.round(primaryColor * 255.0F);
448
				blue = Math.round(secondaryColor * 255.0F);
449
		}
450
451
		return color(alpha, red, green, blue);
452
	}
453
}
454

Decompiled Minecraft 26.3 source of ARGB (net/minecraft/util/ARGB.java) with Mojang official mappings, stored verbatim on the site.