colorpicker

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package colorpicker is a palette-swatch + hex-input color picker.

It is deliberately not built on picker.Model's View: round 11 of the widget-catalog gap analysis found that wrapping a separately-Render-ed swatch style in an outer cursor-highlight Render call clobbers the highlight, because ansi.Style.Render always appends a full SGR reset with no save/restore (see ansi/style.go). Model.View instead computes ONE combined ansi.Style per swatch — swatch color and cursor highlight together — and calls Render exactly once per swatch.

Example

Move across the palette and press Enter to choose the color under the cursor.

package main

import (
	"fmt"

	"github.com/ows4444/tui"
	"github.com/ows4444/tui/ansi"
	"github.com/ows4444/tui/colorpicker"
)

func main() {
	m := colorpicker.New(ansi.BasicColor(1), ansi.BasicColor(2), ansi.BasicColor(4))
	m, _ = m.Update(tui.Key{Type: tui.KeyRight})
	fmt.Println(m.Cursor())
	_, cmd := m.Update(tui.Key{Type: tui.KeyEnter})
	fmt.Println(cmd().(colorpicker.SelectedMsg).Color == ansi.BasicColor(2))
}
Output:
1
true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyMap

type KeyMap struct {
	Prev       keymap.Binding // move the palette cursor left
	Next       keymap.Binding // move the palette cursor right
	Accept     keymap.Binding // confirm the swatch, or the hex value
	SwitchPane keymap.Binding // toggle focus between palette and hex input
}

KeyMap names the keys of each action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns the keys a Model used before KeyMap existed.

type Model

type Model struct {
	Palette  []ansi.Color
	HexInput textinput.Model
	Theme    theme.Theme

	// KeyMap holds the keys for each action. New fills it with
	// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
	// behaves as if it held DefaultKeyMap. While the hex input has focus,
	// keys other than SwitchPane and Accept go to HexInput.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: a left click on a
	// swatch moves the cursor to it and gives the palette focus (a click on
	// the highlighted swatch confirms it, like Enter), a left click on the hex
	// input focuses it, and the wheel moves the palette cursor. Off (the
	// default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the picker (one
	// row); mouse events outside it are ignored.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is a keyboard-navigable palette of ansi.Color swatches plus an embedded hex text input, for callers who want an arbitrary RGB color rather than one of Palette's fixed entries.

func New

func New(palette ...ansi.Color) Model

New builds a Model from palette, with the cursor on the first entry and focus on the palette (not the hex input).

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the actions active for the focused pane, with descriptions, for help text: SwitchPane and Accept, then the palette movement keys, or the editing keys of HexInput while it has focus.

func (Model) Cursor

func (m Model) Cursor() int

Cursor returns the index of the palette entry currently under the cursor.

func (Model) HexFocused

func (m Model) HexFocused() bool

HexFocused reports whether keys are currently forwarded to HexInput rather than moving the palette cursor.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the picker to a layout.Node: the swatches keep their room and the hex input scrolls its value in whatever width is left, all cut to the allotted Size. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the picker as plain text for accessible output (see tui.Linearizer): one line per palette swatch with its colour in words, its position and ", selected" on the cursor swatch (none while the hex field has focus), then the hex input's own line.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentColorPicker, ...) and then by WithTokens.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update moves the cursor among Palette on Left/Right (clamped at the ends, not wrapping — Home/End style navigation isn't in scope here, so clamping keeps repeated Left/Right presses inert at the boundary instead of silently jumping to the opposite end), confirms the highlighted swatch on Enter, and toggles hex-input focus on Tab. While hex-focused, keys other than Tab are forwarded to HexInput.Update, and Enter attempts to parse HexInput.Value() as a "#RRGGBB" hex color: a valid value confirms it via SelectedMsg, an invalid one is a no-op (no panic, no Cmd). With Mouse on it also handles tui.MouseEvent (see Model.Mouse).

func (Model) View

func (m Model) View() string

View renders the palette swatches followed by the hex input. Each swatch is rendered with a single combined ansi.Style built for that swatch specifically (background color alone for a plain swatch, background + bold + inverse-text for the cursor swatch), then Render is called exactly once on the whole swatch block — never a base style Render wrapped inside a separate cursor-highlight Render.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

type SelectedMsg

type SelectedMsg struct {
	Color ansi.Color
}

SelectedMsg is delivered (via the Cmd Update returns) when a color is confirmed, either from the palette (Enter on a swatch) or from the hex input (Enter while hex-focused, with a valid value).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL