Skip to content

Commit

Permalink
color fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Oct 5, 2023
1 parent e876846 commit fba3e14
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 35 deletions.
124 changes: 92 additions & 32 deletions src/main/java/com/cleanroommc/modularui/utils/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public static int rgb(float red, float green, float blue) {
return argb(red, green, blue, 1f);
}

/**
* Converts the HSV format into ARGB. With H being hue, S being saturation and V being value.
*
* @param hue value from 0 to 360 (wraps around)
* @param saturation value from 0 to 1
* @param value value from 0 to 1
* @return the color with fully opacity
*/
public static int ofHSV(float hue, float saturation, float value) {
return ofHSV(hue, saturation, value, 1f);
}

/**
* Converts the HSV format into ARGB. With H being hue, S being saturation and V being value.
*
Expand All @@ -75,7 +87,7 @@ public static int rgb(float red, float green, float blue) {
* @param alpha value from 0 to 1
* @return the color
*/
public static int ofHSV(int hue, float saturation, float value, float alpha) {
public static int ofHSV(float hue, float saturation, float value, float alpha) {
hue %= 360;
if (hue < 0) hue += 360;
saturation = MathHelper.clamp(saturation, 0f, 1f);
Expand All @@ -87,6 +99,18 @@ public static int ofHSV(int hue, float saturation, float value, float alpha) {
return ofHxcm(hue, c, x, m, alpha);
}

/**
* Converts the HSL format into ARGB. With H being hue, S being saturation and L being lightness.
*
* @param hue value from 0 to 360 (wraps around)
* @param saturation value from 0 to 1
* @param lightness value from 0 to 1
* @return the color with fully opacity
*/
public static int ofHSL(float hue, float saturation, float lightness) {
return ofHSL(hue, saturation, lightness, 1f);
}

/**
* Converts the HSL format into ARGB. With H being hue, S being saturation and L being lightness.
*
Expand All @@ -96,8 +120,9 @@ public static int ofHSV(int hue, float saturation, float value, float alpha) {
* @param alpha value from 0 to 1
* @return the color
*/
public static int ofHSL(int hue, float saturation, float lightness, float alpha) {
public static int ofHSL(float hue, float saturation, float lightness, float alpha) {
hue %= 360;
if (hue < 0) hue += 360;
saturation = MathHelper.clamp(saturation, 0f, 1f);
lightness = MathHelper.clamp(lightness, 0f, 1f);
alpha = MathHelper.clamp(alpha, 0f, 1f);
Expand All @@ -108,9 +133,9 @@ public static int ofHSL(int hue, float saturation, float lightness, float alpha)
}

/**
* Helper method to calculate hue based argb's
* Helper method to calculate argb's of hue based formats.
*/
private static int ofHxcm(int hue, float c, float x, float m, float alpha) {
private static int ofHxcm(float hue, float c, float x, float m, float alpha) {
if (hue < 60) return argb(c + m, x + m, m, alpha);
if (hue < 120) return argb(x + m, c + m, m, alpha);
if (hue < 180) return argb(m, c + m, x + m, alpha);
Expand All @@ -120,7 +145,20 @@ private static int ofHxcm(int hue, float c, float x, float m, float alpha) {
}

/**
* Converts the CMYK format into ARGB.
* Converts the CMYK format into ARGB. All values are from 0 to 1.
*
* @param cyan cyan value
* @param magenta magenta value
* @param yellow yellow value
* @param black black value
* @return ARGB color with fully opacity
*/
public static int ofCMYK(float cyan, float magenta, float yellow, float black) {
return ofCMYK(cyan, magenta, yellow, black, 1f);
}

/**
* Converts the CMYK format into ARGB. All values are from 0 to 1.
*
* @param cyan cyan value
* @param magenta magenta value
Expand Down Expand Up @@ -302,28 +340,30 @@ public static int withAlpha(int argb, float alpha) {
* Calculates the hue value (HSV or HSL format) from the ARGB color.
*
* @param argb color
* @return hue value
* @return hue value from 0 to 360
*/
public static int getHue(int argb) {
public static float getHue(int argb) {
float r = getRedF(argb), g = getGreenF(argb), b = getBlueF(argb);
if (r == g && r == b) return 0;
float min = Math.min(r, Math.min(g, b));
float result = 0;
float hue = 0;
if (r >= g && r >= b) {
result = ((g - b) / (r - min)) % 6;
hue = ((g - b) / (r - min)) % 6;
} else if (g >= r && g >= b) {
result = ((b - r) / (g - min)) + 2;
hue = ((b - r) / (g - min)) + 2;
} else if (b >= r && b >= g) {
result = ((r - g) / (b - min)) + 4;
hue = ((r - g) / (b - min)) + 4;
}
return (int) (result * 60 + 0.5f);
hue *= 60;
if (hue < 0) hue += 360;
return hue;
}

/**
* Calculates the HSV saturation value from the ARGB color.
*
* @param argb color
* @return HSV saturation value.
* @return HSV saturation value from 0 to 1
*/
public static float getHSVSaturation(int argb) {
float r = getRedF(argb), g = getGreenF(argb), b = getBlueF(argb);
Expand All @@ -336,7 +376,7 @@ public static float getHSVSaturation(int argb) {
* Calculates the HSL saturation value from the ARGB color.
*
* @param argb color
* @return HSL saturation value.
* @return HSL saturation value from 0 to 1
*/
public static float getHSLSaturation(int argb) {
float r = getRedF(argb), g = getGreenF(argb), b = getBlueF(argb);
Expand All @@ -349,7 +389,7 @@ public static float getHSLSaturation(int argb) {
* Calculates the HSV value from the ARGB color.
*
* @param argb color
* @return HSV value.
* @return HSV value from 0 to 1
*/
public static float getValue(int argb) {
float r = getRedF(argb), g = getGreenF(argb), b = getBlueF(argb);
Expand All @@ -360,7 +400,7 @@ public static float getValue(int argb) {
* Calculates the HSL lightness value from the ARGB color.
*
* @param argb color
* @return HSL lightness value.
* @return HSL lightness value from 0 to 1
*/
public static float getLightness(int argb) {
float r = getRedF(argb), g = getGreenF(argb), b = getBlueF(argb);
Expand All @@ -373,18 +413,18 @@ public static float getLightness(int argb) {
* Replaces the hue value in the ARGB color in the HSV format.
*
* @param argb color
* @param hue new hue
* @param hue new hue from 0 to 360
* @return new ARGB color
*/
public static int withHSVHue(int argb, int hue) {
public static int withHSVHue(int argb, float hue) {
return ofHSV(hue, getHSVSaturation(argb), getValue(argb), getAlphaF(argb));
}

/**
* Replaces the saturation value in the ARGB color in the HSV format.
*
* @param argb color
* @param saturation new saturation
* @param saturation new saturation from 0 to 1
* @return new ARGB color
*/
public static int withHSVSaturation(int argb, float saturation) {
Expand All @@ -395,7 +435,7 @@ public static int withHSVSaturation(int argb, float saturation) {
* Replaces the value in the ARGB color in the HSV format.
*
* @param argb color
* @param value new value
* @param value new value from 0 to 1
* @return new ARGB color
*/
public static int withValue(int argb, float value) {
Expand All @@ -406,18 +446,18 @@ public static int withValue(int argb, float value) {
* Replaces the hue value in the ARGB color in the HSL format.
*
* @param argb color
* @param hue new hue
* @param hue new hue from 0 to 360
* @return new ARGB color
*/
public static int withHSLHue(int argb, int hue) {
public static int withHSLHue(int argb, float hue) {
return ofHSL(hue, getHSLSaturation(argb), getLightness(argb), getAlphaF(argb));
}

/**
* Replaces the saturation value in the ARGB color in the HSL format.
*
* @param argb color
* @param saturation new saturation
* @param saturation new saturation from 0 to 1
* @return new ARGB color
*/
public static int withHSLSaturation(int argb, float saturation) {
Expand All @@ -428,7 +468,7 @@ public static int withHSLSaturation(int argb, float saturation) {
* Replaces the lightness value in the ARGB color in the HSL format.
*
* @param argb color
* @param lightness new lightness
* @param lightness new lightness from 0 to 1
* @return new ARGB color
*/
public static int withLightness(int argb, float lightness) {
Expand All @@ -439,7 +479,7 @@ public static int withLightness(int argb, float lightness) {
* Calculates the CMYK cyan value from the ARGB color.
*
* @param argb color
* @return cyan value
* @return cyan value from 0 to 1
*/
public static float getCyan(int argb) {
float r = getRedF(argb), g = getGreenF(argb), b = getBlueF(argb);
Expand All @@ -451,7 +491,7 @@ public static float getCyan(int argb) {
* Calculates the CMYK magenta value from the ARGB color.
*
* @param argb color
* @return magenta value
* @return magenta value from 0 to 1
*/
public static float getMagenta(int argb) {
float r = getRedF(argb), g = getGreenF(argb), b = getBlueF(argb);
Expand All @@ -463,7 +503,7 @@ public static float getMagenta(int argb) {
* Calculates the CMYK yellow value from the ARGB color.
*
* @param argb color
* @return yellow value
* @return yellow value from 0 to 1
*/
public static float getYellow(int argb) {
float r = getRedF(argb), g = getGreenF(argb), b = getBlueF(argb);
Expand All @@ -475,7 +515,7 @@ public static float getYellow(int argb) {
* Calculates the CMYK black value from the ARGB color.
*
* @param argb color
* @return black value
* @return black value from 0 to 1
*/
public static float getBlack(int argb) {
float r = getRedF(argb), g = getGreenF(argb), b = getBlueF(argb);
Expand All @@ -486,7 +526,7 @@ public static float getBlack(int argb) {
* Replaces the cyan value in the ARGB color in the CMYK format.
*
* @param argb color
* @param cyan new cyan
* @param cyan new cyan from 0 to 1
* @return new ARGB color
*/
public static int withCyan(int argb, float cyan) {
Expand All @@ -497,7 +537,7 @@ public static int withCyan(int argb, float cyan) {
* Replaces the magenta value in the ARGB color in the CMYK format.
*
* @param argb color
* @param magenta new magenta
* @param magenta new magenta from 0 to 1
* @return new ARGB color
*/
public static int withMagenta(int argb, float magenta) {
Expand All @@ -508,7 +548,7 @@ public static int withMagenta(int argb, float magenta) {
* Replaces the yellow value in the ARGB color in the CMYK format.
*
* @param argb color
* @param yellow new yellow
* @param yellow new yellow from 0 to 1
* @return new ARGB color
*/
public static int withYellow(int argb, float yellow) {
Expand All @@ -519,7 +559,7 @@ public static int withYellow(int argb, float yellow) {
* Replaces the black value in the ARGB color in the CMYK format.
*
* @param argb color
* @param black new black
* @param black new black from 0 to 1
* @return new ARGB color
*/
public static int withBlack(int argb, float black) {
Expand All @@ -544,6 +584,26 @@ public static int[] getARGBValues(int argb) {
return new int[]{getRed(argb), getGreen(argb), getBlue(argb), getAlpha(argb)};
}

/**
* Calculates the HSV values and puts them into an array.
*
* @param argb ARGB color
* @return HSV values array
*/
public static float[] getHSVValues(int argb) {
return new float[]{getHue(argb), getHSVSaturation(argb), getValue(argb)};
}

/**
* Calculates the HSL values and puts them into an array.
*
* @param argb ARGB color
* @return HSL values array
*/
public static float[] getHSLValues(int argb) {
return new float[]{getHue(argb), getHSLSaturation(argb), getLightness(argb)};
}

/**
* Calculates the CMYK values and puts them into an array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,28 @@ private IWidget createHSVPage(IWidget alphaSlider) {
.widthRel(1f).height(12)
.child(IKey.str("H: ").asWidget().heightRel(1f))
.child(createSlider(new HueBar(GuiAxis.X))
.debugName("HUE")
.bounds(0, 360)
.value(new DoubleValue.Dynamic(() -> Color.getHue(this.color), val -> updateColor(Color.withHSVHue(this.color, (int) val))))))
.value(new DoubleValue.Dynamic(() -> {
float h = Color.getHue(this.color);
return h;
}, val -> {
float h = (float) val;
int c = Color.withHSVHue(this.color, h);
updateColor(c);
}))))
.child(new Row()
.widthRel(1f).height(12)
.child(IKey.str("S: ").asWidget().heightRel(1f))
.child(createSlider(this.sliderBackgroundS)
.bounds(0, 1)
.value(new DoubleValue.Dynamic(() -> Color.getHSVSaturation(this.color), val -> updateColor(Color.withHSVSaturation(this.color, (float) val))))))
.value(new DoubleValue.Dynamic(() -> {
float s = Color.getHSVSaturation(this.color);
return s;
}, val -> {
float s = (float) val;
int c = Color.withHSVSaturation(this.color, s);
updateColor(c);
}))))
.child(new Row()
.widthRel(1f).height(12)
.child(IKey.str("V: ").asWidget().heightRel(1f))
Expand Down

0 comments on commit fba3e14

Please sign in to comment.