Skip to content

Color picker

ui.ColorPicker is a component that picks a color with alpha, a linear gradient (stops you add, drag and remove, plus an angle) or a fill with an opacity mask. on_change receives an event whose value is a "#rrggbb[aa]" string, a pz.Gradient or a pz.Paint, all valid backgrounds. ui.color_swatch shows any of them over a checkerboard.

ui.ColorPicker · Category: Forms · Full reference →

Color pickerColor picker
  • For theme editors, drawing apps and style properties.
  • Show a color_swatch and open the picker in a Popover when it is pressed.
import pyzed as pz
from pyzed import ui
class Styles(pz.App):
def __init__(self):
self.fill = "#3b82f6"
def view(self):
return pz.row([
ui.ColorPicker(value=self.fill, on_change=lambda e: setattr(self, "fill", e.value)),
pz.container(width=120, height=120, bg=self.fill, radius=8),
], spacing=16)
Color picker: EverythingColor picker: Everything
Everything
Code
ui.ColorPicker(value="#3b82f6cc", on_change=on_change)
Color picker: alpha=False, gradients=False, masks=FalseColor picker: alpha=False, gradients=False, masks=False
alpha=False, gradients=False, masks=False
Code
ui.ColorPicker(value="#a1c181", alpha=False, gradients=False, masks=False,
on_change=on_change)
Color picker: swatches=[...]Color picker: swatches=[...]
swatches=[...]
Code
ui.ColorPicker(value="#e5c07b", gradients=False, masks=False,
swatches=["#e06c75", "#e5c07b", "#98c379", "#61afef", "#c678dd"], on_change=on_change)

A swatch draws any color, gradient or paint over a checkerboard, so transparency shows.

Color picker: ColorColor picker: Color
Color
Code
ui.color_swatch("#3b82f6", size=32)
Color picker: With alphaColor picker: With alpha
With alpha
Code
ui.color_swatch("#a1c181aa", size=32)
Color picker: pz.GradientColor picker: pz.Gradient
pz.Gradient
Code
ui.color_swatch(pz.Gradient(["#f97316", "#7c3aed"], angle=90), size=32)
Color picker: ui.checkerboardColor picker: ui.checkerboard
ui.checkerboard
Code
ui.checkerboard(96, 48, size=8)

Color picker has 2 events of its own, listed below. Like every widget it also has the 18 common events (click, double_click, right_click, mouse_enter, drag, key_down, resize…), attached with .on(...): see Events and Events of every component.

It is a pz.Component, so it also raises on_mount and on_unmount, and its handlers get a pz.Event whose value holds the data.

Event Of Handler gets When
on_change ui.ColorPicker (event) Receives event.value.
on_press ui.color_swatch () Makes it clickable.

Handler gets is what Pyzed passes to your callable: () means nothing. Any handler can also be a message (a value that is not callable), which goes to update().

The same events written three ways. Imperative: methods of the app change self. Declarative: the handlers produce messages and one update() decides. Functional: a pure function draws an immutable state and asks for changes with set_state(...). See Imperative, declarative and functional.

class Styles(pz.App):
def __init__(self):
self.fill = "#3b82f6"
def picked(self, event): # a pz.Component: the color is event.value
self.fill = event.value
def view(self):
return pz.row([
ui.ColorPicker(value=self.fill, on_change=self.picked),
pz.container(width=80, height=80, bg=self.fill, radius=8),
ui.color_swatch("#ef4444", on_press=lambda: setattr(self, "fill", "#ef4444")),
], spacing=16)
class ColorPicker

A component that picks a color with alpha, a linear gradient (stops you add, drag and remove, plus an angle) or a fill with an opacity mask. on_change gets a "#rrggbb[aa]" string, a pz.Gradient or a pz.Paint, all valid as a background. Pass the picked value back as value. See Styles.

Property Default Description
value "#3b82f6" A color, theme token, pz.Gradient or pz.Paint.
alpha True Shows the alpha bar and field.
gradients True Shows the Solid/Gradient switch.
masks True Shows the mask editor.
swatches None Quick colors (default: the theme’s accent and status colors).

Events: on_change (events).

Full reference →

color_swatch(value, *, size=20, on_press=None)

A small square showing a color, gradient or paint over a checkerboard.

Property Default Description
value required What to show.
size 20 Side in pixels.

Events: on_press (events).

Full reference →

checkerboard(width, height, size=6)

A light/dark checkerboard to draw under translucent colors.

Property Default Description
width required Width.
height required Height.
size 6 Cell size.

Full reference →