Class ColorPicker

java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.ColorPicker

public final class ColorPicker extends Entry
A color setting: a button "label: #RRGGBB" with a swatch of the color on it. Pressing it opens an editor screen where the player sets the color with red, green and blue sliders (and alpha, if enabled) or types it as hex.
ColorPicker.builder("highlight")
        .label(Text.translatable("mymod.highlight", "Highlight"))
        .value(() -> config.highlightColor)       // 0xRRGGBB
        .onChange(c -> config.highlightColor = c)
        .build();

ColorPicker.builder("overlay")
        .label(Text.translatable("mymod.overlay", "Overlay"))
        .alpha(true)                               // 0xAARRGGBB, with an alpha slider
        .value(() -> config.overlayColor)
        .onChange(c -> config.overlayColor = c)
        .build();

Colors

Colors are ints: 0xRRGGBB, or 0xAARRGGBB with alpha(true). Without alpha the top byte is ignored when reading and cleared before onChange. The value lives in the mod.

The editor

Pressing the button calls open(UiHost), which shows an editor screen (titled with the label) built from plain entries: a preview (<id>.preview, a disabled copy of this widget), 0 – 255 sliders for red, green, blue and, with alpha, alpha (<id>.red, <id>.green, <id>.blue, <id>.alpha), a hex field (<id>.hex) and Done / Cancel (<id>.done, <id>.cancel). The sliders and the hex field edit a draft; Done passes it to onChange, Cancel (and Escape) drops it, so onChange runs at most once per editing session.

Required: label, value, onChange. Can't be in a MenuScreen's footer.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
  • Method Summary

    Modifier and Type
    Method
    Description
    <R> R
    accept(EntryVisitor<R> visitor)
    Calls the EntryVisitor method for this entry's type (visitButton for a Button, …) and returns its result.
    Starts building a color picker.
    int
    The mod's color: 0xRRGGBB (top byte cleared), or 0xAARRGGBB with alpha.
    boolean
    Whether the color has an alpha byte the player can edit (ColorPicker.Builder.alpha).
    hex()
    The color as the button shows it: "#RRGGBB", or "#AARRGGBB" with alpha; upper-case hex digits.
    The text before ": #RRGGBB", from the mod's supplier; also the editor's title.
    void
    open(UiHost host)
    Opens the editor screen on host, starting from the current color.
    static Integer
    parseHex(String text, boolean alpha)
    Parses a hex color as the editor's hex field does; useful for mods that read colors from config text.
    void
    set(int color)
    Passes color to onChange (without alpha, its top byte cleared).
    int
    The color to fill the swatch with, as 0xAARRGGBB: fully opaque unless the picker has alpha.

    Methods inherited from class Entry

    enabled, id, tooltip, visible

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • builder

      public static ColorPicker.Builder builder(String id)
      Starts building a color picker.
      Parameters:
      id - the picker's id, unique within its screen; also the prefix of its editor's entry ids
      Returns:
      a new builder
      Throws:
      IllegalArgumentException - if id is invalid
    • label

      public Text label()
      The text before ": #RRGGBB", from the mod's supplier; also the editor's title.
      Returns:
      the text shown before the hex value
    • hasAlpha

      public boolean hasAlpha()
      Whether the color has an alpha byte the player can edit (ColorPicker.Builder.alpha).
      Returns:
      true if the player can edit the alpha byte
    • color

      public int color()
      The mod's color: 0xRRGGBB (top byte cleared), or 0xAARRGGBB with alpha.
      Returns:
      the color: 0xRRGGBB with the top byte cleared, or 0xAARRGGBB if the picker has alpha
    • swatchColor

      public int swatchColor()
      The color to fill the swatch with, as 0xAARRGGBB: fully opaque unless the picker has alpha.
      Returns:
      the swatch color as 0xAARRGGBB, opaque unless the picker has alpha
    • hex

      public String hex()
      The color as the button shows it: "#RRGGBB", or "#AARRGGBB" with alpha; upper-case hex digits.
      Returns:
      the color as #RRGGBB, or #AARRGGBB with alpha
    • set

      public void set(int color)
      Passes color to onChange (without alpha, its top byte cleared).
      Parameters:
      color - 0xRRGGBB, or 0xAARRGGBB with alpha
    • parseHex

      public static Integer parseHex(String text, boolean alpha)
      Parses a hex color as the editor's hex field does; useful for mods that read colors from config text. parseHex("#ff8800", false) is 0xFF8800; parseHex("80FF8800", true) is 0x80FF8800.
      Parameters:
      text - 6 hex digits (RRGGBB), or 8 (AARRGGBB) with alpha; with or without a leading '#', surrounding spaces ignored, any case
      alpha - whether to expect 8 digits instead of 6
      Returns:
      the color, or null if text is not such a color
    • open

      public void open(UiHost host)
      Opens the editor screen on host, starting from the current color. Backends call it when the button is pressed, with their own host; the editor belongs to the same mod as this entry, for Failsafe.
      Parameters:
      host - the host showing this picker's screen
    • accept

      public <R> R accept(EntryVisitor<R> visitor)
      Description copied from class: Entry
      Calls the EntryVisitor method for this entry's type (visitButton for a Button, …) and returns its result. A Section visits as its header, a NumberField as its TextInput.
      Specified by:
      accept in class Entry
      Type Parameters:
      R - the visitor's result type
      Parameters:
      visitor - the visitor, usually a backend's widget factory
      Returns:
      what the visitor returned