Skip to content

Commit

Permalink
Fixed colors not correctly displaying when first opening ClickGUI.
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonk9043 committed Dec 5, 2023
1 parent ab0ba08 commit 475a595
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
60 changes: 50 additions & 10 deletions src/main/java/net/aoba/gui/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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;
}
}

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -279,5 +319,5 @@ public static Color hsv2rgb(float hue, float saturation, float luminance) {
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 475a595

Please sign in to comment.