package im.expensive.utils.render;
import lombok.experimental.UtilityClass;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import com.mojang.blaze3d.systems.RenderSystem;
import im.expensive.functions.impl.ui.HUD;
import im.expensive.utils.math.MathUtil;
import java.awt.Color;
import im.expensive.utils.client.IMinecraft;
import im.expensive.utils.render.DisplayUtils;
@UtilityClass
public class ColorUtils {
public final int green = new Color(64, 255, 64).getRGB();
public final int yellow = new Color(255, 255, 64).getRGB();
public final int orange = new Color(255, 128, 32).getRGB();
public final int red = new Color(255, 64, 64).getRGB();
public static IntColor intColor;
public static int rgb(int r, int g, int b) {
return 255 << 24 | r << 16 | g << 8 | b;
}
public static Color TwoColorEffect(Color cl1, Color cl2, double speed, int index) {
int angle = (int) (((System.currentTimeMillis()) / speed + index) % 360);
angle = (angle >= 180 ? 360 - angle : angle) * 2;
return interpolateColorC(cl1, cl2, angle / 360f);
}
public static int HUEtoRGB(int value) {
float hue = (float)value / 360.0F;
return Color.HSBtoRGB(hue, 1.0F, 1.0F);
}
public static Color interpolateColorC(Color color1, Color color2, float amount) {
amount = Math.min(1, Math.max(0, amount));
return new Color(interpolateInt(color1.getRed(), color2.getRed(), amount),
interpolateInt(color1.getGreen(), color2.getGreen(), amount),
interpolateInt(color1.getBlue(), color2.getBlue(), amount),
interpolateInt(color1.getAlpha(), color2.getAlpha(), amount));
}
public static int interpolateInt(int oldValue, int newValue, double interpolationValue) {
return interpolate((int) oldValue, (int) newValue, (float) interpolationValue);
}
public static class IntColor {
public static float[] rgb(final int color) {
return new float[]{
(color >> 16 & 0xFF) / 255f,
(color >> 8 & 0xFF) / 255f,
(color & 0xFF) / 255f,
(color >> 24 & 0xFF) / 255f
};
}
public static int rgba(final int r,
final int g,
final int b,
final int a) {
return a << 24 | r << 16 | g << 8 | b;
}
public static int rgb(int r, int g, int b) {
return 255 << 24 | r << 16 | g << 8 | b;
}
public static float[] getRGBAf(int color) {
return new float[]{(float)(color >> 16 & 255) / 255.0F, (float)(color >> 8 & 255) / 255.0F, (float)(color & 255) / 255.0F, (float)(color >> 24 & 255) / 255.0F};
}
public static int getRed(final int hex) {
return hex >> 16 & 255;
}
public static int getGreen(final int hex) {
return hex >> 8 & 255;
}
public static int getBlue(final int hex) {
return hex & 255;
}
public static int getAlpha(final int hex) {
return hex >> 24 & 255;
}
}
public Color random() {
return new Color(Color.HSBtoRGB((float) Math.random(), (float) (0.75F + (Math.random() / 4F)), (float) (0.75F + (Math.random() / 4F))));
}
public static float[] getRGBAf(int color) {
return new float[]{(float)(color >> 16 & 255) / 255.0F, (float)(color >> 8 & 255) / 255.0F, (float)(color & 255) / 255.0F, (float)(color >> 24 & 255) / 255.0F};
}
public int overCol(int c1, int c2, float pc01) {
return getColor((float) red(c1) * (1 - pc01) + (float) red(c2) * pc01, (float) green(c1) * (1 - pc01) + (float) green(c2) * pc01, (float) blue(c1) * (1 - pc01) + (float) blue(c2) * pc01, (float) alpha(c1) * (1 - pc01) + (float) alpha(c2) * pc01);
}
public int overCol(int c1, int c2) {
return overCol(c1, c2, 0.5f);
}
public static int rgba(int r, int g, int b, int a) {
return a << 24 | r << 16 | g << 8 | b;
}
public static void setAlphaColor(final int color, final float alpha) {
final float red = (float) (color >> 16 & 255) / 255.0F;
final float green = (float) (color >> 8 & 255) / 255.0F;
final float blue = (float) (color & 255) / 255.0F;
RenderSystem.color4f(red, green, blue, alpha);
}
public int red(int c) {
return c >> 16 & 0xFF;
}
public int green(int c) {
return c >> 8 & 0xFF;
}
public int blue(int c) {
return c & 0xFF;
}
public int alpha(int c) {
return c >> 24 & 0xFF;
}
public float redf(int c) {
return (float) red(c) / 255.F;
}
public float greenf(int c) {
return (float) green(c) / 255.F;
}
public float bluef(int c) {
return (float) blue(c) / 255.F;
}
public float alphaf(int c) {
return (float) alpha(c) / 255.F;
}
public static void setColor(int color) {
setAlphaColor(color, (float) (color >> 24 & 255) / 255.0F);
}
public static int toColor(String hexColor) {
int argb = Integer.parseInt(hexColor.substring(1), 16);
return setAlpha(argb, 255);
}
public static int setAlpha(int color, int alpha) {
return (color & 0x00ffffff) | (alpha << 24);
}
public static void drawImageAlpha(ResourceLocation resourceLocation, float x, float y, float width, float height,
int color) {
RenderSystem.pushMatrix();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.shadeModel(7425);
IMinecraft.mc.getTextureManager().bindTexture(resourceLocation);
DisplayUtils.quads(x, y, width, height, 7, color);
RenderSystem.shadeModel(7424);
RenderSystem.color4f(1, 1, 1, 1);
RenderSystem.popMatrix();
}
public static float[] rgba(final int color) {
return new float[] {
(color >> 16 & 0xFF) / 255f,
(color >> 8 & 0xFF) / 255f,
(color & 0xFF) / 255f,
(color >> 24 & 0xFF) / 255f
};
}
public static int reAlphaInt(final int color,
final int alpha) {
return (MathHelper.clamp(alpha, 0, 255) << 24) | (color & 16777215);
}
public int getColor(double d, double e, double f, double g) {
return new Color((int) d, (int) e, (int) f, (int) g).getRGB();
}
public int getColor(float r, float g, float b, float a) {
return new Color((int) r, (int) g, (int) b, (int) a).getRGB();
}
public int multAlpha(int c, float apc) {
return getColor(red(c), green(c), blue(c), (float) alpha(c) * apc);
}
public int astolfo(int speed, int index) {
double angle = (int) ((System.currentTimeMillis() / speed + index) % 360);
return Color.getHSBColor(
((angle %= 360) / 360.0) < 0.5 ? -((float) (angle / 360.0)) : (float) (angle / 360.0),
0.5F,
1.0F
).hashCode();
}
public int rainbow(int speed, int index, float saturation, float brightness, float opacity) {
int angle = (int) ((System.currentTimeMillis() / speed + index) % 360);
float hue = angle / 360f;
int color = Color.HSBtoRGB(hue, saturation, brightness);
return getColor(
red(color),
green(color),
blue(color),
Math.max(0, Math.min(255, (int) (opacity * 255)))
);
}
private static int calculateHueDegrees(int divisor, int offset) {
long currentTime = System.currentTimeMillis();
long calculatedValue = (currentTime / divisor + offset) % 360L;
return (int) calculatedValue;
}
public static int gradient(int start, int end, int index, int speed) {
int angle = (int) ((System.currentTimeMillis() / speed + index) % 360);
angle = (angle > 180 ? 360 - angle : angle) + 180;
int color = interpolate(start, end, MathHelper.clamp(angle / 180f - 1, 0, 1));
float[] hs = rgba(color);
float[] hsb = Color.RGBtoHSB((int) (hs[0] * 255), (int) (hs[1] * 255), (int) (hs[2] * 255), null);
hsb[1] *= 1.5F;
hsb[1] = Math.min(hsb[1], 1.0f);
return Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
}
public Color interpolate(Color color1, Color color2, double amount) {
amount = 1F - amount;
amount = (float) MathHelper.clamp(0, 1, amount);
return new Color(
(int) MathUtil.lerp(color1.getRed(), color2.getRed(), amount),
(int) MathUtil.lerp(color1.getGreen(), color2.getGreen(), amount),
(int) MathUtil.lerp(color1.getBlue(), color2.getBlue(), amount),
(int) MathUtil.lerp(color1.getAlpha(), color2.getAlpha(), amount)
);
}
public static int getColor(int index) {
return HUD.getColor(index);
}
public int getColor(int r, int g, int b, int a) {
return new Color(r, g, b, a).getRGB();
}
public int getColor(int r, int g, int b) {
return new Color(r, g, b, 255).getRGB();
}
public int getColor(int br, int a) {
return new Color(br, br, br, a).getRGB();
}
public int interpolate(int color1, int color2, double amount) {
amount = (float) MathHelper.clamp(0, 1, amount);
return getColor(
MathUtil.lerp(red(color1), red(color2), amount),
MathUtil.lerp(green(color1), green(color2), amount),
MathUtil.lerp(blue(color1), blue(color2), amount),
MathUtil.lerp(alpha(color1), alpha(color2), amount)
);
}
public static int interpolate(int start, int end, float value) {
float[] startColor = rgba(start);
float[] endColor = rgba(end);
return rgba((int) MathUtil.interpolate(startColor[0] * 255, endColor[0] * 255, value),
(int) MathUtil.interpolate(startColor[1] * 255, endColor[1] * 255, value),
(int) MathUtil.interpolate(startColor[2] * 255, endColor[2] * 255, value),
(int) MathUtil.interpolate(startColor[3] * 255, endColor[3] * 255, value));
}
public static Color lerp(int speed, int index, Color start, Color end) {
int angle = (int) (((System.currentTimeMillis()) / speed + index) % 360);
angle = (angle >= 180 ? 360 - angle : angle) * 2;
return interpolate(start, end, angle / 360f);
}
public static Color withAlpha(Color color, int alpha) {
return new Color(color.getRed(), color.getGreen(), color.getBlue(), (int) MathHelper.clamp(0, 255, alpha));
}
public static int HSBtoRGB(float hue, float saturation, float brightness) {
return Color.HSBtoRGB(hue, saturation, brightness);
}
public static float[] RGBtoHSB(int rgb) {
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = rgb & 0xFF;
return Color.RGBtoHSB(r, g, b, null);
}
public static int rainbow(float speed, float saturation, float brightness) {
float hue = (System.currentTimeMillis() % (int)(speed * 1000)) / (speed * 1000f);
return HSBtoRGB(hue, saturation, brightness);
}
public static int fade(int color, int speed, float offset) {
float[] hsb = RGBtoHSB(color);
float hue = hsb[0];
float brightness = ((float) Math.sin((System.currentTimeMillis() + offset) / speed) + 1f) / 2f;
return HSBtoRGB(hue, hsb[1], brightness * hsb[2]);
}
public static int addColoring(int color, int value) {
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
int a = (color >> 24) & 0xFF;
r = Math.min(255, Math.max(0, r + value));
g = Math.min(255, Math.max(0, g + value));
b = Math.min(255, Math.max(0, b + value));
return (a << 24) | (r << 16) | (g << 8) | b;
}
}