Skip to content

Commit

Permalink
Dynamic ProgressComponent text color (#1123)
Browse files Browse the repository at this point in the history
* progress text color

* applied requested changes

* only apply tint if the bar is taking the whole text space

* applied suggested changes
  • Loading branch information
DatL4g authored Jan 5, 2025
1 parent 8575fb8 commit 15bce9f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -43,6 +45,7 @@ public ProgressComponent(ItemStack ico, Text d, Text b, float pcnt, int color) {
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) {
Expand All @@ -62,8 +65,12 @@ public void render(DrawContext context, int x, int y) {
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);
}
}
41 changes: 41 additions & 0 deletions src/main/java/de/hysky/skyblocker/utils/ColorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,45 @@ public static int interpolate(int firstColor, int secondColor, double percentage

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);
}


}

0 comments on commit 15bce9f

Please sign in to comment.