PaletteMapping .java

net.minecraft.client.resources.palette.PaletteMapping (record, 45 lines): members: palette, NONE, apply, create. imports: Int2IntMaps, Int2IntOpenHashMap.

net.minecraft.client.resources.paletterecord45 linesclient1.4 KB

Implements

API Surface (2)

PaletteMapping create(final Palette base, final Palette target)
int apply(final int baseColor)

Fields & Constants (1)

NONE

String literals (1)

"PaletteMapping has different sizes: "

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

PaletteMapping.java45 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.resources.palette;
2
3
import it.unimi.dsi.fastutil.ints.Int2IntMap;
4
import it.unimi.dsi.fastutil.ints.Int2IntMaps;
5
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
6
import it.unimi.dsi.fastutil.ints.IntUnaryOperator;
7
import net.fabricmc.api.EnvType;
8
import net.fabricmc.api.Environment;
9
import net.minecraft.util.ARGB;
10
11
@Environment(EnvType.CLIENT)
12
public record PaletteMapping(Int2IntMap palette) implements IntUnaryOperator {
13
	public static final PaletteMapping NONE = new PaletteMapping(Int2IntMaps.EMPTY_MAP);
14
15
	public static PaletteMapping create(final Palette base, final Palette target) {
16
		if (target.size() != base.size()) {
17
			throw new IllegalArgumentException("PaletteMapping has different sizes: " + base.size() + " != " + target.size());
18
		}
19
20
		Int2IntMap palette = new Int2IntOpenHashMap(base.size());
21
22
		for (int i = 0; i < base.size(); i++) {
23
			int key = base.get(i);
24
			if (ARGB.alpha(key) != 0) {
25
				palette.put(ARGB.opaque(key), target.get(i));
26
			}
27
		}
28
29
		return new PaletteMapping(palette);
30
	}
31
32
	@Override
33
	public int apply(final int baseColor) {
34
		int baseAlpha = ARGB.alpha(baseColor);
35
		if (baseAlpha == 0) {
36
			return baseColor;
37
		}
38
39
		int baseRgb = ARGB.opaque(baseColor);
40
		int targetRgb = this.palette.getOrDefault(baseRgb, baseRgb);
41
		int valueAlpha = ARGB.alpha(targetRgb);
42
		return ARGB.color(baseAlpha * valueAlpha / 255, targetRgb);
43
	}
44
}
45

Decompiled Minecraft 26.3 source of PaletteMapping (net/minecraft/client/resources/palette/PaletteMapping.java) with Mojang official mappings, stored verbatim on the site.