From e2be27199866b31f6fd7288dc8719db03e67ff0d Mon Sep 17 00:00:00 2001 From: BRanulf_Explode Date: Mon, 25 Aug 2025 21:03:02 +0800 Subject: [PATCH] ha --- gradle.properties | 2 +- .../toolbox/simplehud/SimpleHudHandler.java | 52 +++++++++++++++++-- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/gradle.properties b/gradle.properties index 64b11af..cd47e63 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.21.4 yarn_mappings=1.21.4+build.8 loader_version=0.16.10 # Mod Properties -mod_version=1.14.514.008 +mod_version=1.14.514.009 maven_group=org.branulf archives_base_name=branulf_toolbox # Dependencies diff --git a/src/main/java/org/branulf/toolbox/simplehud/SimpleHudHandler.java b/src/main/java/org/branulf/toolbox/simplehud/SimpleHudHandler.java index 3aafd32..3e39f28 100644 --- a/src/main/java/org/branulf/toolbox/simplehud/SimpleHudHandler.java +++ b/src/main/java/org/branulf/toolbox/simplehud/SimpleHudHandler.java @@ -178,15 +178,57 @@ public class SimpleHudHandler { } private int getColorForPercentage(float percentage) { - if (percentage > 0.80f) { - return 0x7700EEFF; // 青 - } else if (percentage > 0.30f) { - return 0x7700FF00; // 绿 + percentage = MathHelper.clamp(percentage, 0.0f, 1.0f); + + final int COLOR_CYAN = 0x7700EEFF; // 青 + final int COLOR_GREEN = 0x7700FF00; // 绿 + final int COLOR_YELLOW = 0x77FFFF00; // 黄 + final int COLOR_RED = 0x77FF0000; // 红 + + final float THRESHOLD_CYAN_GREEN = 0.75f; + final float THRESHOLD_GREEN_YELLOW = 0.50f; + final float THRESHOLD_YELLOW_RED = 0.25f; + + if (percentage >= THRESHOLD_CYAN_GREEN) { + // 青 绿 + float offset = (percentage - THRESHOLD_CYAN_GREEN) / (1.0f - THRESHOLD_CYAN_GREEN); + return interpolateColor(COLOR_GREEN, COLOR_CYAN, offset); + } else if (percentage >= THRESHOLD_GREEN_YELLOW) { + // 绿 黄 + float offset = (percentage - THRESHOLD_GREEN_YELLOW) / (THRESHOLD_CYAN_GREEN - THRESHOLD_GREEN_YELLOW); + return interpolateColor(COLOR_YELLOW, COLOR_GREEN, offset); + } else if (percentage >= THRESHOLD_YELLOW_RED) { + // 黄 红 + float offset = (percentage - THRESHOLD_YELLOW_RED) / (THRESHOLD_GREEN_YELLOW - THRESHOLD_YELLOW_RED); + return interpolateColor(COLOR_RED, COLOR_YELLOW, offset); } else { - return 0x77FF0000; // 红 + // 红 + return COLOR_RED; } } + // 从betterping那边偷来的(不是) + private int interpolateColor(int colorStart, int colorEnd, float offset) { + offset = MathHelper.clamp(offset, 0.0f, 1.0f); + + int alphaStart = (colorStart >> 24) & 0xFF; + int redStart = (colorStart >> 16) & 0xFF; + int greenStart = (colorStart >> 8) & 0xFF; + int blueStart = colorStart & 0xFF; + + int alphaEnd = (colorEnd >> 24) & 0xFF; + int redEnd = (colorEnd >> 16) & 0xFF; + int greenEnd = (colorEnd >> 8) & 0xFF; + int blueEnd = colorEnd & 0xFF; + + int newAlpha = Math.round(alphaStart + (alphaEnd - alphaStart) * offset); + int newRed = Math.round(redStart + (redEnd - redStart) * offset); + int newGreen = Math.round(greenStart + (greenEnd - greenStart) * offset); + int newBlue = Math.round(blueStart + (blueEnd - blueStart) * offset); + + return (newAlpha << 24) | (newRed << 16) | (newGreen << 8) | newBlue; + } + private void drawArc(DrawContext context, int centerX, int centerY, int radius, int thickness, int startAngleDegrees, int totalArcDegrees, float fillPercentage, int color, boolean clockwise) { RenderSystem.backupProjectionMatrix(); Matrix4f originalProjection = RenderSystem.getProjectionMatrix();