1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-09-29 15:30:48 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/shared/util/ColourTracker.java
SquidDev 88de097c1c Add more general item colouring system
This allows for other items, such as turtles, to be dyed in the future.
This also adds support for the ore dictionary, meaning you can use other
mod's dyes to colour items.
2017-05-14 14:17:54 +01:00

49 lines
1.2 KiB
Java

package dan200.computercraft.shared.util;
/**
* A reimplementation of the colour system in {@link net.minecraft.item.crafting.RecipesArmorDyes}, but
* bundled together as an object.
*/
public class ColourTracker
{
private int total;
private int totalR;
private int totalG;
private int totalB;
private int count;
public void addColour( int r, int g, int b )
{
total += Math.max( r, Math.max( g, b ) );
totalR += r;
totalG += g;
totalB += b;
count++;
}
public void addColour( float r, float g, float b )
{
addColour( (int) (r * 255), (int) (g * 255), (int) (b * 255) );
}
public boolean hasColour()
{
return count > 0;
}
public int getColour()
{
int avgR = totalR / count;
int avgG = totalG / count;
int avgB = totalB / count;
float avgTotal = (float) total / (float) count;
float avgMax = (float) Math.max( avgR, Math.max( avgG, avgB ) );
avgR = (int) (avgR * avgTotal / avgMax);
avgG = (int) (avgG * avgTotal / avgMax);
avgB = (int) (avgB * avgTotal / avgMax);
return (avgR << 16) | (avgG << 8) | avgB;
}
}