ColorGradientPicker

This page talks about ColorGradientPicker class

ColorGradientPicker

The ColorGradientPicker class provides functionality for creating and displaying color gradient pickers in Minecraft Fabric.

Fields

  • client: The MinecraftClient instance.

  • onColorSelected: The callback to call when a color is selected.

  • gradientSlider: The gradient slider used by this color picker.

  • gradientBox: The gradient box used by this color picker.

  • colorPickerButton: The color picker button used by this color picker.

Constructors

ColorGradientPicker(MinecraftClient client, int x, int y, int initialColor, Consumer onColorSelected, int BoxSize, int Colors, Widget selectedWidget)

Constructs a ColorGradientPicker object with the given client instance, position, initial color, callback, box size, number of colors, and selected widget.

Methods

tick()

Updates the scale of the gradient slider and gradient box.

render(MatrixStack matrices)

Renders this color picker on screen.

mouseClicked(double mouseX, double mouseY, int button)

Handles mouse clicks on this color picker. This method updates the hue of the gradient slider and gradient box based on the mouse position if the mouse is over the gradient slider or gradient box. It also updates the saturation and value of the gradient box based on the mouse position if the mouse is over the gradient box.

mouseReleased(double mouseX, double mouseY, int button)

Handles mouse release events on this color picker. This method stops dragging or scaling the gradient slider or gradient box.

mouseDragged(double mouseX, double mouseY, int button)

Handles mouse dragging on this color picker. This method updates the hue of the gradient slider and gradient box based on the mouse position if the mouse is over the gradient slider or gradient box. It also updates the saturation and value of the gradient box based on the mouse position if the mouse is over the gradient box.

Here's an example of how to use a ColorGradientPicker in your Minecraft Fabric mod:

// Create a new ColorGradientPicker
ColorGradientPicker picker = new ColorGradientPicker(client, 10, 10, 0xFFFFFFFF, color -> {
    // Handle color selection here
}, 100, 100);

In this example, we create a new ColorGradientPicker with a position of (10, 10), an initial color of white (0xFFFFFFFF), a callback that handles color selection, and a box size and number of colors of 100.

The MouseHandler and AbstractMoveableScreen Class automatically takes care of the ColorPicker mouse inputs, but if you are thinking of using it differently somewhere else then you need to add the following to the mouse handler events and rendering.

// Render the ColorGradientPicker
picker.render(matrices);

// Handle mouse input
if (picker.mouseClicked(mouseX, mouseY)) {
    // The mouse was clicked on the picker
}
if (picker.mouseDragged(mouseX, mouseY)) {
    // The mouse was dragged on the picker
}
if (picker.mouseReleased(mouseX, mouseY)) {
    // The mouse was released on the picker
}

We then render the ColorGradientPicker on screen using its render method. We also handle mouse input by calling its mouseClicked, mouseDragged, and mouseReleased methods.