| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.Lifecycle;
|
| 6 | import java.util.HashMap;
|
| 7 | import java.util.Locale;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Objects;
|
| 10 | import net.minecraft.ChatFormatting;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | /**
|
| 14 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 15 | */
|
| 16 | public final class TextColor {
|
| 17 | private static final String CUSTOM_COLOR_PREFIX = "#";
|
| 18 | public static final Codec<TextColor> CODEC = Codec.STRING.comapFlatMap(TextColor::parseColor, TextColor::serialize);
|
| 19 | private static final Map<String, TextColor> NAMED_COLORS = new HashMap<>();
|
| 20 | public static final TextColor BLACK = named("black", 0);
|
| 21 | public static final TextColor DARK_BLUE = named("dark_blue", 170);
|
| 22 | public static final TextColor DARK_GREEN = named("dark_green", 43520);
|
| 23 | public static final TextColor DARK_AQUA = named("dark_aqua", 43690);
|
| 24 | public static final TextColor DARK_RED = named("dark_red", 11141120);
|
| 25 | public static final TextColor DARK_PURPLE = named("dark_purple", 11141290);
|
| 26 | public static final TextColor GOLD = named("gold", 16755200);
|
| 27 | public static final TextColor GRAY = named("gray", 11184810);
|
| 28 | public static final TextColor DARK_GRAY = named("dark_gray", 5592405);
|
| 29 | public static final TextColor BLUE = named("blue", 5592575);
|
| 30 | public static final TextColor GREEN = named("green", 5635925);
|
| 31 | public static final TextColor AQUA = named("aqua", 5636095);
|
| 32 | public static final TextColor RED = named("red", 16733525);
|
| 33 | public static final TextColor LIGHT_PURPLE = named("light_purple", 16733695);
|
| 34 | public static final TextColor YELLOW = named("yellow", 16777045);
|
| 35 | public static final TextColor WHITE = named("white", 16777215);
|
| 36 | private final int value;
|
| 37 | private final @Nullable String name;
|
| 38 |
|
| 39 | private TextColor(final int value, final String name) {
|
| 40 | this.value = value & 16777215;
|
| 41 | this.name = name;
|
| 42 | }
|
| 43 |
|
| 44 | private TextColor(final int value) {
|
| 45 | this.value = value & 16777215;
|
| 46 | this.name = null;
|
| 47 | }
|
| 48 |
|
| 49 | private static TextColor named(final String name, final int rgb) {
|
| 50 | TextColor result = new TextColor(rgb, name);
|
| 51 | NAMED_COLORS.put(name, result);
|
| 52 | return result;
|
| 53 | }
|
| 54 |
|
| 55 | public int getValue() {
|
| 56 | return this.value;
|
| 57 | }
|
| 58 |
|
| 59 | public String serialize() {
|
| 60 | return this.name != null ? this.name : this.formatValue();
|
| 61 | }
|
| 62 |
|
| 63 | /**
|
| 64 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 65 | */
|
| 66 | public final String formatValue() {
|
| 67 | return String.format(Locale.ROOT, "#%06X", this.value);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public boolean equals(final Object o) {
|
| 72 | if (this == o) {
|
| 73 | return true;
|
| 74 | } else if (o != null && this.getClass() == o.getClass()) {
|
| 75 | TextColor other = (TextColor)o;
|
| 76 | return this.value == other.value;
|
| 77 | } else {
|
| 78 | return false;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public int hashCode() {
|
| 84 | return Objects.hash(this.value, this.name);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public String toString() {
|
| 89 | return this.serialize();
|
| 90 | }
|
| 91 |
|
| 92 | public static @Nullable TextColor fromLegacyFormat(final ChatFormatting format) {
|
| 93 | return switch (format) {
|
| 94 | case BLACK -> BLACK;
|
| 95 | case DARK_BLUE -> DARK_BLUE;
|
| 96 | case DARK_GREEN -> DARK_GREEN;
|
| 97 | case DARK_AQUA -> DARK_AQUA;
|
| 98 | case DARK_RED -> DARK_RED;
|
| 99 | case DARK_PURPLE -> DARK_PURPLE;
|
| 100 | case GOLD -> GOLD;
|
| 101 | case GRAY -> GRAY;
|
| 102 | case DARK_GRAY -> DARK_GRAY;
|
| 103 | case BLUE -> BLUE;
|
| 104 | case GREEN -> GREEN;
|
| 105 | case AQUA -> AQUA;
|
| 106 | case RED -> RED;
|
| 107 | case LIGHT_PURPLE -> LIGHT_PURPLE;
|
| 108 | case YELLOW -> YELLOW;
|
| 109 | case WHITE -> WHITE;
|
| 110 | default -> null;
|
| 111 | };
|
| 112 | }
|
| 113 |
|
| 114 | public static TextColor fromRgb(final int rgb) {
|
| 115 | return new TextColor(rgb);
|
| 116 | }
|
| 117 |
|
| 118 | public static DataResult<TextColor> parseColor(final String color) {
|
| 119 | if (color.startsWith("#")) {
|
| 120 | try {
|
| 121 | int value = Integer.parseInt(color.substring(1), 16);
|
| 122 | return value >= 0 && value <= 16777215
|
| 123 | ? DataResult.success(fromRgb(value), Lifecycle.stable())
|
| 124 | : DataResult.error(() -> "Color value out of range: " + color);
|
| 125 | } catch (NumberFormatException e) {
|
| 126 | return DataResult.error(() -> "Invalid color value: " + color);
|
| 127 | }
|
| 128 | } else {
|
| 129 | TextColor predefinedColor = NAMED_COLORS.get(color);
|
| 130 | return predefinedColor == null ? DataResult.error(() -> "Invalid color name: " + color) : DataResult.success(predefinedColor, Lifecycle.stable());
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 |
|