/
ColorGetter.java
180 lines (164 loc) · 6.06 KB
/
ColorGetter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package mezz.jei.util.color;
import javax.annotation.Nullable;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import mezz.jei.util.MathUtil;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockModelShapes;
import net.minecraft.client.renderer.BlockRendererDispatcher;
import net.minecraft.client.renderer.ItemModelMesher;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.color.BlockColors;
import net.minecraft.client.renderer.color.ItemColors;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
public class ColorGetter {
private static final String[] defaultColors = new String[] {
"White:EEEEEE",
"LightBlue:7492cc",
"Cyan:00EEEE",
"Blue:2222dd",
"LapisBlue:25418b",
"Teal:008080",
"Yellow:cacb58",
"GoldenYellow:EED700",
"Orange:d97634",
"Pink:D1899D",
"HotPink:FC0FC0",
"Magenta:b24bbb",
"Purple:813eb9",
"JadedPurple:43324f",
"EvilPurple:2e1649",
"Lavender:B57EDC",
"Indigo:480082",
"Sand:dbd3a0",
"Tan:bb9b63",
"LightBrown:A0522D",
"Brown:634b33",
"DarkBrown:3a2d13",
"LimeGreen:43b239",
"SlimeGreen:83cb73",
"Green:008000",
"DarkGreen:224d22",
"GrassGreen:548049",
"Red:963430",
"BrickRed:b0604b",
"NetherBrick:2a1516",
"Redstone:ce3e36",
"Black:181515",
"CharcoalGray:464646",
"IronGray:646464",
"Gray:808080",
"Silver:C0C0C0"
};
private ColorGetter() {
}
public static String[] getColorDefaults() {
return defaultColors;
}
public static List<Color> getColors(ItemStack itemStack, int colorCount) {
final Item item = itemStack.getItem();
if (item == null) {
return Collections.emptyList();
} else if (item instanceof ItemBlock) {
final ItemBlock itemBlock = (ItemBlock) item;
final Block block = itemBlock.getBlock();
if (block == null) {
return Collections.emptyList();
}
return getColors(itemStack, block, colorCount);
} else {
return getColors(itemStack, item, colorCount);
}
}
private static List<Color> getColors(ItemStack itemStack, Item item, int colorCount) {
final ItemColors itemColors = Minecraft.getMinecraft().getItemColors();
final int renderColor = itemColors.getColorFromItemstack(itemStack, 0);
final TextureAtlasSprite textureAtlasSprite = getTextureAtlasSprite(itemStack);
if (textureAtlasSprite == null) {
return Collections.emptyList();
}
return getColors(textureAtlasSprite, renderColor, colorCount);
}
private static List<Color> getColors(ItemStack itemStack, Block block, int colorCount) {
final int meta = itemStack.getMetadata();
IBlockState blockState;
try {
blockState = block.getStateFromMeta(meta);
} catch (RuntimeException ignored) {
blockState = block.getDefaultState();
} catch (LinkageError ignored) {
blockState = block.getDefaultState();
}
final BlockColors blockColors = Minecraft.getMinecraft().getBlockColors();
final int renderColor = blockColors.colorMultiplier(blockState, null, null, 0);
final TextureAtlasSprite textureAtlasSprite = getTextureAtlasSprite(blockState);
if (textureAtlasSprite == null) {
return Collections.emptyList();
}
return getColors(textureAtlasSprite, renderColor, colorCount);
}
private static List<Color> getColors(TextureAtlasSprite textureAtlasSprite, int renderColor, int colorCount) {
final BufferedImage bufferedImage = getBufferedImage(textureAtlasSprite);
if (bufferedImage == null) {
return Collections.emptyList();
}
final List<Color> colors = new ArrayList<Color>(colorCount);
final int[][] palette = ColorThief.getPalette(bufferedImage, colorCount);
if (palette != null) {
for (int[] colorInt : palette) {
int red = (int) ((colorInt[0] - 1) * (float) (renderColor >> 16 & 255) / 255.0F);
int green = (int) ((colorInt[1] - 1) * (float) (renderColor >> 8 & 255) / 255.0F);
int blue = (int) ((colorInt[2] - 1) * (float) (renderColor & 255) / 255.0F);
red = MathUtil.clamp(red, 0, 255);
green = MathUtil.clamp(green, 0, 255);
blue = MathUtil.clamp(blue, 0, 255);
Color color = new Color(red, green, blue);
colors.add(color);
}
}
return colors;
}
@Nullable
private static BufferedImage getBufferedImage(TextureAtlasSprite textureAtlasSprite) {
final int iconWidth = textureAtlasSprite.getIconWidth();
final int iconHeight = textureAtlasSprite.getIconHeight();
final int frameCount = textureAtlasSprite.getFrameCount();
if (iconWidth <= 0 || iconHeight <= 0 || frameCount <= 0) {
return null;
}
BufferedImage bufferedImage = new BufferedImage(iconWidth, iconHeight * frameCount, BufferedImage.TYPE_4BYTE_ABGR);
for (int i = 0; i < frameCount; i++) {
int[][] frameTextureData = textureAtlasSprite.getFrameTextureData(i);
int[] largestMipMapTextureData = frameTextureData[0];
bufferedImage.setRGB(0, i * iconHeight, iconWidth, iconHeight, largestMipMapTextureData, 0, iconWidth);
}
return bufferedImage;
}
@Nullable
private static TextureAtlasSprite getTextureAtlasSprite(IBlockState blockState) {
Minecraft minecraft = Minecraft.getMinecraft();
BlockRendererDispatcher blockRendererDispatcher = minecraft.getBlockRendererDispatcher();
BlockModelShapes blockModelShapes = blockRendererDispatcher.getBlockModelShapes();
TextureAtlasSprite textureAtlasSprite = blockModelShapes.getTexture(blockState);
if (textureAtlasSprite == minecraft.getTextureMapBlocks().getMissingSprite()) {
return null;
}
return textureAtlasSprite;
}
@Nullable
private static TextureAtlasSprite getTextureAtlasSprite(ItemStack itemStack) {
RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
ItemModelMesher itemModelMesher = renderItem.getItemModelMesher();
IBakedModel itemModel = itemModelMesher.getItemModel(itemStack);
return itemModel.getParticleTexture();
}
}