From 475a595c8641e43a4c43b633f7aae467f5e2e84f Mon Sep 17 00:00:00 2001 From: Colton Kennedy Date: Tue, 5 Dec 2023 18:53:03 -0500 Subject: [PATCH] Fixed colors not correctly displaying when first opening ClickGUI. --- src/main/java/net/aoba/gui/Color.java | 60 +++++++++++++++---- .../tabs/components/ColorPickerComponent.java | 13 +++- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/aoba/gui/Color.java b/src/main/java/net/aoba/gui/Color.java index fa46cd04..a3bab577 100644 --- a/src/main/java/net/aoba/gui/Color.java +++ b/src/main/java/net/aoba/gui/Color.java @@ -31,7 +31,7 @@ public class Color { public int g; public int b; public int alpha = 255; - + public float hue; public float saturation; public float luminance; @@ -47,15 +47,54 @@ public Color(int r, int g, int b) { this.r = r; this.g = g; this.b = b; + + HSVFromRGB(r, g, b); } public Color(int r, int g, int b, int alpha) { this.r = r; this.g = g; this.b = b; + + HSVFromRGB(r, g, b); + this.alpha = alpha; } - + + private void HSVFromRGB(int r, int g, int b) { + // Calculate HSV value + float rPrime = r / 255.0f; + float gPrime = g / 255.0f; + float bPrime = b / 255.0f; + + float cMax = Math.max(rPrime, Math.max(gPrime, bPrime)); + float cMin = Math.min(rPrime, Math.min(gPrime, bPrime)); + + float delta = cMax - cMin; + + // Calculate Hue + if(delta == 0.0f) { + hue = 0.0f; + }else { + if (cMax == rPrime) { + hue = (60.0f * (((gPrime - bPrime) / delta) % 6)); + } else if (cMax == gPrime) { + hue = (60.0f * (((bPrime - rPrime) / delta) + 2)); + } else if (cMax == bPrime) { + hue = (60.0f * (((rPrime - gPrime) / delta) + 2)); + } + } + + // Calculate Saturation + if (cMax == 0.0f) + saturation = 0.0f; + else + saturation = delta / cMax; + + // Calculate Luminance + luminance = cMax; + } + /** * Color Constructor using HSV color space. * @@ -79,9 +118,11 @@ public void setHSV(float hue, float saturation, float luminance) { this.saturation = saturation; this.luminance = luminance; Color vec = hsv2rgb(hue, saturation, luminance); - this.r = vec.r; - this.g = vec.g; - this.b = vec.b; + if(vec != null) { + this.r = vec.r; + this.g = vec.g; + this.b = vec.b; + } } /** @@ -103,12 +144,11 @@ public void setRGBA(int r, int g, int b, int alpha) { this.b = b; this.alpha = alpha; } - + public void setAlpha(int alpha) { this.alpha = alpha; } - - + /** * Returns the Color as a string in format RRRGGGBBB. * @@ -171,7 +211,7 @@ public float getGreenFloat() { public float getBlueFloat() { return ((float) this.b) / 255.0f; } - + public float getAlphaFloat() { return ((float) this.alpha) / 255.0f; } @@ -279,5 +319,5 @@ public static Color hsv2rgb(float hue, float saturation, float luminance) { } return null; } - + } diff --git a/src/main/java/net/aoba/gui/tabs/components/ColorPickerComponent.java b/src/main/java/net/aoba/gui/tabs/components/ColorPickerComponent.java index fe4070e1..d47b44b0 100644 --- a/src/main/java/net/aoba/gui/tabs/components/ColorPickerComponent.java +++ b/src/main/java/net/aoba/gui/tabs/components/ColorPickerComponent.java @@ -17,7 +17,7 @@ public class ColorPickerComponent extends Component implements LeftMouseDownList private String text; private boolean isSliding = false; - + private boolean collapsed = false; private float hue = 0.0f; private float saturation = 0.0f; private float luminance = 0.0f; @@ -42,6 +42,7 @@ public ColorPickerComponent(IHudElement parent, ColorSetting color) { this.text = color.displayName; this.color = color; + this.color.setOnUpdate((Color newColor) -> ensureGuiUpdated(newColor)); this.hue = color.getValue().hue; this.saturation = color.getValue().saturation; @@ -55,6 +56,12 @@ public ColorPickerComponent(IHudElement parent, ColorSetting color) { Aoba.getInstance().eventManager.AddListener(LeftMouseUpListener.class, this); } + public void ensureGuiUpdated(Color newColor) { + this.hue = newColor.hue; + this.saturation = newColor.saturation; + this.luminance = newColor.luminance; + } + public void setText(String text) { this.text = text; } @@ -139,11 +146,11 @@ public void draw(DrawContext drawContext, float partialTicks, Color color) { // Draw Outlines renderUtils.drawOutline(matrixStack, actualX + 4, actualY + 29, actualWidth - 78, actualHeight - 33); renderUtils.drawOutline(matrixStack, actualX + actualWidth - 70, actualY + 29, 30f, actualHeight - 33); - renderUtils.drawOutline(matrixStack, actualX + actualWidth - 36, actualY + 29, 30f, actualHeight - 33); + renderUtils.drawOutline(matrixStack, actualX + actualWidth - 35, actualY + 29, 30f, actualHeight - 33); // Draw Indicators renderUtils.drawCircle(matrixStack, actualX + 4 + (saturation * (actualWidth - 74)), actualY + 29 + ((1.0f - luminance) * (actualHeight - 33)), 3, new Color(255, 255, 255, 255)); renderUtils.drawOutlinedBox(matrixStack, actualX + actualWidth - 70, actualY + 29 + ((hue / 360.0f) * (actualHeight - 33)), 30, 3, new Color(255, 255, 255, 255)); - renderUtils.drawOutlinedBox(matrixStack, actualX + actualWidth - 36, actualY + 29 + ((alpha / 360.0f) * (actualHeight - 33)), 30, 3, new Color(255, 255, 255, 255)); + renderUtils.drawOutlinedBox(matrixStack, actualX + actualWidth - 36, actualY + 29 + (((255.0f - alpha) / 255.0f) * (actualHeight - 33)), 30, 3, new Color(255, 255, 255, 255)); } } \ No newline at end of file