Class ColorPicker
java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.ColorPicker
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 areints: 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 callsopen(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 -
Method Summary
Modifier and TypeMethodDescription<R> Raccept(EntryVisitor<R> visitor) Calls theEntryVisitormethod for this entry's type (visitButtonfor aButton, …) and returns its result.static ColorPicker.BuilderStarts building a color picker.intcolor()The mod's color:0xRRGGBB(top byte cleared), or0xAARRGGBBwith alpha.booleanhasAlpha()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.label()The text before ": #RRGGBB", from the mod's supplier; also the editor's title.voidOpens the editor screen onhost, starting from the current color.static IntegerParses a hex color as the editor's hex field does; useful for mods that read colors from config text.voidset(int color) PassescolortoonChange(without alpha, its top byte cleared).intThe color to fill the swatch with, as0xAARRGGBB: fully opaque unless the picker has alpha.
-
Method Details
-
builder
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- ifidis invalid
-
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:
trueif the player can edit the alpha byte
-
color
public int color()The mod's color:0xRRGGBB(top byte cleared), or0xAARRGGBBwith alpha.- Returns:
- the color:
0xRRGGBBwith the top byte cleared, or0xAARRGGBBif the picker has alpha
-
swatchColor
public int swatchColor()The color to fill the swatch with, as0xAARRGGBB: fully opaque unless the picker has alpha.- Returns:
- the swatch color as
0xAARRGGBB, opaque unless the picker has alpha
-
hex
The color as the button shows it: "#RRGGBB", or "#AARRGGBB" with alpha; upper-case hex digits.- Returns:
- the color as
#RRGGBB, or#AARRGGBBwith alpha
-
set
public void set(int color) PassescolortoonChange(without alpha, its top byte cleared).- Parameters:
color-0xRRGGBB, or0xAARRGGBBwith alpha
-
parseHex
Parses a hex color as the editor's hex field does; useful for mods that read colors from config text.parseHex("#ff8800", false)is0xFF8800;parseHex("80FF8800", true)is0x80FF8800.- Parameters:
text- 6 hex digits (RRGGBB), or 8 (AARRGGBB) withalpha; with or without a leading '#', surrounding spaces ignored, any casealpha- whether to expect 8 digits instead of 6- Returns:
- the color, or
nulliftextis not such a color
-
open
-
accept
Description copied from class:EntryCalls theEntryVisitormethod for this entry's type (visitButtonfor aButton, …) and returns its result. ASectionvisits as its header, aNumberFieldas itsTextInput.
-