Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic ProgressComponent text color #1123

Merged
merged 6 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,14 @@ 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);
int textColor = 0xffffffff;
if (endOffsX >= textWidth) {
textColor = (this.colorIsBright) ? 0xff000000 : 0xffffffff;
}

context.drawText(txtRend, bar, barX + 3, barY + 2, textColor, textColor != 0xff000000);
DatL4g marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 0.2126 * r + 0.7152 * g + 0.0722 * b;
DatL4g marked this conversation as resolved.
Show resolved Hide resolved
}

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


}