XU XUv0.13.1
中文

UI / WIDGETS / Selection

ColorPicker

Choose a color and inspect its value

Run the Xu example

The example executes in the browser using the Xu/WASM runtime.

What to try

Choose a color and read its value.

Usage notes

Show a swatch together with a readable value. Validate input format and alpha semantics, and do not use color as the only state indicator.

Create through Screen

The Screen constructor returns a node ID. Pass that ID to Screen layout, state and event APIs. It is separate from the standalone control object documented below.

Native Xu setup fragment: call configure during initialization of an existing Screen. See the complete example for window creation, event dispatch and cleanup.

#ui/widgets
#ui/draw

fn configure(screen $&widgets.Screen) int {
    id := screen.color_picker(draw.rgb(180,69,50)) or >>
    screen.place(id,24.0,24.0,320.0,80.0) or >>
    << id
}

($&Screen) color_picker

XuAPI
pub fn (s $&Screen) color_picker(value draw.Color) int

Purpose

Create ColorPicker in Screen and return the component node ID; throw on creation failure.

Parameters

s $&Screen
The Screen instance used by this call (exclusive borrow).
value draw.Color
value (draw.Color)

Returns

int — component node ID for place and other Screen methods.

Placement, state and events

The application owns the component. Screen.place positions nodes; Screen.set_enabled and Screen.set_visible control availability and visibility. Present again after state changes, handle or errors, and keep topology changes outside paint callbacks. ToastHost uses its own queue API.

Action · Styles · UI lifecycle and styling guide

Screen.place · Screen.set_enabled · Screen.set_visible

Standalone control object API

These types and methods support applications that manage control objects, input and painting directly. Their receiver is a control object, not a Screen node ID. For Screen usage, use the constructor above and the Screen methods in the Widgets module.

struct ColorPicker

XuAPI
pub struct ColorPicker {

}

Purpose

Inline RGBA picker: four quantized 0..255 channels and a checkerboard alpha preview. Up/Down selects a channel; Left/Right, Home/End and Page keys adjust it. Each channel is also a draggable Slider. This is deliberately a small RGB editor rather than an HSV spectrum, OS eyedropper or color-profile tool.

Public fields

This type has no public fields.

(&ColorPicker) id

XuAPI
pub fn (picker &ColorPicker) id() int

Purpose

Return the unique integer ID of the element or control.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

int — a value of this type

(&ColorPicker) bounds

XuAPI
pub fn (picker &ColorPicker) bounds() layout.Rect

Purpose

Return the bounding Rectangle of the control or geometry.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

layout.Rect — a value of this type

(&ColorPicker) is_enabled

XuAPI
pub fn (picker &ColorPicker) is_enabled() bool

Purpose

Return whether the control is currently enabled.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

bool — true or false

(&ColorPicker) is_selectable

XuAPI
pub fn (picker &ColorPicker) is_selectable() bool

Purpose

Operation on &ColorPicker: whether it is selectable.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

bool — true or false

($&ColorPicker) set_selectable

XuAPI
pub fn (picker $&ColorPicker) set_selectable(selectable bool)

Purpose

Operation on $&ColorPicker: set selectable.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
selectable bool
selectable (bool)

Returns

This call does not return a value.

(&ColorPicker) is_focused

XuAPI
pub fn (picker &ColorPicker) is_focused() bool

Purpose

Return whether the control currently has input focus.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

bool — true or false

(&ColorPicker) active_channel

XuAPI
pub fn (picker &ColorPicker) active_channel() int

Purpose

Operation on &ColorPicker: active channel.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

int — a value of this type

(&ColorPicker) needs_paint

XuAPI
pub fn (picker &ColorPicker) needs_paint() bool

Purpose

Return whether the control needs repainting; call painted after painting to clear the flag.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

bool — true or false

(&ColorPicker) channel

XuAPI
pub fn (picker &ColorPicker) channel(index int) int

Purpose

Execute channel on &ColorPicker. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).
index int
index (int)

Returns

int — a value of this type

(&ColorPicker) value

XuAPI
pub fn (picker &ColorPicker) value() draw.Color

Purpose

