diff options
Diffstat (limited to 'src/main/java/de')
| -rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/tabhud/widget/component/ProgressComponent.java | 13 | ||||
| -rw-r--r-- | src/main/java/de/hysky/skyblocker/utils/ColorUtils.java | 41 |
2 files changed, 51 insertions, 3 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/tabhud/widget/component/ProgressComponent.java b/src/main/java/de/hysky/skyblocker/skyblock/tabhud/widget/component/ProgressComponent.java index ae6d64c4..3f5f3443 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/tabhud/widget/component/ProgressComponent.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/tabhud/widget/component/ProgressComponent.java @@ -1,6 +1,7 @@ package de.hysky.skyblocker.skyblock.tabhud.widget.component; import de.hysky.skyblocker.skyblock.tabhud.util.Ico; +import de.hysky.skyblocker.utils.ColorUtils; import net.minecraft.client.gui.DrawContext; import net.minecraft.item.ItemStack; import net.minecraft.text.Text; @@ -23,6 +24,7 @@ public class ProgressComponent extends Component { private final Text desc, bar; private final float pcnt; private final int color; + private final boolean colorIsBright; private final int barW; public ProgressComponent(ItemStack ico, Text d, Text b, float pcnt, int color) { @@ -43,6 +45,7 @@ public class ProgressComponent extends Component { this.barW = BAR_WIDTH; this.width = ICO_DIM + PAD_L + Math.max(this.barW, txtRend.getWidth(this.desc)); this.height = txtRend.fontHeight + PAD_S + 2 + txtRend.fontHeight + 2; + this.colorIsBright = ColorUtils.isBright(this.color); } public ProgressComponent(ItemStack ico, Text text, float pcnt, int color) { @@ -62,8 +65,12 @@ public class ProgressComponent extends Component { int barY = y + txtRend.fontHeight + PAD_S; int endOffsX = ((int) (this.barW * (this.pcnt / 100f))); context.fill(barX + endOffsX, barY, barX + this.barW, barY + BAR_HEIGHT, COL_BG_BAR); - context.fill(barX, barY, barX + endOffsX, barY + BAR_HEIGHT, - this.color); - context.drawTextWithShadow(txtRend, bar, barX + 3, barY + 2, 0xffffffff); + context.fill(barX, barY, barX + endOffsX, barY + BAR_HEIGHT, this.color); + + int textWidth = txtRend.getWidth(bar); + // Only turn text dark when it is wider than the filled bar and the filled bar is bright. + // The + 4 is because the text is indented 3 pixels and 1 extra pixel to the right as buffer. + boolean textDark = endOffsX >= textWidth + 4 && this.colorIsBright; + context.drawText(txtRend, bar, barX + 3, barY + 2, textDark ? 0xff000000 : 0xffffffff, !textDark); } } diff --git a/src/main/java/de/hysky/skyblocker/utils/ColorUtils.java b/src/main/java/de/hysky/skyblocker/utils/ColorUtils.java index 1babe5e5..a9ea6a9f 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ColorUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ColorUtils.java @@ -54,4 +54,45 @@ public class ColorUtils { return (r3 << 16) | (g3 << 8 ) | b3; } + + /** + * Checks if the specified color is bright or dark. + */ + public static boolean isBright(int color) { + float[] components = getFloatComponents(color); + + float red = components[0]; + float green = components[1]; + float blue = components[2]; + + double luminance = luminance(red, green, blue); + double whiteContrast = contrastRatio(luminance, 1.0); + double blackContrast = contrastRatio(luminance, 0.0); + return whiteContrast < blackContrast; + } + + /** + * Returns the luminance value of the color. + * + * @link <a href="https://stackoverflow.com/questions/596216/formula-to-determine-perceived-brightness-of-rgb-color/56678483#56678483">Stackoverflow explanation</a> + */ + private static double luminance(float red, float green, float blue) { + double r = (red <= 0.04045F) ? red / 12.92F : Math.pow((red + 0.055F) / 1.055F, 2.4F); + double g = (green <= 0.04045F) ? green / 12.92F : Math.pow((green + 0.055F) / 1.055F, 2.4F); + double b = (blue <= 0.04045F) ? blue / 12.92F : Math.pow((blue + 0.055F) / 1.055F, 2.4F); + + return Math.fma(0.2126, r, Math.fma(0.7152, g, 0.0722 * b)); + } + + /** + * Checks the contrast ratio between two luminance values. + */ + private static double contrastRatio(double background, double content) { + double brightest = Math.max(background, content); + double darkest = Math.min(background, content); + + return (brightest + 0.05) / (darkest + 0.05); + } + + } |
