Skip to content

Commit

Permalink
Fix item model colours
Browse files Browse the repository at this point in the history
  • Loading branch information
Bawnorton committed Jun 24, 2023
1 parent 1d098bc commit 6796290
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bawnorton.allthetrims.compat.Compat;
import com.bawnorton.allthetrims.compat.client.YACLImpl;
import com.bawnorton.allthetrims.util.ImageUtil;
import com.bawnorton.allthetrims.util.PaletteHelper;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
Expand Down Expand Up @@ -69,7 +70,12 @@ public void onInitializeClient() {
return -1;
}

return tintIndex >= 1 ? palette.get(MathHelper.clamp(5 - tintIndex, 0, palette.size() - 1)).getRGB() : -1;
if(tintIndex < 1) return -1;
Color color = palette.get(MathHelper.clamp(6 - tintIndex, 0, palette.size() - 1));
if(tintIndex == 1) return ImageUtil.changeBrightness(color, 0.6f).getRGB();
if(tintIndex == 2) return ImageUtil.changeBrightness(color, 0.75f).getRGB();
if(tintIndex == 3) return ImageUtil.changeBrightness(color, 0.9f).getRGB();
return color.getRGB();
}, Registries.ITEM.stream().filter(item -> item instanceof Equipment).toArray(Item[]::new));
}

Expand Down
8 changes: 8 additions & 0 deletions src/client/java/com/bawnorton/allthetrims/util/ImageUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bawnorton.allthetrims.util;

import net.minecraft.util.math.MathHelper;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -74,4 +76,10 @@ public static BufferedImage removeOtherColours(BufferedImage bufferedImage, Colo
}
return maskedImage;
}

public static Color changeBrightness(Color color, float percent) {
float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
float brightness = MathHelper.clamp(hsb[2] * percent, 0, 1);
return Color.getHSBColor(hsb[0], hsb[1], brightness);
}
}
14 changes: 2 additions & 12 deletions src/client/java/com/bawnorton/allthetrims/util/PaletteHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,12 @@ private static List<Color> coloursToPalette(List<Color> colours) {
return hsb[1];
})));
Color darkest = colours.get(0);
darkest = darken(darkest);
darkest = ImageUtil.changeBrightness(darkest, 0.95f);
Color lightest = colours.get(colours.size() - 1);
lightest = lighten(lightest);
lightest = ImageUtil.changeBrightness(lightest, 1.05f);
return stretchColors(darkest, lightest);
}

private static Color darken(Color colour) {
float[] hsb = Color.RGBtoHSB(colour.getRed(), colour.getGreen(), colour.getBlue(), null);
return Color.getHSBColor(hsb[0], hsb[1], MathHelper.clamp(hsb[2] - 0.05f, 0.0f, 1.0f));
}

private static Color lighten(Color colour) {
float[] hsb = Color.RGBtoHSB(colour.getRed(), colour.getGreen(), colour.getBlue(), null);
return Color.getHSBColor(hsb[0], hsb[1], MathHelper.clamp(hsb[2] + 0.05f, 0.0f, 1.0f));
}

private static List<Color> stretchColors(Color... originalColors) {
List<Color> stretchedColors = new ArrayList<>();

Expand Down

0 comments on commit 6796290

Please sign in to comment.