Read the color assembled from the four current channels for painting or storage.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

A draw.Color with red, green, blue and alpha.

($&ColorPicker) set_channel

XuAPI
pub fn (picker $&ColorPicker) set_channel(index int,value int) int

Purpose

Update one RGBA channel, for example from a numeric field.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
index int
Channel: 0 red, 1 green, 2 blue, 3 alpha.
value int
Channel intensity 0..255; alpha 0 is transparent and 255 is opaque.

Returns

Returns 0 on success; failure throws rather than returning a nonzero status.

Error handling

  • A disposed control, invalid channel or out-of-range value throws instead of silently clamping.

($&ColorPicker) set_value

XuAPI
pub fn (picker $&ColorPicker) set_value(value draw.Color) int

Purpose

Set an RGBA color and synchronize all four channel sliders.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
value draw.Color
Complete draw.Color, including alpha.

Returns

Returns 0 on success; failure throws rather than returning a nonzero status.

Error handling

  • A channel update failure propagates.

(&ColorPicker) style_or

XuAPI
pub fn (picker &ColorPicker) style_or(styles &Styles) ControlStyle

Purpose

Operation on &ColorPicker: style or.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).
styles &Styles
styles (read-only borrow; caller keeps the original; type &Styles)

Returns

ControlStyle — a value of this type

($&ColorPicker) set_style

XuAPI
pub fn (picker $&ColorPicker) set_style(style &ControlStyle) int

Purpose

Operation on $&ColorPicker: set style. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
style &ControlStyle
style (read-only borrow; caller keeps the original; type &ControlStyle)

Returns

int — a value of this type

($&ColorPicker) set_enabled

XuAPI
pub fn (picker $&ColorPicker) set_enabled(enabled bool)

Purpose

Operation on $&ColorPicker: set enabled.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
enabled bool
enabled (bool)

Returns

This call does not return a value.

($&ColorPicker) set_read_only

XuAPI
pub fn (picker $&ColorPicker) set_read_only(read_only bool) int

Purpose

Operation on $&ColorPicker: set read only. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
read_only bool
read_only (bool)

Returns

int — a value of this type

($&ColorPicker) set_invalid

XuAPI
pub fn (picker $&ColorPicker) set_invalid(invalid bool)

Purpose

Operation on $&ColorPicker: set invalid.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
invalid bool
invalid (bool)

Returns

This call does not return a value.

($&ColorPicker) set_focus

XuAPI
pub fn (picker $&ColorPicker) set_focus(focused bool)

Purpose

Operation on $&ColorPicker: set focus.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
focused bool
focused (bool)

Returns

This call does not return a value.

(&ColorPicker) preview_bounds

XuAPI
pub fn (picker &ColorPicker) preview_bounds() layout.Rect

Purpose

Operation on &ColorPicker: preview bounds.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

layout.Rect — a value of this type

(&ColorPicker) channel_bounds

XuAPI
pub fn (picker &ColorPicker) channel_bounds(index int) layout.Rect

Purpose

Operation on &ColorPicker: channel bounds. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).
index int
index (int)

Returns

layout.Rect — a value of this type

($&ColorPicker) set_bounds

XuAPI
pub fn (picker $&ColorPicker) set_bounds(bounds &layout.Rect) int

Purpose

Operation on $&ColorPicker: set bounds. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
bounds &layout.Rect
bounds (read-only borrow; caller keeps the original; type &layout.Rect)

Returns

int — a value of this type

($&ColorPicker) apply_styles

XuAPI
pub fn (picker $&ColorPicker) apply_styles(styles &Styles) int

Purpose

Resolve and apply theme metrics to the control. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
styles &Styles
styles (read-only borrow; caller keeps the original; type &Styles)

Returns

int — a value of this type

($&ColorPicker) input

XuAPI
pub fn (picker $&ColorPicker) input(event &ui.InputEvent) Action

Purpose

Route input to the active RGBA channel and report color changes or commits.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
event &ui.InputEvent
Input event for the same window, canvas and generation, with coordinates in the control's canvas space.

Returns

An Action: kind identifies the action, consumed marks a handled event, request_focus requests focus, and needs_paint requests repainting.

