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.
Creating a Color
Section titled “Creating a Color”Constructors
Section titled “Constructors”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, greennew Color(0f, 0.5f, 1f);
// Red, blue, green, alphanew Color(0f, 0.5f, 1f, 0.5f);
// Luminancenew Color(0.5f);
// Luminance, alphanew Color(0.5f, 0.5f);Examples for int constructors:
// Red, blue, greennew Color(0, 128, 255);
// Red, blue, green, alphanew Color(0, 128, 255, 128);
// Luminancenew Color(128);
// Luminance, alphanew Color(128, 128);Factory methods
Section titled “Factory methods”There are some factory methods for creating Color’s from decimal colors or Minecraft color types.
Examples:
// Ignores alpha componentColor.ofRgb(33023);Color.ofRgb(0x0080FF);
// Parses alpha componentColor.ofArgb(-2147450625);Color.ofArgb(0x800080FF);
// Minecraft text formatting colorColor.ofFormatting(Formatting.RED);
// Minecraft dye colorColor.ofDye(DyeColor.BLUE);Hex codes
Section titled “Hex codes”You can also parse hex code strings into Color’s.
Examples:
// Returns true if it is validColor.isValidHexCode("#FF5555");
// Parse hex code as color, crashes if invalidColor.parseHexCode("ff555580");
// Returns null if it was invalidColor.tryParseHexCode("hello");Constants
Section titled “Constants”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;Using the Color
Section titled “Using the Color”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 alphaaColor.rgb(); // 33023
// Returns the decimal color with alphaaColor.argb(); // -2147450625
// Stringifies the color without alphaaColor.asHexCode(false); // "#0080FF"
// Pass true to include alphaaColor.asHexCode(true); // "#0080FF80"