Skip to content

Color

This is an enhanced Color record that stores red, green, blue, and alpha as floats between 0.0 and 1.0. It has several constructors and factory methods to easily create colors from many sources, and it can interoperate with Minecraft code that expect decimal colors.

You can choose to use either floats between 0.0 and 1.0 or integers between 0 and 255. You can also choose to either specify the red, green, and blue components individually, or just the luminance. The alpha component can be optionally specified for all versions.

Examples for float constructors:

// Red, blue, green
new Color(0f, 0.5f, 1f);
// Red, blue, green, alpha
new Color(0f, 0.5f, 1f, 0.5f);
// Luminance
new Color(0.5f);
// Luminance, alpha
new Color(0.5f, 0.5f);

Examples for int constructors:

// Red, blue, green
new Color(0, 128, 255);
// Red, blue, green, alpha
new Color(0, 128, 255, 128);
// Luminance
new Color(128);
// Luminance, alpha
new Color(128, 128);

There are some factory methods for creating Color’s from decimal colors or Minecraft color types.

Examples:

// Ignores alpha component
Color.ofRgb(33023);
Color.ofRgb(0x0080FF);
// Parses alpha component
Color.ofArgb(-2147450625);
Color.ofArgb(0x800080FF);
// Minecraft text formatting color
Color.ofFormatting(Formatting.RED);
// Minecraft dye color
Color.ofDye(DyeColor.BLUE);

You can also parse hex code strings into Color’s.

Examples:

// Returns true if it is valid
Color.isValidHexCode("#FF5555");
// Parse hex code as color, crashes if invalid
Color.parseHexCode("ff555580");
// Returns null if it was invalid
Color.tryParseHexCode("hello");

The Color record includes some constants as well that you may prefer to use.

Examples:

Color.BLACK;
Color.WHITE;
Color.RED;
Color.GREEN;
Color.BLUE;
Color.YELLOW;
Color.MAGENTA;
Color.CYAN;

Aside from using the color component fields directly, there are three methods that you can use, two of which are for interoperating with Minecraft code.

Examples:

Color aColor = new Color(0f, 0.5f, 1f, 0.5f);
// Returns the decimal color without alpha
aColor.rgb(); // 33023
// Returns the decimal color with alpha
aColor.argb(); // -2147450625
// Stringifies the color without alpha
aColor.asHexCode(false); // "#0080FF"
// Pass true to include alpha
aColor.asHexCode(true); // "#0080FF80"