Usage notes

  • While focused, Up/Down switches channels; Action.value contains the packed color number, and value() returns a draw.Color.

($&ColorPicker) route

XuAPI
pub fn (picker $&ColorPicker) route(kind int,event &ui.InputEvent) Action

Purpose

Route and dispatch the input event to matching targets. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
kind int
kind tag (int)
event &ui.InputEvent
event (read-only borrow; caller keeps the original; type &ui.InputEvent)

Returns

Action — input result with kind, id, repaint and focus requests.

($&ColorPicker) event

XuAPI
pub fn (picker $&ColorPicker) event(event &ui.Event) Action

Purpose

Handle and respond to window or system lifecycle and interaction events. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
event &ui.Event
event (read-only borrow; caller keeps the original; type &ui.Event)

Returns

Action — input result with kind, id, repaint and focus requests.

($&ColorPicker) measure_styled

XuAPI
pub fn (picker $&ColorPicker) measure_styled(styles &Styles) layout.Size

Purpose

Operation on $&ColorPicker: measure styled. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
styles &Styles
styles (read-only borrow; caller keeps the original; type &Styles)

Returns

layout.Size — a value of this type

($&ColorPicker) paint_styled

XuAPI
pub fn (picker $&ColorPicker) paint_styled(frame $&draw.Frame,styles &Styles) int

Purpose

Operation on $&ColorPicker: paint styled. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
frame $&draw.Frame
frame (exclusive borrow; may modify the original; type $&draw.Frame)
styles &Styles
styles (read-only borrow; caller keeps the original; type &Styles)

Returns

int — a value of this type

($&ColorPicker) dispose

XuAPI
pub fn (picker $&ColorPicker) dispose()

Purpose

Release and dispose native resources and underlying handles.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).

Returns

This call does not return a value.

(&ColorPicker) hex

XuAPI
pub fn (picker &ColorPicker) hex() string

Purpose

Execute hex on &ColorPicker.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

string — result string

($&ColorPicker) set_hex

XuAPI
pub fn (picker $&ColorPicker) set_hex(text &string) int

Purpose

Operation on $&ColorPicker: set hex. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
text &string
text (read-only borrow; caller keeps the original; type &string)

Returns

int — a value of this type

($&ColorPicker) set_swatches

XuAPI
pub fn (picker $&ColorPicker) set_swatches(colors &[]draw.Color) int

Purpose

Operation on $&ColorPicker: set swatches. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
colors &[]draw.Color
colors (read-only borrow; caller keeps the original; type &[]draw.Color)

Returns

int — a value of this type

($&ColorPicker) remember

XuAPI
pub fn (picker $&ColorPicker) remember()

Purpose

Execute remember on $&ColorPicker.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).

Returns

This call does not return a value.

(&ColorPicker) recent_colors

XuAPI
pub fn (picker &ColorPicker) recent_colors() []draw.Color

Purpose

Operation on &ColorPicker: recent colors.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).

Returns

[]draw.Color — slice result

(&ColorPicker) swatch_bounds

XuAPI
pub fn (picker &ColorPicker) swatch_bounds(index int) layout.Rect

Purpose

Operation on &ColorPicker: swatch bounds. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker &ColorPicker
The ColorPicker instance used by this call (borrow).
index int
index (int)

Returns

layout.Rect — a value of this type

($&ColorPicker) set_show_swatches

XuAPI
pub fn (picker $&ColorPicker) set_show_swatches(visible bool)

Purpose

Operation on $&ColorPicker: set show swatches.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
visible bool
visible (bool)

Returns

This call does not return a value.

($&ColorPicker) set_locale

XuAPI
pub fn (picker $&ColorPicker) set_locale(locale &Locale) int

Purpose

Operation on $&ColorPicker: set locale. Throws on failure; handle the error or propagate it to the caller.

Parameters

picker $&ColorPicker
The ColorPicker instance used by this call (exclusive borrow).
locale &Locale
locale (read-only borrow; caller keeps the original; type &Locale)

Returns

int — a value of this type

Complete native example

Component Workshop · Back to Widgets