| 1 | /*
|
| 2 | * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
|
| 3 | * Copyright (c) Meteor Development.
|
| 4 | */
|
| 5 |
|
| 6 | package meteordevelopment.meteorclient.utils.render.color;
|
| 7 |
|
| 8 | import meteordevelopment.meteorclient.utils.misc.ICopyable;
|
| 9 | import meteordevelopment.meteorclient.utils.misc.ISerializable;
|
| 10 | import net.minecraft.ChatFormatting;
|
| 11 | import net.minecraft.nbt.CompoundTag;
|
| 12 | import net.minecraft.network.chat.Style;
|
| 13 | import net.minecraft.network.chat.TextColor;
|
| 14 | import net.minecraft.world.phys.Vec3;
|
| 15 | import org.joml.Vector3f;
|
| 16 | import org.joml.Vector4f;
|
| 17 |
|
| 18 | @SuppressWarnings("unused")
|
| 19 | public class Color implements ICopyable<Color>, ISerializable<Color> {
|
| 20 | public static final Color CLEAR = new Color(0, 0, 0, 0);
|
| 21 | public static final Color WHITE = new Color(java.awt.Color.WHITE);
|
| 22 | public static final Color LIGHT_GRAY = new Color(java.awt.Color.LIGHT_GRAY);
|
| 23 | public static final Color GRAY = new Color(java.awt.Color.GRAY);
|
| 24 | public static final Color DARK_GRAY = new Color(java.awt.Color.DARK_GRAY);
|
| 25 | public static final Color BLACK = new Color(java.awt.Color.BLACK);
|
| 26 | public static final Color RED = new Color(java.awt.Color.RED);
|
| 27 | public static final Color PINK = new Color(java.awt.Color.PINK);
|
| 28 | public static final Color ORANGE = new Color(java.awt.Color.ORANGE);
|
| 29 | public static final Color YELLOW = new Color(java.awt.Color.YELLOW);
|
| 30 | public static final Color GREEN = new Color(java.awt.Color.GREEN);
|
| 31 | public static final Color MAGENTA = new Color(java.awt.Color.MAGENTA);
|
| 32 | public static final Color CYAN = new Color(java.awt.Color.CYAN);
|
| 33 | public static final Color BLUE = new Color(java.awt.Color.BLUE);
|
| 34 |
|
| 35 | public int r, g, b, a;
|
| 36 |
|
| 37 | public Color() {
|
| 38 | this(255, 255, 255, 255);
|
| 39 | }
|
| 40 |
|
| 41 | public Color(int r, int g, int b) {
|
| 42 | this.r = r;
|
| 43 | this.g = g;
|
| 44 | this.b = b;
|
| 45 | this.a = 255;
|
| 46 |
|
| 47 | validate();
|
| 48 | }
|
| 49 |
|
| 50 | public Color(int r, int g, int b, int a) {
|
| 51 | this.r = r;
|
| 52 | this.g = g;
|
| 53 | this.b = b;
|
| 54 | this.a = a;
|
| 55 |
|
| 56 | validate();
|
| 57 | }
|
| 58 |
|
| 59 | public Color(float r, float g, float b, float a) {
|
| 60 | this.r = (int) (r * 255);
|
| 61 | this.g = (int) (g * 255);
|
| 62 | this.b = (int) (b * 255);
|
| 63 | this.a = (int) (a * 255);
|
| 64 |
|
| 65 | validate();
|
| 66 | }
|
| 67 |
|
| 68 | public Color(int packed) {
|
| 69 | this.r = toRGBAR(packed);
|
| 70 | this.g = toRGBAG(packed);
|
| 71 | this.b = toRGBAB(packed);
|
| 72 | this.a = toRGBAA(packed);
|
| 73 | }
|
| 74 |
|
| 75 | public Color(Color color) {
|
| 76 | this.r = color.r;
|
| 77 | this.g = color.g;
|
| 78 | this.b = color.b;
|
| 79 | this.a = color.a;
|
| 80 | }
|
| 81 |
|
| 82 | public Color(java.awt.Color color) {
|
| 83 | this.r = color.getRed();
|
| 84 | this.g = color.getGreen();
|
| 85 | this.b = color.getBlue();
|
| 86 | this.a = color.getAlpha();
|
| 87 | }
|
| 88 |
|
| 89 | public Color(ChatFormatting formatting) {
|
| 90 | TextColor textColor = TextColor.fromLegacyFormat(formatting);
|
| 91 | if (textColor != null) {
|
| 92 | this.r = toRGBAR(textColor.getValue());
|
| 93 | this.g = toRGBAG(textColor.getValue());
|
| 94 | this.b = toRGBAB(textColor.getValue());
|
| 95 | this.a = toRGBAA(textColor.getValue());
|
| 96 | } else {
|
| 97 | this.r = 255;
|
| 98 | this.g = 255;
|
| 99 | this.b = 255;
|
| 100 | this.a = 255;
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | public Color(TextColor textColor) {
|
| 105 | this.r = toRGBAR(textColor.getValue());
|
| 106 | this.g = toRGBAG(textColor.getValue());
|
| 107 | this.b = toRGBAB(textColor.getValue());
|
| 108 | this.a = toRGBAA(textColor.getValue());
|
| 109 | }
|
| 110 |
|
| 111 | public Color(Style style) {
|
| 112 | TextColor textColor = style.getColor();
|
| 113 | if (textColor == null) {
|
| 114 | this.r = 255;
|
| 115 | this.g = 255;
|
| 116 | this.b = 255;
|
| 117 | this.a = 255;
|
| 118 | } else {
|
| 119 | this.r = toRGBAR(textColor.getValue());
|
| 120 | this.g = toRGBAG(textColor.getValue());
|
| 121 | this.b = toRGBAB(textColor.getValue());
|
| 122 | this.a = toRGBAA(textColor.getValue());
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | public static int fromRGBA(int r, int g, int b, int a) {
|
| 127 | return (r << 16) + (g << 8) + (b) + (a << 24);
|
| 128 | }
|
| 129 |
|
| 130 | public static int toRGBAR(int color) {
|
| 131 | return (color >> 16) & 0x000000FF;
|
| 132 | }
|
| 133 |
|
| 134 | public static int toRGBAG(int color) {
|
| 135 | return (color >> 8) & 0x000000FF;
|
| 136 | }
|
| 137 |
|
| 138 | public static int toRGBAB(int color) {
|
| 139 | return (color) & 0x000000FF;
|
| 140 | }
|
| 141 |
|
| 142 | public static int toRGBAA(int color) {
|
| 143 | return (color >> 24) & 0x000000FF;
|
| 144 | }
|
| 145 |
|
| 146 | public static Color fromHsv(double h, double s, double v) {
|
| 147 | double hh, p, q, t, ff;
|
| 148 | int i;
|
| 149 | double r, g, b;
|
| 150 |
|
| 151 | if (s <= 0.0) { // < is bogus, just shuts up warnings
|
| 152 | r = v;
|
| 153 | g = v;
|
| 154 | b = v;
|
| 155 | return new Color((int) (r * 255), (int) (g * 255), (int) (b * 255), 255);
|
| 156 | }
|
| 157 | hh = h;
|
| 158 | if (hh >= 360.0) hh = 0.0;
|
| 159 | hh /= 60.0;
|
| 160 | i = (int) hh;
|
| 161 | ff = hh - i;
|
| 162 | p = v * (1.0 - s);
|
| 163 | q = v * (1.0 - (s * ff));
|
| 164 | t = v * (1.0 - (s * (1.0 - ff)));
|
| 165 |
|
| 166 | switch (i) {
|
| 167 | case 0:
|
| 168 | r = v;
|
| 169 | g = t;
|
| 170 | b = p;
|
| 171 | break;
|
| 172 | case 1:
|
| 173 | r = q;
|
| 174 | g = v;
|
| 175 | b = p;
|
| 176 | break;
|
| 177 | case 2:
|
| 178 | r = p;
|
| 179 | g = v;
|
| 180 | b = t;
|
| 181 | break;
|
| 182 |
|
| 183 | case 3:
|
| 184 | r = p;
|
| 185 | g = q;
|
| 186 | b = v;
|
| 187 | break;
|
| 188 | case 4:
|
| 189 | r = t;
|
| 190 | g = p;
|
| 191 | b = v;
|
| 192 | break;
|
| 193 | case 5:
|
| 194 | default:
|
| 195 | r = v;
|
| 196 | g = p;
|
| 197 | b = q;
|
| 198 | break;
|
| 199 | }
|
| 200 | return new Color((int) (r * 255), (int) (g * 255), (int) (b * 255), 255);
|
| 201 | }
|
| 202 |
|
| 203 | public Color set(int r, int g, int b, int a) {
|
| 204 | this.r = r;
|
| 205 | this.g = g;
|
| 206 | this.b = b;
|
| 207 | this.a = a;
|
| 208 |
|
| 209 | validate();
|
| 210 |
|
| 211 | return this;
|
| 212 | }
|
| 213 |
|
| 214 | public Color r(int r) {
|
| 215 | this.r = r;
|
| 216 | validate();
|
| 217 | return this;
|
| 218 | }
|
| 219 |
|
| 220 | public Color g(int g) {
|
| 221 | this.g = g;
|
| 222 | validate();
|
| 223 | return this;
|
| 224 | }
|
| 225 |
|
| 226 | public Color b(int b) {
|
| 227 | this.b = b;
|
| 228 | validate();
|
| 229 | return this;
|
| 230 | }
|
| 231 |
|
| 232 | public Color a(int a) {
|
| 233 | this.a = a;
|
| 234 | validate();
|
| 235 | return this;
|
| 236 | }
|
| 237 |
|
| 238 | @Override
|
| 239 | public Color set(Color value) {
|
| 240 | r = value.r;
|
| 241 | g = value.g;
|
| 242 | b = value.b;
|
| 243 | a = value.a;
|
| 244 |
|
| 245 | validate();
|
| 246 |
|
| 247 | return this;
|
| 248 | }
|
| 249 |
|
| 250 | public boolean parse(String text) {
|
| 251 | String[] split = text.split(",");
|
| 252 | if (split.length != 3 && split.length != 4) return false;
|
| 253 |
|
| 254 | try {
|
| 255 | // Not assigned directly because of exception handling
|
| 256 | int r = Integer.parseInt(split[0]);
|
| 257 | int g = Integer.parseInt(split[1]);
|
| 258 | int b = Integer.parseInt(split[2]);
|
| 259 | int a = split.length == 4 ? Integer.parseInt(split[3]) : this.a;
|
| 260 |
|
| 261 | this.r = r;
|
| 262 | this.g = g;
|
| 263 | this.b = b;
|
| 264 | this.a = a;
|
| 265 |
|
| 266 | return true;
|
| 267 | } catch (NumberFormatException _) {
|
| 268 | return false;
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | @Override
|
| 273 | public Color copy() {
|
| 274 | return new Color(r, g, b, a);
|
| 275 | }
|
| 276 |
|
| 277 | public SettingColor toSetting() {
|
| 278 | return new SettingColor(r, g, b, a);
|
| 279 | }
|
| 280 |
|
| 281 | public TextColor toTextColor() {
|
| 282 | return TextColor.fromRgb(getPacked());
|
| 283 | }
|
| 284 |
|
| 285 | public Style toStyle() {
|
| 286 | return Style.EMPTY.withColor(toTextColor());
|
| 287 | }
|
| 288 |
|
| 289 | public Style styleWith(Style style) {
|
| 290 | return style.withColor(toTextColor());
|
| 291 | }
|
| 292 |
|
| 293 | public void validate() {
|
| 294 | if (r < 0) r = 0;
|
| 295 | else if (r > 255) r = 255;
|
| 296 |
|
| 297 | if (g < 0) g = 0;
|
| 298 | else if (g > 255) g = 255;
|
| 299 |
|
| 300 | if (b < 0) b = 0;
|
| 301 | else if (b > 255) b = 255;
|
| 302 |
|
| 303 | if (a < 0) a = 0;
|
| 304 | else if (a > 255) a = 255;
|
| 305 | }
|
| 306 |
|
| 307 | public Vec3 getVec3d() {
|
| 308 | return new Vec3(r / 255.0, g / 255.0, b / 255.0);
|
| 309 | }
|
| 310 |
|
| 311 | public Vector3f getVec3f() {
|
| 312 | return new Vector3f(r / 255.0f, g / 255.0f, b / 255.0f);
|
| 313 | }
|
| 314 |
|
| 315 | public Vector4f getVec4f() {
|
| 316 | return getVec4f(new Vector4f());
|
| 317 | }
|
| 318 |
|
| 319 | public Vector4f getVec4f(Vector4f dest) {
|
| 320 | return dest.set(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
|
| 321 | }
|
| 322 |
|
| 323 | public int getPacked() {
|
| 324 | return fromRGBA(r, g, b, a);
|
| 325 | }
|
| 326 |
|
| 327 | @Override
|
| 328 | public CompoundTag toTag() {
|
| 329 | CompoundTag tag = new CompoundTag();
|
| 330 |
|
| 331 | tag.putInt("r", r);
|
| 332 | tag.putInt("g", g);
|
| 333 | tag.putInt("b", b);
|
| 334 | tag.putInt("a", a);
|
| 335 |
|
| 336 | return tag;
|
| 337 | }
|
| 338 |
|
| 339 | @Override
|
| 340 | public Color fromTag(CompoundTag tag) {
|
| 341 | r = tag.getIntOr("r", 0);
|
| 342 | g = tag.getIntOr("g", 0);
|
| 343 | b = tag.getIntOr("b", 0);
|
| 344 | a = tag.getIntOr("a", 0);
|
| 345 |
|
| 346 | validate();
|
| 347 | return this;
|
| 348 | }
|
| 349 |
|
| 350 | @Override
|
| 351 | public String toString() {
|
| 352 | return r + " " + g + " " + b + " " + a;
|
| 353 | }
|
| 354 |
|
| 355 | @Override
|
| 356 | public boolean equals(Object o) {
|
| 357 | if (this == o) return true;
|
| 358 | if (o == null || getClass() != o.getClass()) return false;
|
| 359 |
|
| 360 | Color color = (Color) o;
|
| 361 |
|
| 362 | return r == color.r && g == color.g && b == color.b && a == color.a;
|
| 363 | }
|
| 364 |
|
| 365 | @Override
|
| 366 | public int hashCode() {
|
| 367 | int result = r;
|
| 368 | result = 31 * result + g;
|
| 369 | result = 31 * result + b;
|
| 370 | result = 31 * result + a;
|
| 371 | return result;
|
| 372 | }
|
| 373 | }
|
| 374 |
|