minecraft-src/net/minecraft/util/ARGB.java
2025-09-18 12:27:44 +00:00

227 lines
5.6 KiB
Java

package net.minecraft.util;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3f;
public class ARGB {
public static int alpha(int color) {
return color >>> 24;
}
public static int red(int color) {
return color >> 16 & 0xFF;
}
public static int green(int color) {
return color >> 8 & 0xFF;
}
public static int blue(int color) {
return color & 0xFF;
}
public static int color(int alpha, int red, int green, int blue) {
return alpha << 24 | red << 16 | green << 8 | blue;
}
public static int color(int red, int green, int blue) {
return color(255, red, green, blue);
}
public static int color(Vec3 color) {
return color(as8BitChannel((float)color.x()), as8BitChannel((float)color.y()), as8BitChannel((float)color.z()));
}
public static int multiply(int color1, int color2) {
if (color1 == -1) {
return color2;
} else {
return color2 == -1
? color1
: color(alpha(color1) * alpha(color2) / 255, red(color1) * red(color2) / 255, green(color1) * green(color2) / 255, blue(color1) * blue(color2) / 255);
}
}
public static int scaleRGB(int color, float scale) {
return scaleRGB(color, scale, scale, scale);
}
public static int scaleRGB(int color, float redScale, float greenScale, float blueScale) {
return color(
alpha(color),
Math.clamp((int)(red(color) * redScale), 0, 255),
Math.clamp((int)(green(color) * greenScale), 0, 255),
Math.clamp((int)(blue(color) * blueScale), 0, 255)
);
}
public static int scaleRGB(int color, int scale) {
return color(
alpha(color),
Math.clamp((long)red(color) * scale / 255L, 0, 255),
Math.clamp((long)green(color) * scale / 255L, 0, 255),
Math.clamp((long)blue(color) * scale / 255L, 0, 255)
);
}
public static int greyscale(int color) {
int i = (int)(red(color) * 0.3F + green(color) * 0.59F + blue(color) * 0.11F);
return color(i, i, i);
}
public static int lerp(float delta, int color1, int color2) {
int i = Mth.lerpInt(delta, alpha(color1), alpha(color2));
int j = Mth.lerpInt(delta, red(color1), red(color2));
int k = Mth.lerpInt(delta, green(color1), green(color2));
int l = Mth.lerpInt(delta, blue(color1), blue(color2));
return color(i, j, k, l);
}
public static int opaque(int color) {
return color | 0xFF000000;
}
public static int transparent(int color) {
return color & 16777215;
}
public static int color(int alpha, int color) {
return alpha << 24 | color & 16777215;
}
public static int color(float alpha, int color) {
return as8BitChannel(alpha) << 24 | color & 16777215;
}
public static int white(float alpha) {
return as8BitChannel(alpha) << 24 | 16777215;
}
public static int colorFromFloat(float alpha, float red, float green, float blue) {
return color(as8BitChannel(alpha), as8BitChannel(red), as8BitChannel(green), as8BitChannel(blue));
}
public static Vector3f vector3fFromRGB24(int color) {
float f = red(color) / 255.0F;
float g = green(color) / 255.0F;
float h = blue(color) / 255.0F;
return new Vector3f(f, g, h);
}
public static int average(int color1, int color2) {
return color((alpha(color1) + alpha(color2)) / 2, (red(color1) + red(color2)) / 2, (green(color1) + green(color2)) / 2, (blue(color1) + blue(color2)) / 2);
}
public static int as8BitChannel(float value) {
return Mth.floor(value * 255.0F);
}
public static float alphaFloat(int color) {
return from8BitChannel(alpha(color));
}
public static float redFloat(int color) {
return from8BitChannel(red(color));
}
public static float greenFloat(int color) {
return from8BitChannel(green(color));
}
public static float blueFloat(int color) {
return from8BitChannel(blue(color));
}
private static float from8BitChannel(int value) {
return value / 255.0F;
}
public static int toABGR(int color) {
return color & -16711936 | (color & 0xFF0000) >> 16 | (color & 0xFF) << 16;
}
public static int fromABGR(int color) {
return toABGR(color);
}
public static int setBrightness(int color, float brightness) {
int i = red(color);
int j = green(color);
int k = blue(color);
int l = alpha(color);
int m = Math.max(Math.max(i, j), k);
int n = Math.min(Math.min(i, j), k);
float f = m - n;
float g;
if (m != 0) {
g = f / m;
} else {
g = 0.0F;
}
float h;
if (g == 0.0F) {
h = 0.0F;
} else {
float o = (m - i) / f;
float p = (m - j) / f;
float q = (m - k) / f;
if (i == m) {
h = q - p;
} else if (j == m) {
h = 2.0F + o - q;
} else {
h = 4.0F + p - o;
}
h /= 6.0F;
if (h < 0.0F) {
h++;
}
}
if (g == 0.0F) {
i = j = k = Math.round(brightness * 255.0F);
return color(l, i, j, k);
} else {
float ox = (h - (float)Math.floor(h)) * 6.0F;
float px = ox - (float)Math.floor(ox);
float qx = brightness * (1.0F - g);
float r = brightness * (1.0F - g * px);
float s = brightness * (1.0F - g * (1.0F - px));
switch ((int)ox) {
case 0:
i = Math.round(brightness * 255.0F);
j = Math.round(s * 255.0F);
k = Math.round(qx * 255.0F);
break;
case 1:
i = Math.round(r * 255.0F);
j = Math.round(brightness * 255.0F);
k = Math.round(qx * 255.0F);
break;
case 2:
i = Math.round(qx * 255.0F);
j = Math.round(brightness * 255.0F);
k = Math.round(s * 255.0F);
break;
case 3:
i = Math.round(qx * 255.0F);
j = Math.round(r * 255.0F);
k = Math.round(brightness * 255.0F);
break;
case 4:
i = Math.round(s * 255.0F);
j = Math.round(qx * 255.0F);
k = Math.round(brightness * 255.0F);
break;
case 5:
i = Math.round(brightness * 255.0F);
j = Math.round(qx * 255.0F);
k = Math.round(r * 255.0F);
}
return color(l, i, j, k);
}
}
}