use crate::binding::Binding;
use crate::draw::{inset_rect, mix_color};
use sui_core::{
Color, ColorSpace, Event, ImageHandle, KeyState, Oklch, Path, PathBuilder, Point,
PointerButton, PointerEventKind, Rect, SemanticsAction, SemanticsNode, SemanticsRole,
SemanticsValue, Size, WidgetId,
};
use sui_layout::{Constraints, Padding as Insets};
use sui_reactive::Observable;
use sui_runtime::{EventCtx, FrameClock, MeasureCtx, PaintCtx, SemanticsCtx, Widget};
use sui_scene::{Brush, GradientStop, ImageSource, StrokeStyle, WidgetShader};
use sui_text::{FontFeature, TextAlign, TextStyle};
use crate::frame::{
draw_control_shape, draw_focus_ring, physical_pixels, snap_to_pixels, stroke_border,
};
use crate::{
ControlMetrics, DefaultTheme, Progress, SemanticTone, ThemeDensity, ThemeTextToken,
text_align::paint_text_line,
};
const SIGNAL_METER_PATTERN: [f32; 12] = [
0.32, 0.58, 0.86, 0.44, 0.72, 0.96, 0.52, 0.78, 0.38, 0.64, 0.90, 0.48,
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImageFit {
Fill,
Contain,
Cover,
None,
}
pub struct Image {
theme: Box<DefaultTheme>,
theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
image: ImageHandle,
label: Option<String>,
fit: ImageFit,
width: Option<f32>,
height: Option<f32>,
aspect_ratio: Option<f32>,
source_rect: Option<Rect>,
tint: Option<Color>,
background: Option<Color>,
background_reader: Option<Box<dyn Fn() -> Color>>,
show_border: bool,
corner_radius: Option<f32>,
resolved_source_size: Size,
}
impl Image {
pub fn new(image: ImageHandle) -> Self {
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
image,
label: None,
fit: ImageFit::Contain,
width: None,
height: None,
aspect_ratio: None,
source_rect: None,
tint: None,
background: None,
background_reader: None,
show_border: true,
corner_radius: None,
resolved_source_size: Size::new(96.0, 96.0),
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn semantic_name(mut self, name: impl Into<String>) -> Self {
self.label = Some(name.into());
self
}
pub fn fit(mut self, fit: ImageFit) -> Self {
self.fit = fit;
self
}
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width.max(0.0));
self
}
pub fn height(mut self, height: f32) -> Self {
self.height = Some(height.max(0.0));
self
}
pub fn size(mut self, size: Size) -> Self {
self.width = Some(size.width.max(0.0));
self.height = Some(size.height.max(0.0));
self
}
pub fn aspect_ratio(mut self, aspect_ratio: f32) -> Self {
self.aspect_ratio = Some(aspect_ratio.max(0.01));
self
}
pub fn source_rect(mut self, source_rect: Rect) -> Self {
self.source_rect = Some(source_rect);
self
}
pub fn tint(mut self, tint: Color) -> Self {
self.tint = Some(tint);
self
}
pub fn background(mut self, background: Color) -> Self {
self.background = Some(background);
self.background_reader = None;
self
}
pub fn background_when<F>(mut self, background: F) -> Self
where
F: Fn() -> Color + 'static,
{
self.background_reader = Some(Box::new(background));
self
}
pub fn show_border(mut self, show_border: bool) -> Self {
self.show_border = show_border;
self
}
pub fn corner_radius(mut self, corner_radius: f32) -> Self {
self.corner_radius = Some(corner_radius.max(0.0));
self
}
fn effective_source_size(&self, ctx: &MeasureCtx) -> Size {
self.source_rect
.map(|rect| rect.size)
.or_else(|| ctx.layout().image_size(self.image))
.unwrap_or(Size::new(96.0, 96.0))
}
fn resolved_size(&self) -> Size {
let aspect_ratio = self.aspect_ratio.unwrap_or_else(|| {
(self.resolved_source_size.width / self.resolved_source_size.height.max(1.0)).max(0.01)
});
match (self.width, self.height) {
(Some(width), Some(height)) => Size::new(width, height),
(Some(width), None) => Size::new(width, width / aspect_ratio),
(None, Some(height)) => Size::new(height * aspect_ratio, height),
(None, None) => self.resolved_source_size,
}
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme)
}
fn resolved_background(&self) -> Option<Color> {
self.background_reader
.as_ref()
.map(|background| background())
.or(self.background)
}
fn resolved_corner_radius(&self, theme: &DefaultTheme) -> f32 {
self.corner_radius
.unwrap_or(theme.metrics.image_corner_radius)
}
}
impl Widget for Image {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.resolved_source_size = self.effective_source_size(ctx);
constraints.clamp(self.resolved_size())
}
fn paint(&self, ctx: &mut PaintCtx) {
let bounds = ctx.bounds();
let theme = self.resolved_theme();
let corner_radius = self.resolved_corner_radius(&theme);
let image_rect = fit_rect(bounds, self.resolved_source_size, self.fit);
if let Some(background) = self.resolved_background() {
ctx.fill(Path::rounded_rect(bounds, corner_radius), background);
}
if corner_radius > 0.0 {
ctx.push_clip(Path::rounded_rect(bounds, corner_radius));
} else {
ctx.push_clip_rect(bounds);
}
let mut source = ImageSource::new(self.image);
if let Some(source_rect) = self.source_rect {
source = source.with_source_rect(source_rect);
}
if let Some(tint) = self.tint {
source = source.with_tint(tint);
}
ctx.draw_image_source(image_rect, source);
ctx.pop_clip();
if self.show_border {
stroke_border(
ctx,
bounds,
corner_radius,
physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
theme.palette.border,
);
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Image, ctx.bounds());
node.name = self.label.clone();
ctx.push(node);
}
}
pub struct SignalMeter {
theme: Box<DefaultTheme>,
theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
name: String,
description: Option<String>,
active: bool,
active_reader: Option<Box<dyn Fn() -> bool>>,
level: Option<f32>,
level_reader: Option<Box<dyn Fn() -> f32>>,
tone: SemanticTone,
tone_reader: Option<Box<dyn Fn() -> SemanticTone>>,
bars: usize,
size: Option<Size>,
gap: Option<f32>,
}
impl SignalMeter {
pub fn new(name: impl Into<String>) -> Self {
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
name: name.into(),
description: None,
active: false,
active_reader: None,
level: None,
level_reader: None,
tone: SemanticTone::Accent,
tone_reader: None,
bars: 12,
size: None,
gap: None,
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self.active_reader = None;
self
}
pub fn active_when<F>(mut self, active: F) -> Self
where
F: Fn() -> bool + 'static,
{
self.active_reader = Some(Box::new(active));
self
}
pub fn level(mut self, level: f32) -> Self {
self.level = Some(level);
self.level_reader = None;
self
}
pub fn level_when<F>(mut self, level: F) -> Self
where
F: Fn() -> f32 + 'static,
{
self.level_reader = Some(Box::new(level));
self
}
pub fn tone(mut self, tone: SemanticTone) -> Self {
self.tone = tone;
self.tone_reader = None;
self
}
pub fn tone_when<F>(mut self, tone: F) -> Self
where
F: Fn() -> SemanticTone + 'static,
{
self.tone_reader = Some(Box::new(tone));
self
}
pub fn bars(mut self, bars: usize) -> Self {
self.bars = bars.clamp(3, 64);
self
}
pub fn size(mut self, size: Size) -> Self {
self.size = Some(Size::new(size.width.max(0.0), size.height.max(0.0)));
self
}
pub fn gap(mut self, gap: f32) -> Self {
self.gap = Some(gap.max(0.0));
self
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme)
}
fn is_active(&self) -> bool {
self.active_reader
.as_ref()
.map(|active| active())
.unwrap_or(self.active)
}
fn resolved_level(&self) -> Option<f32> {
self.level_reader
.as_ref()
.map(|level| level())
.or(self.level)
.map(|level| {
if level.is_finite() {
level.clamp(0.0, 1.0)
} else {
0.0
}
})
}
fn resolved_tone(&self) -> SemanticTone {
self.tone_reader
.as_ref()
.map(|tone| tone())
.unwrap_or(self.tone)
}
fn bar_count(&self) -> usize {
self.bars.clamp(3, 64)
}
fn resolved_size(&self) -> Size {
self.size.unwrap_or(Size::new(76.0, 16.0))
}
}
fn signal_meter_bar_layout(
width: f32,
requested_bars: usize,
requested_gap: f32,
) -> (usize, f32, f32) {
let width = if width.is_finite() {
width.max(0.0)
} else {
0.0
};
let bars = requested_bars.max(1).min(width.floor().max(1.0) as usize);
let max_gap = if bars > 1 {
((width - bars as f32).max(0.0) / (bars - 1) as f32).max(0.0)
} else {
0.0
};
let gap = requested_gap.max(0.0).min(max_gap);
let total_gap = gap * bars.saturating_sub(1) as f32;
let bar_width = ((width - total_gap) / bars as f32).max(0.0);
(bars, gap, bar_width)
}
impl Widget for SignalMeter {
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(self.resolved_size())
}
fn paint(&self, ctx: &mut PaintCtx) {
let theme = self.resolved_theme();
let bounds = ctx.bounds();
let active = self.is_active();
let (bars, gap, bar_w) =
signal_meter_bar_layout(bounds.width(), self.bar_count(), self.gap.unwrap_or(3.0));
let reached_bars = self
.resolved_level()
.map(|level| (level * bars as f32).ceil() as usize);
let tone = theme.semantic_tone_color(self.resolved_tone());
let idle_fill = theme.palette.text_muted.with_alpha(0.48);
let min_h = (bounds.height() * if active { 0.20 } else { 0.12 }).max(1.0);
let max_h = bounds.height().max(1.0);
for index in 0..bars {
let pattern = SIGNAL_METER_PATTERN[index % SIGNAL_METER_PATTERN.len()];
let level = if active {
pattern
} else {
(pattern * 0.26).max(0.12)
};
let bar_h = (min_h + (max_h - min_h) * level).min(max_h);
let x = bounds.x() + index as f32 * (bar_w + gap);
let y = bounds.max_y() - bar_h;
let rect = Rect::new(x, y, bar_w, bar_h);
let reached = reached_bars.is_none_or(|reached| index < reached);
let fill = if active && reached { tone } else { idle_fill };
ctx.fill(Path::rounded_rect(rect, (bar_w * 0.5).min(3.0)), fill);
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let level = self.resolved_level();
let mut node = SemanticsNode::new(
ctx.widget_id(),
if level.is_some() {
SemanticsRole::ProgressBar
} else {
SemanticsRole::GenericContainer
},
ctx.bounds(),
);
node.name = Some(self.name.clone());
node.description = self.description.clone();
node.value = Some(match level {
Some(level) => SemanticsValue::Range {
value: level as f64,
min: 0.0,
max: 1.0,
},
None => SemanticsValue::Text(if self.is_active() {
"active".to_string()
} else {
"idle".to_string()
}),
});
ctx.push(node);
}
}
pub struct ColorSwatch {
theme: Box<DefaultTheme>,
theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
name: String,
color: Color,
color_reader: Option<Box<dyn Fn() -> Color>>,
size: Option<Size>,
hovered: bool,
pressed: bool,
hover_animation: AnimatedScalar,
press_animation: AnimatedScalar,
focus_animation: AnimatedScalar,
read_only: bool,
on_press: Option<Box<dyn FnMut(Color)>>,
on_press_with_ctx: Option<Box<dyn FnMut(&mut EventCtx, Color)>>,
}
impl ColorSwatch {
pub fn new(name: impl Into<String>, color: Color) -> Self {
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
name: name.into(),
color,
color_reader: None,
size: None,
hovered: false,
pressed: false,
hover_animation: AnimatedScalar::new(0.0),
press_animation: AnimatedScalar::new(0.0),
focus_animation: AnimatedScalar::new(0.0),
read_only: false,
on_press: None,
on_press_with_ctx: None,
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn size(mut self, size: Size) -> Self {
self.size = Some(Size::new(size.width.max(24.0), size.height.max(24.0)));
self
}
pub fn color_when<F>(mut self, color: F) -> Self
where
F: Fn() -> Color + 'static,
{
self.color_reader = Some(Box::new(color));
self
}
pub fn read_only(mut self, read_only: bool) -> Self {
self.read_only = read_only;
self
}
pub fn is_read_only(&self) -> bool {
self.read_only
}
pub fn on_press<F>(mut self, on_press: F) -> Self
where
F: FnMut(Color) + 'static,
{
self.on_press = Some(Box::new(on_press));
self
}
pub fn on_press_with_ctx<F>(mut self, on_press: F) -> Self
where
F: FnMut(&mut EventCtx, Color) + 'static,
{
self.on_press_with_ctx = Some(Box::new(on_press));
self
}
fn current_color(&self) -> Color {
self.color_reader
.as_ref()
.map(|reader| reader())
.unwrap_or(self.color)
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme)
}
fn activate(&mut self, ctx: &mut EventCtx) {
let color = self.current_color();
if let Some(on_press) = &mut self.on_press {
on_press(color);
}
if let Some(on_press) = &mut self.on_press_with_ctx {
on_press(ctx, color);
}
}
fn resolved_size(&self, theme: &DefaultTheme) -> Size {
self.size.unwrap_or(Size::new(
theme.metrics.color_swatch_width,
theme.metrics.color_swatch_height,
))
}
fn set_hovered(&mut self, hovered: bool, ctx: &mut EventCtx) {
if self.hovered != hovered {
let theme = self.resolved_theme();
self.hovered = hovered;
set_hover_animation_target(
&mut self.hover_animation,
hovered as u8 as f32,
&theme,
ctx,
);
ctx.request_paint();
ctx.request_semantics();
}
}
fn set_pressed(&mut self, pressed: bool, ctx: &mut EventCtx) {
if self.pressed != pressed {
let theme = self.resolved_theme();
self.pressed = pressed;
set_press_animation_target(
&mut self.press_animation,
pressed as u8 as f32,
&theme,
ctx,
);
ctx.request_paint();
ctx.request_semantics();
}
}
}
impl Widget for ColorSwatch {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
if self.read_only {
if matches!(
event,
Event::Pointer(pointer)
if matches!(
pointer.kind,
PointerEventKind::Move
| PointerEventKind::Leave
| PointerEventKind::Cancel
)
) {
ctx.request_paint();
ctx.request_semantics();
}
return;
}
match event {
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
self.set_hovered(ctx.bounds().contains(pointer.position), ctx);
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Down
&& pointer.button == Some(PointerButton::Primary)
&& ctx.bounds().contains(pointer.position) =>
{
self.set_pressed(true, ctx);
self.set_hovered(true, ctx);
ctx.request_focus();
ctx.request_pointer_capture(pointer.pointer_id);
ctx.set_handled();
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Up
&& pointer.button == Some(PointerButton::Primary) =>
{
let hovered = ctx.bounds().contains(pointer.position);
if self.pressed && hovered {
self.activate(ctx);
}
self.set_pressed(false, ctx);
self.set_hovered(hovered, ctx);
ctx.release_pointer_capture(pointer.pointer_id);
ctx.set_handled();
}
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Leave => {
self.set_hovered(false, ctx);
}
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Cancel => {
if self.pressed {
self.set_pressed(false, ctx);
self.set_hovered(false, ctx);
ctx.release_pointer_capture(pointer.pointer_id);
}
}
Event::Keyboard(key)
if ctx.is_focused()
&& key.state == KeyState::Pressed
&& matches!(key.key.as_str(), "Enter" | " ") =>
{
self.activate(ctx);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
_ => {}
}
}
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let theme = self.resolved_theme();
constraints.clamp(self.resolved_size(&theme))
}
fn paint(&self, ctx: &mut PaintCtx) {
let theme = self.resolved_theme();
let metrics = theme.metrics;
let palette = theme.palette;
let pressed_offset = self.press_animation.get(ctx) * theme.interaction.pressed_offset;
let body = Rect::new(
ctx.bounds().x(),
ctx.bounds().y() + pressed_offset,
ctx.bounds().width(),
(ctx.bounds().height() - pressed_offset).max(0.0),
);
let outer_radius = metrics.corner_radius.min(body.height() * 0.5);
let inner_inset = metrics.color_swatch_inner_inset + pressed_offset * 0.5;
let inner_radius = (outer_radius - inner_inset).max(0.0);
let inner_body = inset_rect(body, Insets::all(inner_inset));
let color = self.current_color();
if self.focus_animation.get(ctx) > 0.0 {
draw_focus_ring(
ctx,
ctx.bounds(),
outer_radius,
metrics,
palette.focus_ring.with_alpha(self.focus_animation.get(ctx)),
);
}
if self.press_animation.get(ctx) > 0.0 {
ctx.fill(
Path::rounded_rect(ctx.bounds(), outer_radius),
mix_color(
palette.control_hover,
palette.control_active,
theme.interaction.pressed_blend * self.press_animation.get(ctx),
),
);
} else if self.hover_animation.get(ctx) > 0.0 {
ctx.fill(
Path::rounded_rect(ctx.bounds(), outer_radius),
palette
.control_hover
.with_alpha(self.hover_animation.get(ctx) * palette.control_hover.alpha),
);
}
draw_rounded_checkerboard(
ctx,
inner_body,
inner_radius,
metrics.color_swatch_checker_size,
&theme,
);
ctx.fill(Path::rounded_rect(inner_body, inner_radius), color);
stroke_border(
ctx,
body,
outer_radius,
physical_pixels(ctx, metrics.border_width.max(1.0)),
if ctx.is_focused() {
palette.border_focus
} else if self.hovered || self.hover_animation.get(ctx) > 0.0 {
palette.border_hover
} else if self.read_only {
palette
.border
.with_alpha(theme.interaction.disabled_opacity)
} else {
palette.border
},
);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let mut node =
SemanticsNode::new(ctx.widget_id(), SemanticsRole::ColorSwatch, ctx.bounds());
node.name = Some(self.name.clone());
node.state.focused = ctx.is_focused();
node.value = Some(SemanticsValue::Text(format_color(self.current_color())));
if !self.read_only {
node.actions = vec![SemanticsAction::Focus, SemanticsAction::Activate];
}
ctx.push(node);
}
fn accepts_focus(&self) -> bool {
!self.read_only
}
fn focus_changed(&mut self, ctx: &mut EventCtx, focused: bool) {
let theme = self.resolved_theme();
set_focus_animation_target(&mut self.focus_animation, focused as u8 as f32, &theme, ctx);
ctx.request_paint();
ctx.request_semantics();
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ColorPaletteSwatch {
name: String,
color: Color,
}
impl ColorPaletteSwatch {
pub fn new(name: impl Into<String>, color: Color) -> Self {
Self {
name: name.into(),
color,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub const fn color(&self) -> Color {
self.color
}
}
pub struct ColorPalette {
theme: Box<DefaultTheme>,
theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
enabled: Binding<bool>,
name: String,
swatches: Vec<ColorPaletteSwatch>,
selected: Option<usize>,
selected_reader: Option<Box<dyn Fn() -> Option<usize>>>,
hovered: Option<usize>,
hover_visual: Option<usize>,
pressed: Option<usize>,
press_visual: Option<usize>,
hover_animation: AnimatedScalar,
press_animation: AnimatedScalar,
focus_animation: AnimatedScalar,
columns: usize,
swatch_size: Option<f32>,
gap: Option<f32>,
on_change: Option<Box<dyn FnMut(usize, String, Color)>>,
on_change_with_ctx: Option<Box<dyn FnMut(&mut EventCtx, usize, String, Color)>>,
}
impl ColorPalette {
pub fn new(name: impl Into<String>) -> Self {
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
enabled: Binding::new(true),
name: name.into(),
swatches: Vec::new(),
selected: None,
selected_reader: None,
hovered: None,
hover_visual: None,
pressed: None,
press_visual: None,
hover_animation: AnimatedScalar::new(0.0),
press_animation: AnimatedScalar::new(0.0),
focus_animation: AnimatedScalar::new(0.0),
columns: 8,
swatch_size: None,
gap: None,
on_change: None,
on_change_with_ctx: None,
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled.set(enabled);
self
}
pub fn enabled_when<F>(mut self, enabled: F) -> Self
where
F: Fn() -> bool + 'static,
{
self.enabled.set_when(enabled);
self
}
pub fn enabled_from<O>(mut self, enabled: O) -> Self
where
O: Observable<bool> + 'static,
{
self.enabled.set_from(enabled);
self
}
pub fn is_enabled(&self) -> bool {
self.enabled.get()
}
fn paint_theme(&self) -> DefaultTheme {
let theme = self.resolved_theme();
if self.enabled.get() {
theme
} else {
theme.for_disabled_control()
}
}
pub fn swatch(mut self, swatch: ColorPaletteSwatch) -> Self {
self.swatches.push(swatch);
self
}
pub fn swatches<I>(mut self, swatches: I) -> Self
where
I: IntoIterator<Item = ColorPaletteSwatch>,
{
self.swatches.extend(swatches);
self
}
pub fn selected(mut self, selected: impl Into<Option<usize>>) -> Self {
let selected = selected.into();
self.selected = selected;
self.selected_reader = None;
self
}
pub fn selected_when<F>(mut self, selected: F) -> Self
where
F: Fn() -> Option<usize> + 'static,
{
self.selected_reader = Some(Box::new(selected));
self
}
pub fn columns(mut self, columns: usize) -> Self {
self.columns = columns.max(1);
self
}
pub fn swatch_size(mut self, size: f32) -> Self {
self.swatch_size = Some(size.max(18.0));
self
}
pub fn gap(mut self, gap: f32) -> Self {
self.gap = Some(gap.max(0.0));
self
}
pub fn on_change<F>(mut self, on_change: F) -> Self
where
F: FnMut(usize, String, Color) + 'static,
{
self.on_change = Some(Box::new(on_change));
self
}
pub fn on_change_with_ctx<F>(mut self, on_change: F) -> Self
where
F: FnMut(&mut EventCtx, usize, String, Color) + 'static,
{
self.on_change_with_ctx = Some(Box::new(on_change));
self
}
pub fn selected_index(&self) -> Option<usize> {
self.current_selected()
}
fn current_selected(&self) -> Option<usize> {
self.selected_reader
.as_ref()
.map(|selected| selected())
.unwrap_or(self.selected)
.filter(|index| *index < self.swatches.len())
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme)
}
fn grid_columns(&self) -> usize {
self.columns.max(1).min(self.swatches.len().max(1))
}
fn grid_rows(&self) -> usize {
if self.swatches.is_empty() {
1
} else {
self.swatches.len().div_ceil(self.grid_columns())
}
}
fn resolved_swatch_size(&self, theme: &DefaultTheme) -> f32 {
self.swatch_size
.unwrap_or(theme.metrics.color_palette_swatch_size)
}
fn resolved_gap(&self, theme: &DefaultTheme) -> f32 {
self.gap.unwrap_or(theme.metrics.color_palette_gap)
}
fn swatch_rect(&self, bounds: Rect, index: usize, theme: &DefaultTheme) -> Option<Rect> {
if index >= self.swatches.len() {
return None;
}
let swatch_size = self.resolved_swatch_size(theme);
let gap = self.resolved_gap(theme);
let columns = self.grid_columns();
let column = index % columns;
let row = index / columns;
let x = bounds.x() + column as f32 * (swatch_size + gap);
let y = bounds.y() + row as f32 * (swatch_size + gap);
let available_width = (bounds.max_x() - x).max(0.0);
let available_height = (bounds.max_y() - y).max(0.0);
let rect = Rect::new(
x,
y,
swatch_size.min(available_width),
swatch_size.min(available_height),
);
(!rect.is_empty()).then_some(rect)
}
fn swatch_at(&self, bounds: Rect, position: Point, theme: &DefaultTheme) -> Option<usize> {
self.swatches.iter().enumerate().find_map(|(index, _)| {
self.swatch_rect(bounds, index, theme)
.filter(|rect| rect.contains(position))
.map(|_| index)
})
}
fn activate(&mut self, ctx: &mut EventCtx, index: usize) {
if self.swatches.is_empty() {
return;
}
let index = index.min(self.swatches.len() - 1);
self.selected = Some(index);
let swatch = &self.swatches[index];
let name = swatch.name.clone();
let color = swatch.color;
if let Some(on_change) = &mut self.on_change {
on_change(index, name.clone(), color);
}
if let Some(on_change_with_ctx) = &mut self.on_change_with_ctx {
on_change_with_ctx(ctx, index, name, color);
}
}
fn move_selection(&mut self, delta: isize, ctx: &mut EventCtx) {
if self.swatches.is_empty() {
return;
}
let current = self.current_selected().unwrap_or(0) as isize;
let last = self.swatches.len() as isize - 1;
let next = (current + delta).clamp(0, last) as usize;
self.set_hovered(Some(next), ctx);
self.activate(ctx, next);
}
fn selected_value(&self) -> Option<String> {
self.current_selected()
.and_then(|index| self.swatches.get(index))
.map(|swatch| format!("{} {}", swatch.name, format_color(swatch.color)))
}
fn set_hovered(&mut self, hovered: Option<usize>, ctx: &mut EventCtx) {
if self.hovered == hovered {
return;
}
let theme = self.resolved_theme();
self.hovered = hovered;
if let Some(index) = hovered {
self.hover_visual = Some(index);
self.hover_animation = AnimatedScalar::new(0.0);
set_hover_animation_target(&mut self.hover_animation, 1.0, &theme, ctx);
} else if !set_hover_animation_target(&mut self.hover_animation, 0.0, &theme, ctx) {
self.hover_visual = None;
}
ctx.request_paint();
ctx.request_semantics();
}
fn set_pressed(&mut self, pressed: Option<usize>, ctx: &mut EventCtx) {
if self.pressed == pressed {
return;
}
let theme = self.resolved_theme();
self.pressed = pressed;
if let Some(index) = pressed {
self.press_visual = Some(index);
self.press_animation = AnimatedScalar::new(0.0);
set_press_animation_target(&mut self.press_animation, 1.0, &theme, ctx);
} else if !set_press_animation_target(&mut self.press_animation, 0.0, &theme, ctx) {
self.press_visual = None;
}
ctx.request_paint();
ctx.request_semantics();
}
fn hover_amount_for(&self, index: usize, clock: &impl FrameClock) -> f32 {
if self.hover_visual == Some(index) {
self.hover_animation.get(clock)
} else {
0.0
}
}
fn press_amount_for(&self, index: usize, clock: &impl FrameClock) -> f32 {
if self.press_visual == Some(index) {
self.press_animation.get(clock)
} else {
0.0
}
}
}
impl Widget for ColorPalette {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
if !self.enabled.get() {
if let Event::Pointer(pointer) = event
&& pointer.kind == PointerEventKind::Up
{
ctx.release_pointer_capture(pointer.pointer_id);
}
return;
}
match event {
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
let theme = self.resolved_theme();
self.set_hovered(self.swatch_at(ctx.bounds(), pointer.position, &theme), ctx);
}
Event::Pointer(_pointer) if matches!(_pointer.kind, PointerEventKind::Leave) => {
self.set_hovered(None, ctx);
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Down
&& pointer.button == Some(PointerButton::Primary) =>
{
let theme = self.resolved_theme();
let hovered = self.swatch_at(ctx.bounds(), pointer.position, &theme);
self.set_hovered(hovered, ctx);
self.set_pressed(hovered, ctx);
if self.hovered.is_some() {
ctx.request_focus();
ctx.request_pointer_capture(pointer.pointer_id);
ctx.set_handled();
}
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Up
&& pointer.button == Some(PointerButton::Primary) =>
{
let theme = self.resolved_theme();
let hovered = self.swatch_at(ctx.bounds(), pointer.position, &theme);
if let Some(index) = self
.pressed
.zip(hovered)
.filter(|(left, right)| left == right)
.map(|(index, _)| index)
{
self.activate(ctx, index);
}
self.set_hovered(hovered, ctx);
self.set_pressed(None, ctx);
ctx.release_pointer_capture(pointer.pointer_id);
ctx.set_handled();
}
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Cancel => {
if self.pressed.is_some() {
self.set_pressed(None, ctx);
self.set_hovered(None, ctx);
ctx.release_pointer_capture(pointer.pointer_id);
ctx.set_handled();
}
}
Event::Keyboard(key) if ctx.is_focused() && key.state == KeyState::Pressed => {
let columns = self.grid_columns() as isize;
match key.key.as_str() {
"ArrowLeft" => self.move_selection(-1, ctx),
"ArrowRight" => self.move_selection(1, ctx),
"ArrowUp" => self.move_selection(-columns, ctx),
"ArrowDown" => self.move_selection(columns, ctx),
"Home" => self.activate(ctx, 0),
"End" if !self.swatches.is_empty() => {
self.activate(ctx, self.swatches.len() - 1);
}
"Enter" | " " => {
if let Some(selected) = self.current_selected().or(Some(0)) {
self.activate(ctx, selected);
}
}
_ => return,
}
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
_ => {}
}
}
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let theme = self.resolved_theme();
let swatch_size = self.resolved_swatch_size(&theme);
let gap = self.resolved_gap(&theme);
let columns = self.grid_columns() as f32;
let rows = self.grid_rows() as f32;
constraints.clamp(Size::new(
columns * swatch_size + (columns - 1.0).max(0.0) * gap,
rows * swatch_size + (rows - 1.0).max(0.0) * gap,
))
}
fn paint(&self, ctx: &mut PaintCtx) {
self.enabled.observe(|source| {
ctx.observe(source);
});
let theme = self.paint_theme();
let palette = theme.palette;
let metrics = theme.metrics;
let interaction = theme.interaction;
let swatch_size = self.resolved_swatch_size(&theme);
let radius = metrics.corner_radius.min(swatch_size * 0.25);
let selected = self.current_selected();
if self.focus_animation.get(ctx) > 0.0 {
draw_focus_ring(
ctx,
ctx.bounds(),
radius,
metrics,
palette.focus_ring.with_alpha(self.focus_animation.get(ctx)),
);
}
for (index, swatch) in self.swatches.iter().enumerate() {
let Some(rect) = self.swatch_rect(ctx.bounds(), index, &theme) else {
continue;
};
let selected = selected == Some(index);
let hovered = self.hovered == Some(index);
let hover_amount = self.hover_amount_for(index, ctx);
let press_amount = self.press_amount_for(index, ctx);
let pressed_offset = press_amount * interaction.pressed_offset;
let body = Rect::new(
rect.x(),
rect.y() + pressed_offset,
rect.width(),
(rect.height() - pressed_offset).max(0.0),
);
let ring = if selected {
palette.selection_border
} else if hovered || hover_amount > 0.0 || press_amount > 0.0 {
palette.border_hover
} else {
palette.border
};
let ring_width = physical_pixels(
ctx,
if selected {
metrics.border_width.max(1.0) + 1.0
} else {
metrics.border_width.max(1.0)
},
);
let fill_inset = if selected {
metrics.color_palette_selected_swatch_inset
} else {
metrics.color_palette_swatch_inset
} + pressed_offset * 0.5;
let fill_rect = inset_rect(body, Insets::all(fill_inset));
let base_background = if selected {
palette.selection
} else {
palette.control
};
let hover_background = if hover_amount > 0.0 {
mix_color(
base_background,
palette.control_hover,
interaction.hover_blend * hover_amount,
)
} else {
base_background
};
let background = if press_amount > 0.0 {
mix_color(
hover_background,
palette.control_active,
interaction.pressed_blend * press_amount,
)
} else {
hover_background
};
if selected {
ctx.fill(Path::rounded_rect(body, radius), background);
} else if hover_amount > 0.0 || press_amount > 0.0 {
ctx.fill(Path::rounded_rect(rect, radius), background);
}
draw_rounded_checkerboard(
ctx,
fill_rect,
(radius - fill_inset).max(0.0),
metrics.color_palette_checker_size,
&theme,
);
ctx.fill(
Path::rounded_rect(fill_rect, (radius - fill_inset).max(0.0)),
swatch.color,
);
stroke_border(ctx, body, radius, ring_width, ring);
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.enabled.observe(|source| {
ctx.observe(source);
});
let enabled = self.enabled.get();
let mut node = SemanticsNode::new(
ctx.widget_id(),
SemanticsRole::GenericContainer,
ctx.bounds(),
);
node.name = Some(self.name.clone());
node.value = self.selected_value().map(SemanticsValue::Text);
node.state.focused = ctx.is_focused();
node.actions = vec![SemanticsAction::Focus, SemanticsAction::SetValue];
if !enabled {
node.state.disabled = true;
node.actions.clear();
}
ctx.push(node);
let selected = self.current_selected();
let theme = self.resolved_theme();
for (index, swatch) in self.swatches.iter().enumerate() {
let Some(rect) = self.swatch_rect(ctx.bounds(), index, &theme) else {
continue;
};
let mut node = SemanticsNode::new(
color_palette_swatch_id(ctx.widget_id(), index),
SemanticsRole::ColorSwatch,
rect,
);
node.parent = Some(ctx.widget_id());
node.name = Some(swatch.name.clone());
node.value = Some(SemanticsValue::Text(format_color(swatch.color)));
node.state.hovered = self.hovered == Some(index);
node.state.selected = selected == Some(index);
node.actions = vec![SemanticsAction::Activate];
if !enabled {
node.state.disabled = true;
node.actions.clear();
}
ctx.push(node);
}
}
fn accepts_focus(&self) -> bool {
self.enabled.get() && (!self.swatches.is_empty())
}
fn focus_changed(&mut self, ctx: &mut EventCtx, focused: bool) {
let theme = self.resolved_theme();
set_focus_animation_target(&mut self.focus_animation, focused as u8 as f32, &theme, ctx);
ctx.request_paint();
ctx.request_semantics();
}
}
fn color_palette_swatch_id(parent: WidgetId, index: usize) -> WidgetId {
const TAG: u64 = 7_u64 << 50;
const LOW_MASK: u64 = (1_u64 << 50) - 1;
WidgetId::new(
TAG | (parent
.get()
.wrapping_mul(509)
.wrapping_add(index as u64 + 1)
& LOW_MASK),
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BrushPreviewShape {
Round,
Square,
}
impl BrushPreviewShape {
pub const fn label(self) -> &'static str {
match self {
Self::Round => "Round",
Self::Square => "Square",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BrushPreviewSpec {
pub color: Color,
pub size: f32,
pub opacity: f32,
pub shape: BrushPreviewShape,
}
impl Default for BrushPreviewSpec {
fn default() -> Self {
Self::new(Color::BLACK, 12.0, 1.0, BrushPreviewShape::Round)
}
}
impl BrushPreviewSpec {
pub fn new(color: Color, size: f32, opacity: f32, shape: BrushPreviewShape) -> Self {
Self {
color,
size,
opacity,
shape,
}
}
}
pub struct BrushPreview {
theme: Box<DefaultTheme>,
theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
name: String,
kind: String,
spec: BrushPreviewSpec,
spec_reader: Option<Box<dyn Fn() -> BrushPreviewSpec>>,
size: Option<Size>,
}
impl BrushPreview {
pub fn new(name: impl Into<String>) -> Self {
let default_theme = DefaultTheme::default();
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
name: name.into(),
kind: "brush".to_string(),
spec: BrushPreviewSpec::new(
default_theme.palette.accent,
18.0,
1.0,
BrushPreviewShape::Round,
),
spec_reader: None,
size: None,
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn spec(mut self, spec: BrushPreviewSpec) -> Self {
self.spec = spec;
self.spec_reader = None;
self
}
pub fn spec_when<F>(mut self, reader: F) -> Self
where
F: Fn() -> BrushPreviewSpec + 'static,
{
self.spec_reader = Some(Box::new(reader));
self
}
pub fn kind(mut self, kind: impl Into<String>) -> Self {
self.kind = kind.into();
self
}
pub fn size(mut self, size: Size) -> Self {
self.size = Some(Size::new(size.width.max(80.0), size.height.max(44.0)));
self
}
fn current_spec(&self) -> BrushPreviewSpec {
let spec = self
.spec_reader
.as_ref()
.map(|reader| reader())
.unwrap_or(self.spec);
BrushPreviewSpec {
size: if spec.size.is_finite() {
spec.size.max(1.0)
} else {
1.0
},
opacity: if spec.opacity.is_finite() {
spec.opacity.clamp(0.0, 1.0)
} else {
1.0
},
..spec
}
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme)
}
fn value_text(kind: &str, spec: BrushPreviewSpec) -> String {
format!(
"{} {}, {:.0} px, {:.0}% opacity",
spec.shape.label(),
kind,
spec.size,
spec.opacity * 100.0
)
}
fn resolved_size(&self, theme: &DefaultTheme) -> Size {
self.size.unwrap_or(Size::new(
theme.metrics.brush_preview_min_width,
theme.metrics.brush_preview_min_height,
))
}
}
impl Widget for BrushPreview {
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let theme = self.resolved_theme();
constraints.clamp(self.resolved_size(&theme))
}
fn paint(&self, ctx: &mut PaintCtx) {
let bounds = ctx.bounds();
let theme = self.resolved_theme();
let palette = theme.palette;
let metrics = theme.metrics;
let spec = self.current_spec();
let content = inset_rect(bounds, metrics.brush_preview_padding);
let swatch_width = metrics.brush_preview_swatch_width.min(content.width());
let swatch = Rect::new(content.x(), content.y(), swatch_width, content.height());
let sample = Rect::new(
swatch.max_x() + metrics.brush_preview_swatch_gap,
content.y(),
(content.max_x() - swatch.max_x() - metrics.brush_preview_swatch_gap).max(0.0),
content.height(),
);
let preview_color = spec
.color
.with_alpha((spec.color.alpha * spec.opacity).clamp(0.0, 1.0));
draw_control_shape(
ctx,
bounds,
metrics.corner_radius,
physical_pixels(ctx, metrics.border_width.max(1.0)),
palette.surface_raised,
palette.border,
);
draw_checkerboard(ctx, swatch, metrics.brush_preview_checker_size, &theme);
stroke_border(
ctx,
swatch,
metrics.indicator_corner_radius,
physical_pixels(ctx, metrics.border_width.max(1.0)),
palette.border.with_alpha(0.70),
);
paint_brush_preview_mark(ctx, swatch, spec, preview_color);
let track = Rect::new(
sample.x(),
sample.y() + sample.height() * 0.44,
sample.width(),
sample.height() * 0.24,
);
draw_checkerboard(ctx, track, metrics.brush_preview_checker_size, &theme);
paint_brush_preview_stroke(ctx, track, spec, preview_color);
let text_token = theme.text.xs;
let text_height = metrics
.brush_preview_text_height
.max(text_token.line_height);
let text_slot = Rect::new(
sample.x(),
sample.max_y() - text_height,
sample.width(),
text_height,
);
let value_text = Self::value_text(&self.kind, spec);
let text_style = TextStyle {
font_size: text_token.size,
line_height: text_token.line_height,
color: palette.text.with_alpha(0.72),
..theme.body_text_style()
};
ctx.push_clip_rect(text_slot);
paint_text_line(ctx, text_slot, &value_text, &text_style, TextAlign::Start);
ctx.pop_clip();
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let spec = self.current_spec();
let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Image, ctx.bounds());
node.name = Some(self.name.clone());
node.value = Some(SemanticsValue::Text(Self::value_text(&self.kind, spec)));
ctx.push(node);
}
}
fn paint_brush_preview_mark(ctx: &mut PaintCtx, rect: Rect, spec: BrushPreviewSpec, color: Color) {
let diameter = spec
.size
.min(rect.width().min(rect.height()) - 10.0)
.max(6.0);
let center = Point::new(
rect.x() + rect.width() * 0.5,
rect.y() + rect.height() * 0.5,
);
let mark = Rect::new(
center.x - diameter * 0.5,
center.y - diameter * 0.5,
diameter,
diameter,
);
match spec.shape {
BrushPreviewShape::Round => ctx.fill(Path::circle(center, diameter * 0.5), color),
BrushPreviewShape::Square => ctx.fill(Path::rounded_rect(mark, 2.0), color),
}
}
fn paint_brush_preview_stroke(
ctx: &mut PaintCtx,
rect: Rect,
spec: BrushPreviewSpec,
color: Color,
) {
let dot_count = 7;
let diameter = (spec.size * 0.62).clamp(4.0, rect.height().max(4.0));
for index in 0..dot_count {
let t = if dot_count <= 1 {
0.0
} else {
index as f32 / (dot_count - 1) as f32
};
let center = Point::new(
rect.x() + diameter * 0.5 + t * (rect.width() - diameter).max(0.0),
rect.y() + rect.height() * 0.5,
);
let alpha = 0.35 + t * 0.65;
let color = color.with_alpha((color.alpha * alpha).clamp(0.0, 1.0));
match spec.shape {
BrushPreviewShape::Round => ctx.fill(Path::circle(center, diameter * 0.5), color),
BrushPreviewShape::Square => ctx.fill(
Path::rounded_rect(
Rect::new(
center.x - diameter * 0.5,
center.y - diameter * 0.5,
diameter,
diameter,
),
1.5,
),
color,
),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ActiveChannel {
ColorWheel,
SaturationValue,
Hue,
Saturation,
Value,
Alpha,
EncodingSelector,
EncodingOption(usize),
RgbRed,
RgbGreen,
RgbBlue,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SimpleColorPickerMode {
Hsl,
Hsv,
Rgb,
Oklch,
}
const OKLCH_MAX_CHROMA: f32 = 0.37;
const OKLCH_ACHROMATIC_CHROMA: f32 = 1.0e-4;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ColorSliderChannel {
Hue,
HsvSaturation,
HsvValue,
HslSaturation,
HslLightness,
Alpha,
Red,
Green,
Blue,
OklchLightness,
OklchChroma,
OklchHue,
}
impl ColorSliderChannel {
fn label(self) -> &'static str {
match self {
ColorSliderChannel::Hue | ColorSliderChannel::OklchHue => "H",
ColorSliderChannel::HsvSaturation | ColorSliderChannel::HslSaturation => "S",
ColorSliderChannel::HsvValue => "V",
ColorSliderChannel::HslLightness | ColorSliderChannel::OklchLightness => "L",
ColorSliderChannel::OklchChroma => "C",
ColorSliderChannel::Alpha => "A",
ColorSliderChannel::Red => "R",
ColorSliderChannel::Green => "G",
ColorSliderChannel::Blue => "B",
}
}
fn name(self) -> &'static str {
match self {
ColorSliderChannel::Hue | ColorSliderChannel::OklchHue => "Hue",
ColorSliderChannel::HsvSaturation | ColorSliderChannel::HslSaturation => "Saturation",
ColorSliderChannel::HsvValue => "Value",
ColorSliderChannel::HslLightness | ColorSliderChannel::OklchLightness => "Lightness",
ColorSliderChannel::OklchChroma => "Chroma",
ColorSliderChannel::Alpha => "Alpha",
ColorSliderChannel::Red => "Red",
ColorSliderChannel::Green => "Green",
ColorSliderChannel::Blue => "Blue",
}
}
}
#[derive(Debug, Clone)]
struct ColorSliderRow {
channel: ColorSliderChannel,
label: &'static str,
name: &'static str,
value: f32,
min: f32,
max: f32,
position: f32,
value_text: String,
}
impl ColorSliderRow {
fn new(
channel: ColorSliderChannel,
value: f32,
min: f32,
max: f32,
position: f32,
value_text: String,
) -> Self {
Self {
channel,
label: channel.label(),
name: channel.name(),
value,
min,
max,
position: position.clamp(0.0, 1.0),
value_text,
}
}
}
#[derive(Debug, Clone, Copy)]
struct ColorSliderValues {
color: Color,
editing_space: ColorSpace,
hue: f32,
hsv_saturation: f32,
hsv_value: f32,
hsl_saturation: f32,
hsl_lightness: f32,
alpha: f32,
red: f32,
green: f32,
blue: f32,
oklch: Oklch,
max_channel_value: f32,
hdr_capable: bool,
}
impl ColorSliderValues {
fn from_color(color: Color) -> Self {
let (hue, hsv_saturation, hsv_value) = rgb_to_hsv(color);
let max_channel_value = color_space_max_channel_value(color.space);
let (_, hsl_saturation, hsl_lightness) = rgb_to_hsl(color, max_channel_value);
Self {
color,
editing_space: color.space,
hue,
hsv_saturation,
hsv_value,
hsl_saturation,
hsl_lightness,
alpha: color.alpha,
red: color.red,
green: color.green,
blue: color.blue,
oklch: color.to_oklch(),
max_channel_value,
hdr_capable: color_space_hdr_capable(color.space),
}
}
fn from_hsv(
editing_space: ColorSpace,
hue: f32,
saturation: f32,
value: f32,
alpha: f32,
) -> Self {
let color = hsv_to_color(editing_space, hue, saturation, value, alpha);
let max_channel_value = color_space_max_channel_value(editing_space);
let (_, hsl_saturation, hsl_lightness) = rgb_to_hsl(color, max_channel_value);
Self {
color,
editing_space,
hue,
hsv_saturation: saturation,
hsv_value: value,
hsl_saturation,
hsl_lightness,
alpha,
red: color.red,
green: color.green,
blue: color.blue,
oklch: color.to_oklch(),
max_channel_value,
hdr_capable: color_space_hdr_capable(editing_space),
}
}
fn from_hsl(
editing_space: ColorSpace,
hue: f32,
saturation: f32,
lightness: f32,
alpha: f32,
) -> Self {
let max_channel_value = color_space_max_channel_value(editing_space);
let color = hsl_to_color(
editing_space,
hue,
saturation,
lightness,
alpha,
max_channel_value,
);
let (_, hsv_saturation, hsv_value) = rgb_to_hsv(color);
Self {
color,
editing_space,
hue,
hsv_saturation,
hsv_value,
hsl_saturation: saturation,
hsl_lightness: lightness,
alpha,
red: color.red,
green: color.green,
blue: color.blue,
oklch: color.to_oklch(),
max_channel_value,
hdr_capable: color_space_hdr_capable(editing_space),
}
}
fn from_rgb(editing_space: ColorSpace, red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self::from_color(Color::new(editing_space, red, green, blue, alpha))
}
fn from_oklch(editing_space: ColorSpace, oklch: Oklch) -> Self {
Self {
oklch,
..Self::from_color(oklch_to_color(editing_space, oklch))
}
}
}
fn oklch_to_color(space: ColorSpace, oklch: Oklch) -> Color {
match space {
ColorSpace::Srgb => oklch.to_srgb(),
ColorSpace::LinearSrgb => oklch.to_srgb().to_linear_srgb(),
ColorSpace::DisplayP3 => oklch.to_display_p3(),
ColorSpace::LinearDisplayP3 => oklch.to_linear_display_p3(),
}
}
fn oklch_gamut_chroma(space: ColorSpace, oklch: Oklch) -> f32 {
match space {
ColorSpace::Srgb | ColorSpace::LinearSrgb => oklch.max_srgb_chroma(),
ColorSpace::DisplayP3 | ColorSpace::LinearDisplayP3 => oklch.max_display_p3_chroma(),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ColorPickerSemanticPart {
CurrentColor,
PreviousColor,
ColorRange,
ColorRangeMenu,
ColorRangeOption(usize),
SaturationValue,
Hue,
Saturation,
Value,
Alpha,
Red,
Green,
Blue,
Hex,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct ColorPickerColors {
pub checkerboard_light: Option<Color>,
pub checkerboard_dark: Option<Color>,
pub chrome_border: Option<Color>,
pub plane_border: Option<Color>,
pub bar_border: Option<Color>,
pub marker_outer: Option<Color>,
pub marker_dark: Option<Color>,
pub marker_light: Option<Color>,
pub sdr_marker: Option<Color>,
pub hdr_divider: Option<Color>,
}
impl ColorPickerColors {
fn apply(self, mut theme: DefaultTheme) -> DefaultTheme {
let dark = theme.surfaces.dark;
theme.surfaces.checkerboard_light = self
.checkerboard_light
.unwrap_or(theme.palette.surface_raised);
theme.surfaces.checkerboard_dark = self
.checkerboard_dark
.unwrap_or(theme.palette.control_active);
theme.surfaces.color_picker_chrome_border =
self.chrome_border
.unwrap_or(
theme
.palette
.text
.with_alpha(if dark { 0.24 } else { 0.18 }),
);
theme.surfaces.color_picker_plane_border =
self.plane_border
.unwrap_or(
theme
.palette
.text
.with_alpha(if dark { 0.22 } else { 0.16 }),
);
theme.surfaces.color_picker_bar_border =
self.bar_border
.unwrap_or(
theme
.palette
.text
.with_alpha(if dark { 0.20 } else { 0.14 }),
);
theme.surfaces.color_picker_marker_outer = self.marker_outer.unwrap_or(if dark {
Color::BLACK.with_alpha(0.92)
} else {
Color::WHITE.with_alpha(0.96)
});
theme.surfaces.color_picker_marker_dark =
self.marker_dark.unwrap_or(Color::BLACK.with_alpha(0.88));
theme.surfaces.color_picker_marker_light =
self.marker_light.unwrap_or(Color::WHITE.with_alpha(0.92));
theme.surfaces.color_picker_sdr_marker = self
.sdr_marker
.unwrap_or(theme.palette.text.with_alpha(0.38));
theme.surfaces.color_picker_hdr_divider = self
.hdr_divider
.unwrap_or(theme.palette.warning.with_alpha(0.55));
theme
}
}
pub struct ColorPicker {
theme: Box<DefaultTheme>,
theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
enabled: Binding<bool>,
name: String,
editing_space: ColorSpace,
hue: f32,
saturation: f32,
value: f32,
alpha: f32,
previous_color: Color,
show_alpha: bool,
compact: bool,
encoding_dropdown_open: bool,
active: Option<ActiveChannel>,
focus_animation: AnimatedScalar,
color_reader: Option<Box<dyn Fn() -> Color>>,
on_change: Option<Box<dyn FnMut(Color)>>,
on_change_with_ctx: Option<Box<dyn FnMut(&mut EventCtx, Color)>>,
appearance: ColorPickerColors,
}
#[derive(Debug, Clone, Copy)]
struct ColorPickerResolvedState {
color: Color,
editing_space: ColorSpace,
hue: f32,
saturation: f32,
value: f32,
alpha: f32,
hdr_capable: bool,
max_channel_value: f32,
}
pub struct SimpleColorPicker {
theme: Box<DefaultTheme>,
theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
name: String,
mode: SimpleColorPickerMode,
editing_space: ColorSpace,
hue: f32,
hsv_saturation: f32,
hsv_value: f32,
hsl_saturation: f32,
hsl_lightness: f32,
red: f32,
green: f32,
blue: f32,
oklch: Oklch,
alpha: f32,
show_alpha: bool,
compact: bool,
active: Option<ColorSliderChannel>,
focus_animation: AnimatedScalar,
color_reader: Option<Box<dyn Fn() -> Color>>,
on_change: Option<Box<dyn FnMut(Color)>>,
on_change_with_ctx: Option<Box<dyn FnMut(&mut EventCtx, Color)>>,
appearance: ColorPickerColors,
}
impl ColorPicker {
const MAX_HDR_VALUE: f32 = 12.0;
const ENCODING_OPTIONS: [ColorSpace; 4] = [
ColorSpace::Srgb,
ColorSpace::LinearSrgb,
ColorSpace::DisplayP3,
ColorSpace::LinearDisplayP3,
];
pub fn new(name: impl Into<String>) -> Self {
let theme = DefaultTheme::default();
Self::from_color(name, theme.palette.accent)
}
pub fn from_color(name: impl Into<String>, color: Color) -> Self {
let (hue, saturation, value) = rgb_to_hsv(color);
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
enabled: Binding::new(true),
name: name.into(),
editing_space: color.space,
hue,
saturation,
value,
alpha: color.alpha,
previous_color: color,
show_alpha: true,
compact: false,
encoding_dropdown_open: false,
active: None,
focus_animation: AnimatedScalar::new(0.0),
color_reader: None,
on_change: None,
on_change_with_ctx: None,
appearance: ColorPickerColors::default(),
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled.set(enabled);
self
}
pub fn enabled_when<F>(mut self, enabled: F) -> Self
where
F: Fn() -> bool + 'static,
{
self.enabled.set_when(enabled);
self
}
pub fn enabled_from<O>(mut self, enabled: O) -> Self
where
O: Observable<bool> + 'static,
{
self.enabled.set_from(enabled);
self
}
pub fn is_enabled(&self) -> bool {
self.enabled.get()
}
fn paint_theme(&self) -> DefaultTheme {
let theme = self.resolved_theme();
if self.enabled.get() {
theme
} else {
theme.for_disabled_control()
}
}
pub fn show_alpha(mut self, show_alpha: bool) -> Self {
self.show_alpha = show_alpha;
self
}
pub fn compact(mut self, compact: bool) -> Self {
self.compact = compact;
self
}
pub fn colors(mut self, colors: ColorPickerColors) -> Self {
self.appearance = colors;
self
}
pub fn on_change<F>(mut self, on_change: F) -> Self
where
F: FnMut(Color) + 'static,
{
self.on_change = Some(Box::new(on_change));
self
}
pub fn on_change_with_ctx<F>(mut self, on_change: F) -> Self
where
F: FnMut(&mut EventCtx, Color) + 'static,
{
self.on_change_with_ctx = Some(Box::new(on_change));
self
}
pub fn color_when<F>(mut self, color: F) -> Self
where
F: Fn() -> Color + 'static,
{
self.color_reader = Some(Box::new(color));
self
}
pub fn current_color(&self) -> Color {
hsv_to_color(
self.editing_space,
self.hue,
self.saturation,
self.value,
self.alpha,
)
}
fn hdr_capable(&self) -> bool {
color_space_hdr_capable(self.editing_space)
}
fn max_channel_value(&self) -> f32 {
color_space_max_channel_value(self.editing_space)
}
fn external_color(&self) -> Option<Color> {
if self.active.is_none() && !self.encoding_dropdown_open {
self.color_reader.as_ref().map(|reader| reader())
} else {
None
}
}
fn resolved_theme(&self) -> DefaultTheme {
let theme = self
.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme);
self.appearance.apply(theme)
}
fn layout_metrics_for(&self, theme: &DefaultTheme) -> ControlMetrics {
if self.compact {
ControlMetrics::from_tokens(theme.spacing, theme.radius, ThemeDensity::Compact)
} else {
theme.metrics
}
}
fn layout_metrics(&self) -> ControlMetrics {
let theme = self.resolved_theme();
self.layout_metrics_for(&theme)
}
fn resolved_state(&self) -> ColorPickerResolvedState {
if let Some(color) = self.external_color() {
let (hue, saturation, value) = rgb_to_hsv(color);
let hdr_capable = color_space_hdr_capable(color.space);
return ColorPickerResolvedState {
color,
editing_space: color.space,
hue,
saturation,
value,
alpha: color.alpha,
hdr_capable,
max_channel_value: color_space_max_channel_value(color.space),
};
}
ColorPickerResolvedState {
color: self.current_color(),
editing_space: self.editing_space,
hue: self.hue,
saturation: self.saturation,
value: self.value,
alpha: self.alpha,
hdr_capable: self.hdr_capable(),
max_channel_value: self.max_channel_value(),
}
}
fn sync_external_color(&mut self) -> bool {
let Some(color) = self.external_color() else {
return false;
};
if !colors_close(self.current_color(), color) {
self.apply_color(color);
return true;
}
false
}
fn content_inset(&self) -> f32 {
self.layout_metrics().color_picker_content_inset
}
fn panel_gap(&self) -> f32 {
self.layout_metrics().color_picker_panel_gap
}
fn top_bar_height(&self) -> f32 {
self.layout_metrics().color_picker_top_bar_height
}
fn swatch_width(&self) -> f32 {
self.layout_metrics().color_picker_swatch_width
}
fn swatch_gap(&self) -> f32 {
self.layout_metrics().color_picker_swatch_gap
}
fn section_gap(&self) -> f32 {
self.layout_metrics().color_picker_section_gap
}
fn wheel_size(&self) -> f32 {
self.layout_metrics().color_picker_wheel_size
}
fn map_size(&self) -> f32 {
self.layout_metrics().color_picker_map_size
}
fn right_panel_width(&self) -> f32 {
self.layout_metrics().color_picker_right_panel_width
}
fn row_height(&self) -> f32 {
self.layout_metrics().color_picker_row_height
}
fn row_gap(&self) -> f32 {
self.layout_metrics().color_picker_row_gap
}
fn field_height(&self) -> f32 {
self.layout_metrics().color_picker_field_height
}
fn field_gap(&self) -> f32 {
self.layout_metrics().color_picker_field_gap
}
fn dropdown_gap(&self) -> f32 {
self.layout_metrics().color_picker_dropdown_gap
}
fn encoding_menu_row_height(&self) -> f32 {
self.layout_metrics().color_picker_encoding_menu_row_height
}
fn channel_slider_count(&self) -> usize {
if self.show_alpha { 4 } else { 3 }
}
fn desired_size(&self) -> Size {
let inset = self.content_inset();
let left_height = self.wheel_size()
+ self.section_gap()
+ self.channel_slider_count() as f32 * self.row_height()
+ self.channel_slider_count().saturating_sub(1) as f32 * self.row_gap();
let right_height = self.map_size()
+ self.section_gap()
+ 3.0 * self.row_height()
+ 2.0 * self.row_gap()
+ self.field_gap()
+ self.field_height();
Size::new(
inset * 2.0 + self.wheel_size() + self.panel_gap() + self.right_panel_width(),
inset * 2.0 + self.top_bar_height() + self.panel_gap() + left_height.max(right_height),
)
}
fn content_rect(&self, bounds: Rect) -> Rect {
inset_rect(bounds, Insets::all(self.content_inset()))
}
fn header_rect(&self, bounds: Rect) -> Rect {
let content = self.content_rect(bounds);
Rect::new(
content.x(),
content.y(),
content.width(),
self.top_bar_height(),
)
}
fn current_swatch_rect(&self, bounds: Rect) -> Rect {
let header = self.header_rect(bounds);
Rect::new(header.x(), header.y(), self.swatch_width(), header.height())
}
fn previous_swatch_rect(&self, bounds: Rect) -> Rect {
let current = self.current_swatch_rect(bounds);
Rect::new(
current.max_x() + self.swatch_gap(),
current.y(),
self.swatch_width(),
current.height(),
)
}
fn left_column_rect(&self, bounds: Rect) -> Rect {
let content = self.content_rect(bounds);
let y = self.header_rect(bounds).max_y() + self.panel_gap();
let width = self.wheel_size().min(content.width());
Rect::new(content.x(), y, width, content.max_y() - y)
}
fn right_column_rect(&self, bounds: Rect) -> Rect {
let content = self.content_rect(bounds);
let left = self.left_column_rect(bounds);
Rect::new(
left.max_x() + self.panel_gap(),
left.y(),
content.max_x() - (left.max_x() + self.panel_gap()),
content.max_y() - left.y(),
)
}
fn color_wheel_rect(&self, bounds: Rect) -> Rect {
let left = self.left_column_rect(bounds);
Rect::new(left.x(), left.y(), self.wheel_size(), self.wheel_size())
}
fn saturation_value_rect(&self, bounds: Rect) -> Rect {
let right = self.right_column_rect(bounds);
Rect::new(
right.x(),
right.y(),
right.width().min(self.map_size()),
self.map_size(),
)
}
fn left_slider_rect(&self, bounds: Rect, index: usize) -> Rect {
let wheel = self.color_wheel_rect(bounds);
let y = wheel.max_y()
+ self.section_gap()
+ index as f32 * (self.row_height() + self.row_gap());
Rect::new(wheel.x(), y, wheel.width(), self.row_height())
}
fn encoding_rect(&self, bounds: Rect) -> Rect {
let header = self.header_rect(bounds);
let selector_x = header.x()
+ self.swatch_width()
+ self.swatch_gap()
+ self.swatch_width()
+ self.panel_gap();
Rect::new(
selector_x,
header.y() + ((header.height() - self.field_height()) * 0.5),
(header.max_x() - selector_x).max(0.0),
self.field_height(),
)
}
fn encoding_menu_rect(&self, bounds: Rect) -> Rect {
let encoding = self.encoding_rect(bounds);
Rect::new(
encoding.x(),
encoding.max_y() + self.dropdown_gap(),
encoding.width(),
self.encoding_menu_row_height() * Self::ENCODING_OPTIONS.len() as f32,
)
}
fn encoding_option_rect(&self, bounds: Rect, index: usize) -> Rect {
let menu = self.encoding_menu_rect(bounds);
Rect::new(
menu.x(),
menu.y() + index as f32 * self.encoding_menu_row_height(),
menu.width(),
self.encoding_menu_row_height(),
)
}
fn encoding_option_at(&self, bounds: Rect, position: Point) -> Option<usize> {
if !self.encoding_dropdown_open || !self.encoding_menu_rect(bounds).contains(position) {
return None;
}
Self::ENCODING_OPTIONS
.iter()
.enumerate()
.find_map(|(index, _)| {
self.encoding_option_rect(bounds, index)
.contains(position)
.then_some(index)
})
}
fn rgb_row_rect(&self, bounds: Rect, index: usize) -> Rect {
let map = self.saturation_value_rect(bounds);
let y =
map.max_y() + self.section_gap() + index as f32 * (self.row_height() + self.row_gap());
Rect::new(map.x(), y, map.width(), self.row_height())
}
fn hex_rect(&self, bounds: Rect) -> Rect {
let last_row = self.rgb_row_rect(bounds, 2);
Rect::new(
last_row.x(),
last_row.max_y() + self.field_gap(),
last_row.width(),
self.field_height(),
)
}
fn update_from_position(
&mut self,
ctx: &mut EventCtx,
bounds: Rect,
channel: ActiveChannel,
position: Point,
) {
if self.apply_position(bounds, channel, position) {
self.emit_change(ctx);
}
}
fn apply_position(&mut self, bounds: Rect, channel: ActiveChannel, position: Point) -> bool {
match channel {
ActiveChannel::ColorWheel => {
let rect = self.color_wheel_rect(bounds);
let center = Point::new(
rect.x() + rect.width() * 0.5,
rect.y() + rect.height() * 0.5,
);
let dx = position.x - center.x;
let dy = position.y - center.y;
let angle = dy.atan2(dx);
self.hue = ((angle / std::f32::consts::TAU) + 1.0).rem_euclid(1.0);
true
}
ActiveChannel::SaturationValue => {
let rect = self.saturation_value_rect(bounds);
self.saturation = ((position.x - rect.x()) / rect.width()).clamp(0.0, 1.0);
let t = (1.0 - ((position.y - rect.y()) / rect.height())).clamp(0.0, 1.0);
self.value = if self.hdr_capable() {
hdr_slider_to_value(t)
} else {
t
};
true
}
ActiveChannel::Hue => {
self.hue = self.slider_position(self.left_slider_rect(bounds, 0), position);
true
}
ActiveChannel::Saturation => {
self.saturation = self.slider_position(self.left_slider_rect(bounds, 1), position);
true
}
ActiveChannel::Value => {
let t = self.slider_position(self.left_slider_rect(bounds, 2), position);
self.value = if self.hdr_capable() {
hdr_slider_to_value(t)
} else {
t
};
true
}
ActiveChannel::Alpha => {
self.alpha = self.slider_position(self.left_slider_rect(bounds, 3), position);
true
}
ActiveChannel::EncodingSelector | ActiveChannel::EncodingOption(_) => false,
ActiveChannel::RgbRed => {
self.apply_rgb_channel_from_position(bounds, 0, position);
true
}
ActiveChannel::RgbGreen => {
self.apply_rgb_channel_from_position(bounds, 1, position);
true
}
ActiveChannel::RgbBlue => {
self.apply_rgb_channel_from_position(bounds, 2, position);
true
}
}
}
fn emit_change(&mut self, ctx: &mut EventCtx) {
let color = self.current_color();
if let Some(on_change) = &mut self.on_change {
on_change(color);
}
if let Some(on_change) = &mut self.on_change_with_ctx {
on_change(ctx, color);
}
}
fn slider_position(&self, rect: Rect, position: Point) -> f32 {
ColorSliderRowSlots::new(rect, &self.resolved_theme()).position(position.x)
}
fn apply_color(&mut self, color: Color) {
self.editing_space = color.space;
let (hue, saturation, value) = rgb_to_hsv(color);
self.hue = hue;
self.saturation = saturation;
self.value = value;
self.alpha = color.alpha;
}
fn set_editing_space(&mut self, ctx: &mut EventCtx, next_space: ColorSpace) {
if self.editing_space == next_space {
return;
}
let current = self.current_color();
self.apply_color(Color::new(
next_space,
current.red,
current.green,
current.blue,
current.alpha,
));
self.emit_change(ctx);
}
fn apply_rgb_channel_from_position(
&mut self,
bounds: Rect,
channel_index: usize,
position: Point,
) {
let t = self.slider_position(self.rgb_row_rect(bounds, channel_index), position);
let mut channels = [
self.current_color().red,
self.current_color().green,
self.current_color().blue,
];
channels[channel_index] = self.max_channel_value() * t;
self.apply_color(Color::new(
self.editing_space,
channels[0],
channels[1],
channels[2],
self.alpha,
));
}
fn hit_channel(&self, bounds: Rect, position: Point) -> Option<ActiveChannel> {
if point_in_wheel_ring(self.color_wheel_rect(bounds), position) {
Some(ActiveChannel::ColorWheel)
} else if self.saturation_value_rect(bounds).contains(position) {
Some(ActiveChannel::SaturationValue)
} else if self.left_slider_rect(bounds, 0).contains(position) {
Some(ActiveChannel::Hue)
} else if self.left_slider_rect(bounds, 1).contains(position) {
Some(ActiveChannel::Saturation)
} else if self.left_slider_rect(bounds, 2).contains(position) {
Some(ActiveChannel::Value)
} else if self.show_alpha && self.left_slider_rect(bounds, 3).contains(position) {
Some(ActiveChannel::Alpha)
} else if self.encoding_rect(bounds).contains(position) {
Some(ActiveChannel::EncodingSelector)
} else if self.rgb_row_rect(bounds, 0).contains(position) {
Some(ActiveChannel::RgbRed)
} else if self.rgb_row_rect(bounds, 1).contains(position) {
Some(ActiveChannel::RgbGreen)
} else if self.rgb_row_rect(bounds, 2).contains(position) {
Some(ActiveChannel::RgbBlue)
} else {
None
}
}
fn color_semantics_text(&self, color: Color, hdr_capable: bool) -> String {
if hdr_capable && is_hdr_color(color) {
format!(
"R {:.3} G {:.3} B {:.3} A {:.3}",
color.red, color.green, color.blue, color.alpha
)
} else {
format_color(color)
}
}
fn push_color_swatch_semantics(
&self,
ctx: &mut SemanticsCtx,
part: ColorPickerSemanticPart,
name: &'static str,
bounds: Rect,
color: Color,
hdr_capable: bool,
) {
let mut node = color_picker_child_semantics_node(
ctx.widget_id(),
part,
SemanticsRole::ColorSwatch,
bounds,
name,
);
node.value = Some(SemanticsValue::Text(
self.color_semantics_text(color, hdr_capable),
));
ctx.push(node);
}
fn push_slider_semantics(
&self,
ctx: &mut SemanticsCtx,
part: ColorPickerSemanticPart,
name: &'static str,
bounds: Rect,
value: f32,
min: f32,
max: f32,
) {
let mut node = color_picker_child_semantics_node(
ctx.widget_id(),
part,
SemanticsRole::Slider,
bounds,
name,
);
node.value = Some(SemanticsValue::Range {
value: value as f64,
min: min as f64,
max: max as f64,
});
node.actions = vec![SemanticsAction::Focus, SemanticsAction::SetValue];
ctx.push(node);
}
fn push_component_semantics(&self, ctx: &mut SemanticsCtx, resolved: ColorPickerResolvedState) {
let bounds = ctx.bounds();
let current = resolved.color;
self.push_color_swatch_semantics(
ctx,
ColorPickerSemanticPart::CurrentColor,
"Current color",
self.current_swatch_rect(bounds),
current,
resolved.hdr_capable,
);
self.push_color_swatch_semantics(
ctx,
ColorPickerSemanticPart::PreviousColor,
"Previous color",
self.previous_swatch_rect(bounds),
self.previous_color,
color_space_hdr_capable(self.previous_color.space),
);
let mut range = color_picker_child_semantics_node(
ctx.widget_id(),
ColorPickerSemanticPart::ColorRange,
SemanticsRole::ComboBox,
self.encoding_rect(bounds),
"Color range",
);
range.value = Some(SemanticsValue::Text(
editing_space_label(resolved.editing_space).to_string(),
));
range.state.expanded = Some(self.encoding_dropdown_open);
range.actions = vec![
SemanticsAction::Focus,
if self.encoding_dropdown_open {
SemanticsAction::Collapse
} else {
SemanticsAction::Expand
},
SemanticsAction::SetValue,
];
ctx.push(range);
let mut saturation_value = color_picker_child_semantics_node(
ctx.widget_id(),
ColorPickerSemanticPart::SaturationValue,
SemanticsRole::Slider,
self.saturation_value_rect(bounds),
"Saturation and value",
);
saturation_value.description = Some(format!(
"Saturation {:.1}%, value {:.3}",
resolved.saturation * 100.0,
resolved.value
));
saturation_value.value = Some(SemanticsValue::Range {
value: resolved.value as f64,
min: 0.0,
max: resolved.max_channel_value as f64,
});
saturation_value.actions = vec![SemanticsAction::Focus, SemanticsAction::SetValue];
ctx.push(saturation_value);
self.push_slider_semantics(
ctx,
ColorPickerSemanticPart::Hue,
"Hue",
self.left_slider_rect(bounds, 0),
resolved.hue * 360.0,
0.0,
360.0,
);
self.push_slider_semantics(
ctx,
ColorPickerSemanticPart::Saturation,
"Saturation",
self.left_slider_rect(bounds, 1),
resolved.saturation * 100.0,
0.0,
100.0,
);
self.push_slider_semantics(
ctx,
ColorPickerSemanticPart::Value,
"Value",
self.left_slider_rect(bounds, 2),
resolved.value,
0.0,
resolved.max_channel_value,
);
if self.show_alpha {
self.push_slider_semantics(
ctx,
ColorPickerSemanticPart::Alpha,
"Alpha",
self.left_slider_rect(bounds, 3),
resolved.alpha * 100.0,
0.0,
100.0,
);
}
let rgb = current.to_array();
for (index, (part, name)) in [
(ColorPickerSemanticPart::Red, "Red"),
(ColorPickerSemanticPart::Green, "Green"),
(ColorPickerSemanticPart::Blue, "Blue"),
]
.into_iter()
.enumerate()
{
self.push_slider_semantics(
ctx,
part,
name,
self.rgb_row_rect(bounds, index),
rgb[index],
0.0,
resolved.max_channel_value,
);
}
let hex_name = if resolved.hdr_capable && is_hdr_color(current) {
"HDR hex unavailable".to_string()
} else {
format!("Hex color {}", format_color(current))
};
let hex = color_picker_child_semantics_node(
ctx.widget_id(),
ColorPickerSemanticPart::Hex,
SemanticsRole::Text,
self.hex_rect(bounds),
hex_name,
);
ctx.push(hex);
if self.encoding_dropdown_open {
let menu_id = color_picker_child_semantics_id(
ctx.widget_id(),
ColorPickerSemanticPart::ColorRangeMenu,
);
let mut menu = color_picker_child_semantics_node(
ctx.widget_id(),
ColorPickerSemanticPart::ColorRangeMenu,
SemanticsRole::Menu,
self.encoding_menu_rect(bounds),
"Color range options",
);
menu.state.expanded = Some(true);
ctx.push(menu);
for (index, space) in Self::ENCODING_OPTIONS.iter().copied().enumerate() {
let mut item = color_picker_child_semantics_node_with_parent(
ctx.widget_id(),
menu_id,
ColorPickerSemanticPart::ColorRangeOption(index),
SemanticsRole::MenuItem,
self.encoding_option_rect(bounds, index),
editing_space_label(space),
);
item.state.selected = space == resolved.editing_space;
item.actions = vec![SemanticsAction::Focus, SemanticsAction::Activate];
ctx.push(item);
}
}
}
}
fn color_picker_child_semantics_id(parent: WidgetId, part: ColorPickerSemanticPart) -> WidgetId {
let slot = match part {
ColorPickerSemanticPart::CurrentColor => 1,
ColorPickerSemanticPart::PreviousColor => 2,
ColorPickerSemanticPart::ColorRange => 3,
ColorPickerSemanticPart::ColorRangeMenu => 4,
ColorPickerSemanticPart::ColorRangeOption(index) => 32 + index as u64,
ColorPickerSemanticPart::SaturationValue => 5,
ColorPickerSemanticPart::Hue => 6,
ColorPickerSemanticPart::Saturation => 7,
ColorPickerSemanticPart::Value => 8,
ColorPickerSemanticPart::Alpha => 9,
ColorPickerSemanticPart::Red => 10,
ColorPickerSemanticPart::Green => 11,
ColorPickerSemanticPart::Blue => 12,
ColorPickerSemanticPart::Hex => 13,
};
const TAG: u64 = 3_u64 << 51;
const LOW_MASK: u64 = (1_u64 << 51) - 1;
WidgetId::new(TAG | (parent.get().wrapping_mul(397).wrapping_add(slot) & LOW_MASK))
}
fn color_picker_child_semantics_node(
parent: WidgetId,
part: ColorPickerSemanticPart,
role: SemanticsRole,
bounds: Rect,
name: impl Into<String>,
) -> SemanticsNode {
color_picker_child_semantics_node_with_parent(parent, parent, part, role, bounds, name)
}
fn color_picker_child_semantics_node_with_parent(
owner: WidgetId,
parent: WidgetId,
part: ColorPickerSemanticPart,
role: SemanticsRole,
bounds: Rect,
name: impl Into<String>,
) -> SemanticsNode {
let mut node = SemanticsNode::new(color_picker_child_semantics_id(owner, part), role, bounds);
node.parent = Some(parent);
node.name = Some(name.into());
node
}
impl SimpleColorPicker {
pub fn new(name: impl Into<String>) -> Self {
let theme = DefaultTheme::default();
Self::from_color(name, theme.palette.accent)
}
pub fn from_color(name: impl Into<String>, color: Color) -> Self {
let values = ColorSliderValues::from_color(color);
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
name: name.into(),
mode: SimpleColorPickerMode::Hsv,
editing_space: color.space,
hue: values.hue,
hsv_saturation: values.hsv_saturation,
hsv_value: values.hsv_value,
hsl_saturation: values.hsl_saturation,
hsl_lightness: values.hsl_lightness,
red: values.red,
green: values.green,
blue: values.blue,
oklch: values.oklch,
alpha: values.alpha,
show_alpha: true,
compact: false,
active: None,
focus_animation: AnimatedScalar::new(0.0),
color_reader: None,
on_change: None,
on_change_with_ctx: None,
appearance: ColorPickerColors::default(),
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn mode(mut self, mode: SimpleColorPickerMode) -> Self {
if self.mode != mode {
let current = self.current_color();
self.mode = mode;
self.apply_color(current);
}
self
}
pub fn color_space(mut self, color_space: ColorSpace) -> Self {
self.set_color_space(color_space);
self
}
pub fn show_alpha(mut self, show_alpha: bool) -> Self {
self.show_alpha = show_alpha;
self
}
pub fn compact(mut self, compact: bool) -> Self {
self.compact = compact;
self
}
pub fn colors(mut self, colors: ColorPickerColors) -> Self {
self.appearance = colors;
self
}
pub fn on_change<F>(mut self, on_change: F) -> Self
where
F: FnMut(Color) + 'static,
{
self.on_change = Some(Box::new(on_change));
self
}
pub fn on_change_with_ctx<F>(mut self, on_change: F) -> Self
where
F: FnMut(&mut EventCtx, Color) + 'static,
{
self.on_change_with_ctx = Some(Box::new(on_change));
self
}
pub fn color_when<F>(mut self, color: F) -> Self
where
F: Fn() -> Color + 'static,
{
self.color_reader = Some(Box::new(color));
self
}
pub fn current_color(&self) -> Color {
match self.mode {
SimpleColorPickerMode::Hsl => hsl_to_color(
self.editing_space,
self.hue,
self.hsl_saturation,
self.hsl_lightness,
self.alpha,
self.max_channel_value(),
),
SimpleColorPickerMode::Hsv => hsv_to_color(
self.editing_space,
self.hue,
self.hsv_saturation,
self.hsv_value,
self.alpha,
),
SimpleColorPickerMode::Rgb => Color::new(
self.editing_space,
self.red,
self.green,
self.blue,
self.alpha,
),
SimpleColorPickerMode::Oklch => {
oklch_to_color(self.editing_space, self.oklch_with_alpha())
}
}
}
fn oklch_with_alpha(&self) -> Oklch {
Oklch {
alpha: self.alpha,
..self.oklch
}
}
fn set_oklch_from(&mut self, oklch: Oklch) {
let hue = if oklch.chroma < OKLCH_ACHROMATIC_CHROMA {
self.oklch.hue
} else {
oklch.hue
};
self.oklch = Oklch { hue, ..oklch };
}
fn hdr_capable(&self) -> bool {
color_space_hdr_capable(self.editing_space)
}
fn max_channel_value(&self) -> f32 {
color_space_max_channel_value(self.editing_space)
}
fn external_color(&self) -> Option<Color> {
if self.active.is_none() {
self.color_reader.as_ref().map(|reader| reader())
} else {
None
}
}
fn resolved_theme(&self) -> DefaultTheme {
let theme = self
.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme);
self.appearance.apply(theme)
}
fn layout_metrics_for(&self, theme: &DefaultTheme) -> ControlMetrics {
if self.compact {
ControlMetrics::from_tokens(theme.spacing, theme.radius, ThemeDensity::Compact)
} else {
theme.metrics
}
}
fn layout_metrics(&self) -> ControlMetrics {
let theme = self.resolved_theme();
self.layout_metrics_for(&theme)
}
fn row_count(&self) -> usize {
if self.show_alpha { 4 } else { 3 }
}
fn content_inset(&self) -> f32 {
self.layout_metrics().color_picker_content_inset
}
fn row_height(&self) -> f32 {
self.layout_metrics().color_picker_row_height
}
fn row_gap(&self) -> f32 {
self.layout_metrics().color_picker_row_gap
}
fn desired_size(&self) -> Size {
let metrics = self.layout_metrics();
let inset = metrics.color_picker_content_inset;
let row_count = self.row_count();
let row_gap_count = row_count.saturating_sub(1);
Size::new(
inset * 2.0
+ metrics
.color_picker_right_panel_width
.max(metrics.color_picker_map_size),
inset * 2.0
+ row_count as f32 * metrics.color_picker_row_height
+ row_gap_count as f32 * metrics.color_picker_row_gap,
)
}
fn content_rect(&self, bounds: Rect) -> Rect {
inset_rect(bounds, Insets::all(self.content_inset()))
}
fn slider_rect(&self, bounds: Rect, index: usize) -> Rect {
let content = self.content_rect(bounds);
Rect::new(
content.x(),
content.y() + index as f32 * (self.row_height() + self.row_gap()),
content.width(),
self.row_height(),
)
}
fn slider_rect_for_channel(&self, bounds: Rect, channel: ColorSliderChannel) -> Option<Rect> {
self.current_rows()
.into_iter()
.position(|row| row.channel == channel)
.map(|index| self.slider_rect(bounds, index))
}
fn current_values(&self) -> ColorSliderValues {
match self.mode {
SimpleColorPickerMode::Hsl => ColorSliderValues::from_hsl(
self.editing_space,
self.hue,
self.hsl_saturation,
self.hsl_lightness,
self.alpha,
),
SimpleColorPickerMode::Hsv => ColorSliderValues::from_hsv(
self.editing_space,
self.hue,
self.hsv_saturation,
self.hsv_value,
self.alpha,
),
SimpleColorPickerMode::Rgb => ColorSliderValues::from_rgb(
self.editing_space,
self.red,
self.green,
self.blue,
self.alpha,
),
SimpleColorPickerMode::Oklch => {
ColorSliderValues::from_oklch(self.editing_space, self.oklch_with_alpha())
}
}
}
fn resolved_values(&self) -> ColorSliderValues {
match self.external_color() {
Some(color) if !colors_close(self.current_color(), color) => {
let mut values = ColorSliderValues::from_color(color);
if values.oklch.chroma < OKLCH_ACHROMATIC_CHROMA {
values.oklch.hue = self.oklch.hue;
}
values
}
_ => self.current_values(),
}
}
fn current_rows(&self) -> Vec<ColorSliderRow> {
color_slider_rows(self.mode, self.current_values(), self.show_alpha)
}
fn resolved_rows(&self, values: ColorSliderValues) -> Vec<ColorSliderRow> {
color_slider_rows(self.mode, values, self.show_alpha)
}
fn sync_external_color(&mut self) -> bool {
let Some(color) = self.external_color() else {
return false;
};
if !colors_close(self.current_color(), color) {
self.apply_color(color);
return true;
}
false
}
fn apply_color(&mut self, color: Color) {
let values = ColorSliderValues::from_color(color);
self.editing_space = color.space;
self.hue = values.hue;
self.hsv_saturation = values.hsv_saturation;
self.hsv_value = values.hsv_value;
self.hsl_saturation = values.hsl_saturation;
self.hsl_lightness = values.hsl_lightness;
self.red = values.red;
self.green = values.green;
self.blue = values.blue;
self.set_oklch_from(values.oklch);
self.alpha = values.alpha;
}
fn set_color_space(&mut self, next_space: ColorSpace) {
if self.editing_space == next_space {
return;
}
let current = self.current_color();
self.apply_color(Color::new(
next_space,
current.red,
current.green,
current.blue,
current.alpha,
));
}
fn emit_change(&mut self, ctx: &mut EventCtx) {
let color = self.current_color();
if let Some(on_change) = &mut self.on_change {
on_change(color);
}
if let Some(on_change) = &mut self.on_change_with_ctx {
on_change(ctx, color);
}
}
fn update_from_position(
&mut self,
ctx: &mut EventCtx,
bounds: Rect,
channel: ColorSliderChannel,
position: Point,
) {
let Some(rect) = self.slider_rect_for_channel(bounds, channel) else {
return;
};
let t = ColorSliderRowSlots::new(rect, &self.resolved_theme()).position(position.x);
let max_channel_value = self.max_channel_value();
match channel {
ColorSliderChannel::Hue => self.hue = t,
ColorSliderChannel::HsvSaturation => self.hsv_saturation = t,
ColorSliderChannel::HsvValue => {
self.hsv_value = if self.hdr_capable() {
hdr_slider_to_value(t)
} else {
t
};
}
ColorSliderChannel::HslSaturation => self.hsl_saturation = t,
ColorSliderChannel::HslLightness => self.hsl_lightness = max_channel_value * t,
ColorSliderChannel::Alpha => self.alpha = t,
ColorSliderChannel::Red => self.red = max_channel_value * t,
ColorSliderChannel::Green => self.green = max_channel_value * t,
ColorSliderChannel::Blue => self.blue = max_channel_value * t,
ColorSliderChannel::OklchLightness => self.oklch.lightness = t,
ColorSliderChannel::OklchChroma => self.oklch.chroma = t * OKLCH_MAX_CHROMA,
ColorSliderChannel::OklchHue => self.oklch.hue = t * 360.0,
}
self.refresh_inactive_channels();
self.emit_change(ctx);
}
fn refresh_inactive_channels(&mut self) {
let values = ColorSliderValues::from_color(self.current_color());
match self.mode {
SimpleColorPickerMode::Hsl => {
self.hsv_saturation = values.hsv_saturation;
self.hsv_value = values.hsv_value;
self.red = values.red;
self.green = values.green;
self.blue = values.blue;
self.set_oklch_from(values.oklch);
}
SimpleColorPickerMode::Hsv => {
self.hsl_saturation = values.hsl_saturation;
self.hsl_lightness = values.hsl_lightness;
self.red = values.red;
self.green = values.green;
self.blue = values.blue;
self.set_oklch_from(values.oklch);
}
SimpleColorPickerMode::Rgb => {
self.hue = values.hue;
self.hsv_saturation = values.hsv_saturation;
self.hsv_value = values.hsv_value;
self.hsl_saturation = values.hsl_saturation;
self.hsl_lightness = values.hsl_lightness;
self.set_oklch_from(values.oklch);
}
SimpleColorPickerMode::Oklch => {
self.hue = values.hue;
self.hsv_saturation = values.hsv_saturation;
self.hsv_value = values.hsv_value;
self.hsl_saturation = values.hsl_saturation;
self.hsl_lightness = values.hsl_lightness;
self.red = values.red;
self.green = values.green;
self.blue = values.blue;
}
}
}
fn hit_channel(&self, bounds: Rect, position: Point) -> Option<ColorSliderChannel> {
self.current_rows()
.into_iter()
.enumerate()
.find_map(|(index, row)| {
self.slider_rect(bounds, index)
.contains(position)
.then_some(row.channel)
})
}
fn color_semantics_text(&self, color: Color, hdr_capable: bool) -> String {
if hdr_capable && is_hdr_color(color) {
format!(
"R {:.3} G {:.3} B {:.3} A {:.3}",
color.red, color.green, color.blue, color.alpha
)
} else {
format_color(color)
}
}
fn mode_label(&self) -> &'static str {
match self.mode {
SimpleColorPickerMode::Hsl => "HSL",
SimpleColorPickerMode::Hsv => "HSV",
SimpleColorPickerMode::Rgb => "RGB",
SimpleColorPickerMode::Oklch => "OKLCH",
}
}
fn push_slider_semantics(&self, ctx: &mut SemanticsCtx, row: &ColorSliderRow, bounds: Rect) {
let mut node = simple_color_picker_child_semantics_node(
ctx.widget_id(),
row.channel,
SemanticsRole::Slider,
bounds,
row.name,
);
node.value = Some(SemanticsValue::Range {
value: row.value as f64,
min: row.min as f64,
max: row.max as f64,
});
node.actions = vec![SemanticsAction::Focus, SemanticsAction::SetValue];
ctx.push(node);
}
}
fn simple_color_picker_child_semantics_id(
parent: WidgetId,
channel: ColorSliderChannel,
) -> WidgetId {
let slot = match channel {
ColorSliderChannel::Hue => 1,
ColorSliderChannel::HsvSaturation => 2,
ColorSliderChannel::HsvValue => 3,
ColorSliderChannel::HslSaturation => 4,
ColorSliderChannel::HslLightness => 5,
ColorSliderChannel::Alpha => 6,
ColorSliderChannel::Red => 7,
ColorSliderChannel::Green => 8,
ColorSliderChannel::Blue => 9,
ColorSliderChannel::OklchLightness => 10,
ColorSliderChannel::OklchChroma => 11,
ColorSliderChannel::OklchHue => 12,
};
const TAG: u64 = 4_u64 << 51;
const LOW_MASK: u64 = (1_u64 << 51) - 1;
WidgetId::new(TAG | (parent.get().wrapping_mul(397).wrapping_add(slot) & LOW_MASK))
}
fn simple_color_picker_child_semantics_node(
parent: WidgetId,
channel: ColorSliderChannel,
role: SemanticsRole,
bounds: Rect,
name: impl Into<String>,
) -> SemanticsNode {
let mut node = SemanticsNode::new(
simple_color_picker_child_semantics_id(parent, channel),
role,
bounds,
);
node.parent = Some(parent);
node.name = Some(name.into());
node
}
impl Widget for SimpleColorPicker {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
if self.sync_external_color() {
ctx.request_paint();
ctx.request_semantics();
}
match event {
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
if let Some(active) = self.active {
self.update_from_position(ctx, ctx.bounds(), active, pointer.position);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Down
&& pointer.button == Some(PointerButton::Primary) =>
{
if let Some(active) = self.hit_channel(ctx.bounds(), pointer.position) {
self.active = Some(active);
self.update_from_position(ctx, ctx.bounds(), active, pointer.position);
ctx.request_focus();
ctx.request_pointer_capture(pointer.pointer_id);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Up
&& pointer.button == Some(PointerButton::Primary) =>
{
if self.active.take().is_some() {
ctx.release_pointer_capture(pointer.pointer_id);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
}
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Cancel => {
if self.active.take().is_some() {
ctx.release_pointer_capture(pointer.pointer_id);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
}
Event::Keyboard(key) if ctx.is_focused() && key.state == KeyState::Pressed => {
let unit_step = if key.modifiers.shift { 0.1 } else { 0.02 };
let value_step = if key.modifiers.shift { 0.5 } else { 0.1 };
let chroma_step = if key.modifiers.shift { 0.02 } else { 0.005 };
let lightness_step = if key.modifiers.shift { 0.05 } else { 0.01 };
match key.key.as_str() {
"ArrowLeft" => match self.mode {
SimpleColorPickerMode::Hsl => {
self.hsl_saturation = (self.hsl_saturation - unit_step).clamp(0.0, 1.0)
}
SimpleColorPickerMode::Hsv => {
self.hsv_saturation = (self.hsv_saturation - unit_step).clamp(0.0, 1.0)
}
SimpleColorPickerMode::Rgb => {
self.red = (self.red - value_step).clamp(0.0, self.max_channel_value())
}
SimpleColorPickerMode::Oklch => {
self.oklch.chroma =
(self.oklch.chroma - chroma_step).clamp(0.0, OKLCH_MAX_CHROMA)
}
},
"ArrowRight" => match self.mode {
SimpleColorPickerMode::Hsl => {
self.hsl_saturation = (self.hsl_saturation + unit_step).clamp(0.0, 1.0)
}
SimpleColorPickerMode::Hsv => {
self.hsv_saturation = (self.hsv_saturation + unit_step).clamp(0.0, 1.0)
}
SimpleColorPickerMode::Rgb => {
self.red = (self.red + value_step).clamp(0.0, self.max_channel_value())
}
SimpleColorPickerMode::Oklch => {
self.oklch.chroma =
(self.oklch.chroma + chroma_step).clamp(0.0, OKLCH_MAX_CHROMA)
}
},
"ArrowUp" => match self.mode {
SimpleColorPickerMode::Hsl => {
self.hsl_lightness = (self.hsl_lightness + value_step)
.clamp(0.0, self.max_channel_value())
}
SimpleColorPickerMode::Hsv => {
self.hsv_value =
(self.hsv_value + value_step).clamp(0.0, self.max_channel_value())
}
SimpleColorPickerMode::Rgb => {
self.green =
(self.green + value_step).clamp(0.0, self.max_channel_value())
}
SimpleColorPickerMode::Oklch => {
self.oklch.lightness =
(self.oklch.lightness + lightness_step).clamp(0.0, 1.0)
}
},
"ArrowDown" => match self.mode {
SimpleColorPickerMode::Hsl => {
self.hsl_lightness = (self.hsl_lightness - value_step)
.clamp(0.0, self.max_channel_value())
}
SimpleColorPickerMode::Hsv => {
self.hsv_value =
(self.hsv_value - value_step).clamp(0.0, self.max_channel_value())
}
SimpleColorPickerMode::Rgb => {
self.green =
(self.green - value_step).clamp(0.0, self.max_channel_value())
}
SimpleColorPickerMode::Oklch => {
self.oklch.lightness =
(self.oklch.lightness - lightness_step).clamp(0.0, 1.0)
}
},
_ => return,
}
self.refresh_inactive_channels();
self.emit_change(ctx);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
_ => {}
}
}
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.sync_external_color();
constraints.clamp(self.desired_size())
}
fn paint(&self, ctx: &mut PaintCtx) {
let theme = self.resolved_theme();
let values = self.resolved_values();
draw_surface(ctx, ctx.bounds(), &theme, self.focus_animation.get(ctx));
for (index, row) in self.resolved_rows(values).into_iter().enumerate() {
let rect = self.slider_rect(ctx.bounds(), index);
paint_color_slider_row(ctx, rect, &row, values, &theme);
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let values = self.resolved_values();
let mut node =
SemanticsNode::new(ctx.widget_id(), SemanticsRole::ColorPicker, ctx.bounds());
node.name = Some(self.name.clone());
node.description = Some(format!(
"{} sliders; {} editing space; {} range available",
self.mode_label(),
editing_space_label(values.editing_space),
if values.hdr_capable { "HDR" } else { "SDR" }
));
node.state.focused = ctx.is_focused();
node.value = Some(SemanticsValue::Text(
self.color_semantics_text(values.color, values.hdr_capable),
));
node.actions = vec![SemanticsAction::Focus, SemanticsAction::SetValue];
ctx.push(node);
for (index, row) in self.resolved_rows(values).iter().enumerate() {
self.push_slider_semantics(ctx, row, self.slider_rect(ctx.bounds(), index));
}
}
fn accepts_focus(&self) -> bool {
true
}
fn focus_changed(&mut self, ctx: &mut EventCtx, focused: bool) {
let theme = self.resolved_theme();
set_focus_animation_target(&mut self.focus_animation, focused as u8 as f32, &theme, ctx);
ctx.request_paint();
ctx.request_semantics();
}
}
impl Widget for ColorPicker {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
if !self.enabled.get() {
if let Event::Pointer(pointer) = event
&& pointer.kind == PointerEventKind::Up
{
ctx.release_pointer_capture(pointer.pointer_id);
}
return;
}
if self.sync_external_color() {
ctx.request_paint();
ctx.request_semantics();
}
match event {
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
if let Some(active) = self.active {
self.update_from_position(ctx, ctx.bounds(), active, pointer.position);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Down
&& pointer.button == Some(PointerButton::Primary) =>
{
if let Some(option) = self.encoding_option_at(ctx.bounds(), pointer.position) {
self.active = Some(ActiveChannel::EncodingOption(option));
ctx.request_focus();
ctx.request_pointer_capture(pointer.pointer_id);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
return;
}
let active = self.hit_channel(ctx.bounds(), pointer.position);
if let Some(active) = active {
self.active = Some(active);
if active == ActiveChannel::EncodingSelector {
self.encoding_dropdown_open = !self.encoding_dropdown_open;
} else {
self.encoding_dropdown_open = false;
self.update_from_position(ctx, ctx.bounds(), active, pointer.position);
}
ctx.request_focus();
ctx.request_pointer_capture(pointer.pointer_id);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
} else if self.encoding_dropdown_open {
self.encoding_dropdown_open = false;
ctx.request_paint();
ctx.request_semantics();
}
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Up
&& pointer.button == Some(PointerButton::Primary) =>
{
if let Some(active) = self.active.take() {
if let ActiveChannel::EncodingOption(index) = active {
if self
.encoding_option_rect(ctx.bounds(), index)
.contains(pointer.position)
{
self.set_editing_space(ctx, Self::ENCODING_OPTIONS[index]);
}
self.encoding_dropdown_open = false;
}
ctx.release_pointer_capture(pointer.pointer_id);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
}
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Cancel => {
if self.active.take().is_some() {
self.encoding_dropdown_open = false;
ctx.release_pointer_capture(pointer.pointer_id);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
}
Event::Keyboard(key) if ctx.is_focused() && key.state == KeyState::Pressed => {
let saturation_step = if key.modifiers.shift { 0.1 } else { 0.02 };
let value_step = if key.modifiers.shift { 0.5 } else { 0.1 };
match key.key.as_str() {
"ArrowLeft" => {
self.saturation = (self.saturation - saturation_step).clamp(0.0, 1.0)
}
"ArrowRight" => {
self.saturation = (self.saturation + saturation_step).clamp(0.0, 1.0)
}
"ArrowUp" => {
self.value = (self.value + value_step).clamp(0.0, self.max_channel_value())
}
"ArrowDown" => {
self.value = (self.value - value_step).clamp(0.0, self.max_channel_value())
}
_ => return,
}
self.emit_change(ctx);
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
_ => {}
}
}
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.sync_external_color();
constraints.clamp(self.desired_size())
}
fn paint(&self, ctx: &mut PaintCtx) {
self.enabled.observe(|source| {
ctx.observe(source);
});
let theme = self.paint_theme();
let resolved = self.resolved_state();
let current = resolved.color;
let wheel = self.color_wheel_rect(ctx.bounds());
let map = self.saturation_value_rect(ctx.bounds());
let encoding = self.encoding_rect(ctx.bounds());
draw_surface(ctx, ctx.bounds(), &theme, self.focus_animation.get(ctx));
paint_picker_header(
ctx,
self.current_swatch_rect(ctx.bounds()),
self.previous_swatch_rect(ctx.bounds()),
&theme,
self.previous_color,
current,
);
paint_dropdown(
ctx,
encoding,
&theme,
editing_space_label(resolved.editing_space),
);
paint_color_wheel(ctx, wheel, &theme);
paint_wheel_marker(ctx, wheel, resolved.hue, &theme);
paint_saturation_value_plane(
ctx,
map,
resolved.editing_space,
resolved.hue,
resolved.max_channel_value,
&theme,
);
let marker = Point::new(
map.x() + resolved.saturation * map.width(),
map.y()
+ (1.0
- if resolved.hdr_capable {
hdr_value_to_slider(resolved.value)
} else {
resolved.value.clamp(0.0, 1.0)
})
* map.height(),
);
paint_marker(ctx, marker, contrast_color(current, &theme), &theme);
let slider_values = ColorSliderValues::from_hsv(
resolved.editing_space,
resolved.hue,
resolved.saturation,
resolved.value,
resolved.alpha,
);
for (index, row) in
color_slider_rows(SimpleColorPickerMode::Hsv, slider_values, self.show_alpha)
.into_iter()
.enumerate()
{
let rect = self.left_slider_rect(ctx.bounds(), index);
paint_color_slider_row(ctx, rect, &row, slider_values, &theme);
}
for (index, row) in color_slider_rows(SimpleColorPickerMode::Rgb, slider_values, false)
.into_iter()
.enumerate()
{
let rect = self.rgb_row_rect(ctx.bounds(), index);
paint_color_slider_row(ctx, rect, &row, slider_values, &theme);
}
if resolved.hdr_capable && is_hdr_color(current) {
paint_disabled_field(
ctx,
self.hex_rect(ctx.bounds()),
&theme,
"HDR hex unavailable",
);
} else {
paint_hex_field(
ctx,
self.hex_rect(ctx.bounds()),
&theme,
&format_color(current),
);
}
if self.encoding_dropdown_open {
paint_encoding_menu(
ctx,
self.encoding_menu_rect(ctx.bounds()),
&theme,
resolved.editing_space,
self.encoding_menu_row_height(),
);
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.enabled.observe(|source| {
ctx.observe(source);
});
let enabled = self.enabled.get();
let resolved = self.resolved_state();
let current = resolved.color;
let mut node =
SemanticsNode::new(ctx.widget_id(), SemanticsRole::ColorPicker, ctx.bounds());
node.name = Some(self.name.clone());
node.description = Some(format!(
"{} editing space; {} range available",
editing_space_label(resolved.editing_space),
if resolved.hdr_capable { "HDR" } else { "SDR" }
));
node.state.focused = ctx.is_focused();
node.value = Some(SemanticsValue::Text(
self.color_semantics_text(current, resolved.hdr_capable),
));
node.actions = vec![SemanticsAction::Focus, SemanticsAction::SetValue];
if !enabled {
node.state.disabled = true;
node.actions.clear();
}
ctx.push(node);
self.push_component_semantics(ctx, resolved);
}
fn accepts_focus(&self) -> bool {
self.enabled.get()
}
fn focus_changed(&mut self, ctx: &mut EventCtx, focused: bool) {
let theme = self.resolved_theme();
set_focus_animation_target(&mut self.focus_animation, focused as u8 as f32, &theme, ctx);
ctx.request_paint();
ctx.request_semantics();
}
}
fn color_slider_rows(
mode: SimpleColorPickerMode,
values: ColorSliderValues,
show_alpha: bool,
) -> Vec<ColorSliderRow> {
let mut rows = Vec::with_capacity(if show_alpha { 4 } else { 3 });
match mode {
SimpleColorPickerMode::Hsl => {
rows.push(hue_slider_row(values.hue));
rows.push(percent_slider_row(
ColorSliderChannel::HslSaturation,
values.hsl_saturation,
));
rows.push(channel_slider_row(
ColorSliderChannel::HslLightness,
values.hsl_lightness,
values.max_channel_value,
));
}
SimpleColorPickerMode::Hsv => {
rows.push(hue_slider_row(values.hue));
rows.push(percent_slider_row(
ColorSliderChannel::HsvSaturation,
values.hsv_saturation,
));
rows.push(ColorSliderRow::new(
ColorSliderChannel::HsvValue,
values.hsv_value,
0.0,
values.max_channel_value,
if values.hdr_capable {
hdr_value_to_slider(values.hsv_value)
} else {
values.hsv_value.clamp(0.0, 1.0)
},
format!("{:.3}", values.hsv_value),
));
}
SimpleColorPickerMode::Rgb => {
rows.push(channel_slider_row(
ColorSliderChannel::Red,
values.red,
values.max_channel_value,
));
rows.push(channel_slider_row(
ColorSliderChannel::Green,
values.green,
values.max_channel_value,
));
rows.push(channel_slider_row(
ColorSliderChannel::Blue,
values.blue,
values.max_channel_value,
));
}
SimpleColorPickerMode::Oklch => {
let oklch = values.oklch;
rows.push(percent_slider_row(
ColorSliderChannel::OklchLightness,
oklch.lightness.clamp(0.0, 1.0),
));
rows.push(ColorSliderRow::new(
ColorSliderChannel::OklchChroma,
oklch.chroma,
0.0,
OKLCH_MAX_CHROMA,
oklch.chroma / OKLCH_MAX_CHROMA,
format!("{:.3}", oklch.chroma),
));
rows.push(ColorSliderRow::new(
ColorSliderChannel::OklchHue,
oklch.hue,
0.0,
360.0,
oklch.hue / 360.0,
format!("{:.1}", oklch.hue),
));
}
}
if show_alpha {
rows.push(percent_slider_row(ColorSliderChannel::Alpha, values.alpha));
}
rows
}
fn hue_slider_row(hue: f32) -> ColorSliderRow {
ColorSliderRow::new(
ColorSliderChannel::Hue,
hue * 360.0,
0.0,
360.0,
hue,
format!("{:.2}", hue * 360.0),
)
}
fn percent_slider_row(channel: ColorSliderChannel, value: f32) -> ColorSliderRow {
ColorSliderRow::new(
channel,
value * 100.0,
0.0,
100.0,
value,
format!("{:.1}", value * 100.0),
)
}
fn channel_slider_row(channel: ColorSliderChannel, value: f32, max_value: f32) -> ColorSliderRow {
ColorSliderRow::new(
channel,
value,
0.0,
max_value,
value / max_value.max(0.0001),
format!("{:.3}", value),
)
}
fn paint_color_slider_row(
ctx: &mut PaintCtx,
row_rect: Rect,
row: &ColorSliderRow,
values: ColorSliderValues,
theme: &DefaultTheme,
) {
let slots = ColorSliderRowSlots::new(row_rect, theme);
let rect = slots.track;
match row.channel {
ColorSliderChannel::Hue => paint_hue_bar(ctx, rect, theme),
ColorSliderChannel::HsvSaturation => paint_saturation_bar(
ctx,
rect,
values.editing_space,
values.hue,
values.hsv_value.max(1.0),
theme,
),
ColorSliderChannel::HsvValue => paint_value_bar(
ctx,
rect,
values.editing_space,
values.hue,
values.hsv_saturation,
values.hdr_capable,
theme,
),
ColorSliderChannel::HslSaturation => paint_hsl_saturation_bar(
ctx,
rect,
values.editing_space,
values.hue,
values.hsl_lightness,
values.max_channel_value,
theme,
),
ColorSliderChannel::HslLightness => paint_hsl_lightness_bar(
ctx,
rect,
values.editing_space,
values.hue,
values.hsl_saturation,
values.max_channel_value,
theme,
),
ColorSliderChannel::Alpha => {
draw_checkerboard(ctx, rect, theme.metrics.color_swatch_checker_size, theme);
paint_alpha_bar(ctx, rect, values.color, theme);
}
ColorSliderChannel::Red => {
paint_rgb_channel_bar(ctx, rect, values.color, 0, values.max_channel_value, theme)
}
ColorSliderChannel::Green => {
paint_rgb_channel_bar(ctx, rect, values.color, 1, values.max_channel_value, theme)
}
ColorSliderChannel::Blue => {
paint_rgb_channel_bar(ctx, rect, values.color, 2, values.max_channel_value, theme)
}
ColorSliderChannel::OklchLightness => paint_oklch_bar(
ctx,
rect,
values.editing_space,
|t| values.oklch.with_lightness(t),
theme,
),
ColorSliderChannel::OklchChroma => {
let space = values.editing_space;
paint_oklch_bar(
ctx,
rect,
space,
|t| values.oklch.with_chroma(t * OKLCH_MAX_CHROMA),
theme,
);
let gamut_edge = oklch_gamut_chroma(space, values.oklch) / OKLCH_MAX_CHROMA;
if gamut_edge < 0.995 {
ctx.fill_rect(
Rect::new(
rect.x() + rect.width() * gamut_edge,
rect.y(),
1.0,
rect.height(),
),
theme.surfaces.color_picker_hdr_divider,
);
}
}
ColorSliderChannel::OklchHue => paint_oklch_bar(
ctx,
rect,
values.editing_space,
|t| Oklch {
hue: t * 360.0,
..values.oklch
},
theme,
),
}
paint_labeled_row_text(
ctx,
row_rect,
row.label,
&row.value_text,
theme,
theme.palette.text_muted,
);
paint_marker(
ctx,
Point::new(
rect.x() + row.position * rect.width(),
rect.y() + rect.height() * 0.5,
),
theme.palette.border_focus,
theme,
);
}
fn paint_picker_header(
ctx: &mut PaintCtx,
current_rect: Rect,
previous_rect: Rect,
theme: &DefaultTheme,
previous: Color,
current: Color,
) {
let palette = theme.palette;
let metrics = theme.metrics;
let radius = metrics.indicator_corner_radius;
let current_rect = snap_to_pixels(ctx, current_rect);
let previous_rect = snap_to_pixels(ctx, previous_rect);
draw_checkerboard(ctx, current_rect, metrics.color_swatch_checker_size, theme);
draw_checkerboard(ctx, previous_rect, metrics.color_swatch_checker_size, theme);
ctx.fill(Path::rounded_rect(current_rect, radius), current);
ctx.fill(Path::rounded_rect(previous_rect, radius), previous);
stroke_border(
ctx,
current_rect,
radius,
physical_pixels(ctx, metrics.border_width.max(1.0)),
palette.border_focus,
);
stroke_border(
ctx,
previous_rect,
radius,
physical_pixels(ctx, metrics.border_width.max(1.0)),
palette.border,
);
}
fn paint_color_wheel(ctx: &mut PaintCtx, rect: Rect, theme: &DefaultTheme) {
let center = Point::new(
rect.x() + rect.width() * 0.5,
rect.y() + rect.height() * 0.5,
);
let outer = rect.width().min(rect.height()) * 0.5;
let inner = outer * 0.55;
ctx.draw_shader_rect(rect, WidgetShader::ColorWheel);
ctx.stroke(
Path::circle(center, outer - 1.0),
theme.surfaces.color_picker_chrome_border,
StrokeStyle::new(1.0),
);
ctx.stroke(
Path::circle(center, inner),
theme.surfaces.color_picker_chrome_border,
StrokeStyle::new(1.0),
);
}
fn paint_wheel_marker(ctx: &mut PaintCtx, rect: Rect, hue: f32, theme: &DefaultTheme) {
let center = Point::new(
rect.x() + rect.width() * 0.5,
rect.y() + rect.height() * 0.5,
);
let outer = rect.width().min(rect.height()) * 0.5;
let inner = outer * 0.55;
let radius = (outer + inner) * 0.5;
let angle = hue * std::f32::consts::TAU;
let point = Point::new(
center.x + angle.cos() * radius,
center.y + angle.sin() * radius,
);
paint_marker(ctx, point, theme.surfaces.color_picker_marker_dark, theme);
}
fn paint_saturation_value_plane(
ctx: &mut PaintCtx,
rect: Rect,
space: ColorSpace,
hue: f32,
max_value: f32,
theme: &DefaultTheme,
) {
ctx.draw_shader_rect(
rect,
WidgetShader::ColorPickerSaturationValuePlane {
color_space: space,
hue,
max_value,
},
);
stroke_border(
ctx,
rect,
0.0,
1.0,
theme.surfaces.color_picker_plane_border,
);
let sdr_marker = Rect::new(
rect.x(),
rect.y()
+ rect.height()
* (1.0
- if max_value <= 1.0001 {
1.0
} else {
hdr_value_to_slider(1.0)
}),
rect.width(),
1.0,
);
ctx.fill_rect(sdr_marker, theme.surfaces.color_picker_sdr_marker);
}
fn paint_hue_bar(ctx: &mut PaintCtx, rect: Rect, theme: &DefaultTheme) {
ctx.draw_shader_rect(rect, WidgetShader::ColorPickerHueBar);
paint_bar_border(ctx, rect, theme);
}
fn paint_saturation_bar(
ctx: &mut PaintCtx,
rect: Rect,
space: ColorSpace,
hue: f32,
value: f32,
theme: &DefaultTheme,
) {
ctx.draw_shader_rect(
rect,
WidgetShader::ColorPickerSaturationBar {
color_space: space,
hue,
value,
},
);
paint_bar_border(ctx, rect, theme);
}
fn paint_bar_border(ctx: &mut PaintCtx, rect: Rect, theme: &DefaultTheme) {
stroke_border(ctx, rect, 0.0, 1.0, theme.surfaces.color_picker_bar_border);
}
fn paint_value_bar(
ctx: &mut PaintCtx,
rect: Rect,
space: ColorSpace,
hue: f32,
saturation: f32,
hdr_capable: bool,
theme: &DefaultTheme,
) {
ctx.draw_shader_rect(
rect,
WidgetShader::ColorPickerValueBar {
color_space: space,
hue,
saturation,
max_value: if hdr_capable {
ColorPicker::MAX_HDR_VALUE
} else {
1.0
},
},
);
paint_bar_border(ctx, rect, theme);
if hdr_capable {
let divider_x = rect.x() + rect.width() * 0.5;
ctx.fill_rect(
Rect::new(divider_x, rect.y(), 1.0, rect.height()),
theme.surfaces.color_picker_hdr_divider,
);
}
}
fn paint_hsl_saturation_bar(
ctx: &mut PaintCtx,
rect: Rect,
space: ColorSpace,
hue: f32,
lightness: f32,
max_value: f32,
theme: &DefaultTheme,
) {
paint_linear_color_bar(
ctx,
rect,
vec![
GradientStop {
offset: 0.0,
color: hsl_to_color(space, hue, 0.0, lightness, 1.0, max_value),
},
GradientStop {
offset: 1.0,
color: hsl_to_color(space, hue, 1.0, lightness, 1.0, max_value),
},
],
theme,
);
}
fn paint_hsl_lightness_bar(
ctx: &mut PaintCtx,
rect: Rect,
space: ColorSpace,
hue: f32,
saturation: f32,
max_value: f32,
theme: &DefaultTheme,
) {
paint_linear_color_bar(
ctx,
rect,
vec![
GradientStop {
offset: 0.0,
color: hsl_to_color(space, hue, saturation, 0.0, 1.0, max_value),
},
GradientStop {
offset: 0.5,
color: hsl_to_color(space, hue, saturation, max_value * 0.5, 1.0, max_value),
},
GradientStop {
offset: 1.0,
color: hsl_to_color(space, hue, saturation, max_value, 1.0, max_value),
},
],
theme,
);
if max_value > 1.0001 {
let divider_x = rect.x() + rect.width() / max_value.max(1.0);
ctx.fill_rect(
Rect::new(divider_x, rect.y(), 1.0, rect.height()),
theme.surfaces.color_picker_hdr_divider,
);
}
}
fn paint_oklch_bar(
ctx: &mut PaintCtx,
rect: Rect,
space: ColorSpace,
sample: impl Fn(f32) -> Oklch,
theme: &DefaultTheme,
) {
const STOPS: usize = 16;
let stops = (0..=STOPS)
.map(|index| {
let offset = index as f32 / STOPS as f32;
GradientStop {
offset,
color: oklch_to_color(
space,
Oklch {
alpha: 1.0,
..sample(offset)
},
),
}
})
.collect();
paint_linear_color_bar(ctx, rect, stops, theme);
}
fn paint_linear_color_bar(
ctx: &mut PaintCtx,
rect: Rect,
stops: Vec<GradientStop>,
theme: &DefaultTheme,
) {
ctx.fill_rect(
rect,
Brush::LinearGradient {
start: rect.origin,
end: Point::new(rect.max_x(), rect.y()),
stops,
},
);
paint_bar_border(ctx, rect, theme);
}
fn paint_alpha_bar(ctx: &mut PaintCtx, rect: Rect, color: Color, theme: &DefaultTheme) {
ctx.draw_shader_rect(rect, WidgetShader::ColorPickerAlphaBar { color });
paint_bar_border(ctx, rect, theme);
}
fn paint_rgb_channel_bar(
ctx: &mut PaintCtx,
rect: Rect,
current: Color,
channel_index: usize,
max_value: f32,
theme: &DefaultTheme,
) {
ctx.draw_shader_rect(
rect,
WidgetShader::ColorPickerRgbChannelBar {
color: current,
channel: channel_index as u32,
max_value,
},
);
paint_bar_border(ctx, rect, theme);
}
#[derive(Debug, Clone, Copy)]
struct ColorSliderRowSlots {
label: Rect,
track: Rect,
value: Rect,
}
impl ColorSliderRowSlots {
const MARKER_CLEARANCE: f32 = 9.0;
fn new(rect: Rect, theme: &DefaultTheme) -> Self {
let label_width = (theme.metrics.icon_size + theme.spacing * 2.0).max(20.0);
let value_width = (rect.width() * 0.18).clamp(44.0, 64.0);
let label = Rect::new(
rect.x() + theme.spacing * 1.5,
rect.y(),
label_width,
rect.height(),
);
let value = Rect::new(
rect.max_x() - value_width - theme.spacing,
rect.y(),
value_width,
rect.height(),
);
let track_x = label.max_x() + Self::MARKER_CLEARANCE;
let track = Rect::new(
track_x,
rect.y(),
(value.x() - Self::MARKER_CLEARANCE - track_x).max(1.0),
rect.height(),
);
Self {
label,
track,
value,
}
}
fn position(&self, x: f32) -> f32 {
((x - self.track.x()) / self.track.width().max(1.0)).clamp(0.0, 1.0)
}
}
fn paint_labeled_row_text(
ctx: &mut PaintCtx,
rect: Rect,
label: &str,
value_text: &str,
theme: &DefaultTheme,
value_color: Color,
) {
let text = theme.text.xs;
let label_style = text_token_style(theme, text, theme.palette.text_muted);
let value_style = numeric_text_style(text_token_style(theme, text, value_color));
let slots = ColorSliderRowSlots::new(rect, theme);
let label_slot = slots.label;
let value_slot = slots.value;
ctx.push_clip_rect(label_slot);
paint_text_line(ctx, label_slot, label, &label_style, TextAlign::Start);
ctx.pop_clip();
ctx.push_clip_rect(value_slot);
paint_text_line(ctx, value_slot, value_text, &value_style, TextAlign::End);
ctx.pop_clip();
}
fn paint_dropdown(ctx: &mut PaintCtx, rect: Rect, theme: &DefaultTheme, label: &str) {
let metrics = theme.metrics;
let radius = metrics.corner_radius;
let text = theme.text.xs;
let padding = metrics.text_input_padding;
let style = text_token_style(theme, text, theme.palette.text);
let text_slot = Rect::new(
rect.x() + padding.left.max(theme.spacing * 2.0),
rect.y(),
rect.width() - padding.left.max(theme.spacing * 2.0) - metrics.icon_size - theme.spacing,
rect.height(),
);
draw_control_shape(
ctx,
rect,
radius,
physical_pixels(ctx, metrics.border_width.max(1.0)),
theme.palette.control,
theme.palette.border_focus,
);
ctx.push_clip_rect(text_slot);
paint_text_line(ctx, text_slot, label, &style, TextAlign::Start);
ctx.pop_clip();
ctx.stroke(
dropdown_chevron_path(rect),
theme.palette.placeholder,
StrokeStyle::new(1.4),
);
}
fn paint_encoding_menu(
ctx: &mut PaintCtx,
rect: Rect,
theme: &DefaultTheme,
selected: ColorSpace,
row_height: f32,
) {
let metrics = theme.metrics;
let radius = metrics.corner_radius;
let text = theme.text.xs;
draw_control_shape(
ctx,
rect,
radius,
physical_pixels(ctx, metrics.border_width.max(1.0)),
theme.palette.surface_raised,
theme.palette.border_focus,
);
for (index, space) in ColorPicker::ENCODING_OPTIONS.iter().copied().enumerate() {
let row = Rect::new(
rect.x(),
rect.y() + index as f32 * row_height,
rect.width(),
row_height,
);
if space == selected {
let selected_rect = inset_rect(row, metrics.menu_item_padding);
ctx.fill(
Path::rounded_rect(
selected_rect,
(radius - metrics.menu_item_padding.top).max(0.0),
),
theme.palette.selection,
);
ctx.fill_rect(
Rect::new(
selected_rect.x(),
selected_rect.y() + theme.spacing,
theme.interaction.active_indicator_thickness,
(selected_rect.height() - theme.spacing * 2.0).max(0.0),
),
theme.palette.selection_border,
);
}
let label = editing_space_label(space);
let style = text_token_style(theme, text, theme.palette.text);
let text_slot = Rect::new(
row.x() + metrics.menu_item_padding.left + theme.spacing * 1.5,
row.y(),
row.width()
- metrics.menu_item_padding.left
- metrics.menu_item_padding.right
- theme.spacing * 2.0,
row.height(),
);
ctx.push_clip_rect(text_slot);
paint_text_line(ctx, text_slot, label, &style, TextAlign::Start);
ctx.pop_clip();
}
}
fn dropdown_chevron_path(rect: Rect) -> Path {
let center = Point::new(rect.max_x() - 14.0, rect.y() + rect.height() * 0.5);
let half_width = 4.0;
let half_height = 2.5;
let mut path = PathBuilder::new();
path.move_to(Point::new(center.x - half_width, center.y - half_height));
path.line_to(Point::new(center.x, center.y + half_height));
path.line_to(Point::new(center.x + half_width, center.y - half_height));
path.build()
}
fn paint_hex_field(ctx: &mut PaintCtx, rect: Rect, theme: &DefaultTheme, value: &str) {
let metrics = theme.metrics;
let text = theme.text.xs;
let padding = metrics.text_input_padding;
let style = text_token_style(theme, text, theme.palette.text);
let text_slot = Rect::new(
rect.x() + padding.left.max(theme.spacing * 2.0),
rect.y(),
rect.width() - (padding.left + padding.right).max(theme.spacing * 4.0),
rect.height(),
);
draw_control_shape(
ctx,
rect,
metrics.corner_radius,
physical_pixels(ctx, metrics.border_width.max(1.0)),
theme.palette.control,
theme.palette.border,
);
ctx.push_clip_rect(text_slot);
paint_text_line(ctx, text_slot, value, &style, TextAlign::Start);
ctx.pop_clip();
}
fn paint_disabled_field(ctx: &mut PaintCtx, rect: Rect, theme: &DefaultTheme, value: &str) {
let metrics = theme.metrics;
let text = theme.text.xs;
let padding = metrics.text_input_padding;
let style = text_token_style(theme, text, theme.palette.placeholder);
let text_slot = Rect::new(
rect.x() + padding.left.max(theme.spacing * 2.0),
rect.y(),
rect.width() - (padding.left + padding.right).max(theme.spacing * 4.0),
rect.height(),
);
draw_control_shape(
ctx,
rect,
metrics.corner_radius,
physical_pixels(ctx, metrics.border_width.max(1.0)),
mix_color(theme.palette.control, theme.palette.surface, 0.5)
.with_alpha(theme.interaction.disabled_opacity),
theme.palette.border,
);
ctx.push_clip_rect(text_slot);
paint_text_line(ctx, text_slot, value, &style, TextAlign::Start);
ctx.pop_clip();
}
fn point_in_wheel_ring(rect: Rect, position: Point) -> bool {
let center = Point::new(
rect.x() + rect.width() * 0.5,
rect.y() + rect.height() * 0.5,
);
let dx = position.x - center.x;
let dy = position.y - center.y;
let distance = (dx * dx + dy * dy).sqrt();
let outer = rect.width().min(rect.height()) * 0.5;
let inner = outer * 0.55;
distance >= inner && distance <= outer
}
fn editing_space_label(space: ColorSpace) -> &'static str {
match space {
ColorSpace::Srgb => "sRGB",
ColorSpace::LinearSrgb => "BT709 Linear",
ColorSpace::DisplayP3 => "Display P3",
ColorSpace::LinearDisplayP3 => "Display P3 Linear",
}
}
fn hdr_value_to_slider(value: f32) -> f32 {
let value = value.clamp(0.0, ColorPicker::MAX_HDR_VALUE);
if value <= 1.0 {
value * 0.5
} else {
0.5 + (value.ln() / ColorPicker::MAX_HDR_VALUE.ln()) * 0.5
}
}
fn hdr_slider_to_value(t: f32) -> f32 {
let t = t.clamp(0.0, 1.0);
if t <= 0.5 {
t / 0.5
} else {
ColorPicker::MAX_HDR_VALUE.powf((t - 0.5) / 0.5)
}
}
fn is_hdr_color(color: Color) -> bool {
color.red > 1.0 || color.green > 1.0 || color.blue > 1.0
}
type AnimatedScalar = Progress;
fn set_animation_target(
animation: &mut AnimatedScalar,
target: f32,
duration: f64,
easing: crate::Easing,
ctx: &mut EventCtx,
) -> bool {
animation.animate(target, crate::AnimationSpec::tween(duration, easing), ctx)
}
fn set_hover_animation_target(
animation: &mut AnimatedScalar,
target: f32,
theme: &DefaultTheme,
ctx: &mut EventCtx,
) -> bool {
set_animation_target(
animation,
target,
theme.motion.hover_duration(),
theme.motion.hover_easing(),
ctx,
)
}
fn set_press_animation_target(
animation: &mut AnimatedScalar,
target: f32,
theme: &DefaultTheme,
ctx: &mut EventCtx,
) -> bool {
set_animation_target(
animation,
target,
theme.motion.press_duration(),
theme.motion.press_easing(),
ctx,
)
}
fn set_focus_animation_target(
animation: &mut AnimatedScalar,
target: f32,
theme: &DefaultTheme,
ctx: &mut EventCtx,
) -> bool {
set_animation_target(
animation,
target,
theme.motion.focus_duration(),
theme.motion.focus_easing(),
ctx,
)
}
fn fit_rect(bounds: Rect, source: Size, fit: ImageFit) -> Rect {
if bounds.is_empty() || source.is_empty() {
return bounds;
}
let scale_x = bounds.width() / source.width.max(1.0);
let scale_y = bounds.height() / source.height.max(1.0);
let scale = match fit {
ImageFit::Fill => None,
ImageFit::Contain => Some(scale_x.min(scale_y)),
ImageFit::Cover => Some(scale_x.max(scale_y)),
ImageFit::None => Some(1.0),
};
let size = if let Some(scale) = scale {
Size::new(source.width * scale, source.height * scale)
} else {
bounds.size
};
Rect::new(
bounds.x() + ((bounds.width() - size.width) * 0.5),
bounds.y() + ((bounds.height() - size.height) * 0.5),
size.width,
size.height,
)
}
fn paint_marker(ctx: &mut PaintCtx, center: Point, color: Color, theme: &DefaultTheme) {
ctx.stroke(
Path::circle(center, 6.5),
theme.surfaces.color_picker_marker_outer,
StrokeStyle::new(2.0),
);
ctx.stroke(Path::circle(center, 5.0), color, StrokeStyle::new(1.5));
}
fn draw_surface(ctx: &mut PaintCtx, rect: Rect, theme: &DefaultTheme, focus_progress: f32) {
let focus_progress = focus_progress.clamp(0.0, 1.0);
draw_control_shape(
ctx,
rect,
theme.metrics.corner_radius,
physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
theme.palette.surface,
mix_color(
theme.palette.border,
theme.palette.border_focus,
focus_progress,
),
);
if focus_progress > AnimatedScalar::EPSILON {
draw_focus_ring(
ctx,
rect,
theme.metrics.corner_radius,
theme.metrics,
theme
.palette
.focus_ring
.with_alpha(theme.palette.focus_ring.alpha * focus_progress),
);
}
}
fn draw_checkerboard(ctx: &mut PaintCtx, rect: Rect, cell_size: f32, theme: &DefaultTheme) {
let light = theme.surfaces.checkerboard_light;
let dark = theme.surfaces.checkerboard_dark;
let cell_size = cell_size.max(2.0);
let cols = (rect.width() / cell_size).ceil() as usize;
let rows = (rect.height() / cell_size).ceil() as usize;
ctx.push_clip_rect(rect);
for row in 0..rows {
for col in 0..cols {
let cell = Rect::new(
rect.x() + col as f32 * cell_size,
rect.y() + row as f32 * cell_size,
cell_size,
cell_size,
);
ctx.fill_rect(cell, if (row + col) % 2 == 0 { light } else { dark });
}
}
ctx.pop_clip();
}
fn draw_rounded_checkerboard(
ctx: &mut PaintCtx,
rect: Rect,
radius: f32,
cell_size: f32,
theme: &DefaultTheme,
) {
ctx.push_clip(Path::rounded_rect(rect, radius));
draw_checkerboard(ctx, rect, cell_size, theme);
ctx.pop_clip();
}
fn format_color(color: Color) -> String {
format!(
"#{:02X}{:02X}{:02X}{:02X}",
(color.red.clamp(0.0, 1.0) * 255.0).round() as u8,
(color.green.clamp(0.0, 1.0) * 255.0).round() as u8,
(color.blue.clamp(0.0, 1.0) * 255.0).round() as u8,
(color.alpha.clamp(0.0, 1.0) * 255.0).round() as u8,
)
}
fn color_space_hdr_capable(space: ColorSpace) -> bool {
space.is_linear() || matches!(space, ColorSpace::DisplayP3)
}
fn color_space_max_channel_value(space: ColorSpace) -> f32 {
if color_space_hdr_capable(space) {
ColorPicker::MAX_HDR_VALUE
} else {
1.0
}
}
fn colors_close(left: Color, right: Color) -> bool {
left.space == right.space
&& (left.red - right.red).abs() < 0.0001
&& (left.green - right.green).abs() < 0.0001
&& (left.blue - right.blue).abs() < 0.0001
&& (left.alpha - right.alpha).abs() < 0.0001
}
fn contrast_color(color: Color, theme: &DefaultTheme) -> Color {
if perceived_luminance(color) > 0.55 {
theme.surfaces.color_picker_marker_dark
} else {
theme.surfaces.color_picker_marker_light
}
}
fn perceived_luminance(color: Color) -> f32 {
(0.299 * color.red) + (0.587 * color.green) + (0.114 * color.blue)
}
fn hsv_to_color(space: ColorSpace, hue: f32, saturation: f32, value: f32, alpha: f32) -> Color {
let hue = hue.rem_euclid(1.0) * 6.0;
let sector = hue.floor();
let fraction = hue - sector;
let p = value * (1.0 - saturation);
let q = value * (1.0 - fraction * saturation);
let t = value * (1.0 - (1.0 - fraction) * saturation);
let (red, green, blue) = match sector as i32 {
0 => (value, t, p),
1 => (q, value, p),
2 => (p, value, t),
3 => (p, q, value),
4 => (t, p, value),
_ => (value, p, q),
};
Color::new(space, red, green, blue, alpha)
}
#[cfg(test)]
fn hsv_to_rgb(hue: f32, saturation: f32, value: f32, alpha: f32) -> Color {
hsv_to_color(ColorSpace::Srgb, hue, saturation, value, alpha)
}
fn rgb_to_hsv(color: Color) -> (f32, f32, f32) {
let max = color.red.max(color.green).max(color.blue);
let min = color.red.min(color.green).min(color.blue);
let delta = max - min;
let hue = if delta <= f32::EPSILON {
0.0
} else if (max - color.red).abs() <= f32::EPSILON {
(((color.green - color.blue) / delta).rem_euclid(6.0)) / 6.0
} else if (max - color.green).abs() <= f32::EPSILON {
(((color.blue - color.red) / delta) + 2.0) / 6.0
} else {
(((color.red - color.green) / delta) + 4.0) / 6.0
};
let saturation = if max <= f32::EPSILON {
0.0
} else {
delta / max
};
(hue, saturation, max)
}
fn hsl_to_color(
space: ColorSpace,
hue: f32,
saturation: f32,
lightness: f32,
alpha: f32,
max_value: f32,
) -> Color {
let max_value = max_value.max(0.0001);
let lightness = (lightness / max_value).clamp(0.0, 1.0);
let saturation = saturation.clamp(0.0, 1.0);
let q = if lightness < 0.5 {
lightness * (1.0 + saturation)
} else {
lightness + saturation - lightness * saturation
};
let p = 2.0 * lightness - q;
let red = hue_to_rgb(p, q, hue + (1.0 / 3.0)) * max_value;
let green = hue_to_rgb(p, q, hue) * max_value;
let blue = hue_to_rgb(p, q, hue - (1.0 / 3.0)) * max_value;
Color::new(space, red, green, blue, alpha)
}
fn hue_to_rgb(p: f32, q: f32, hue: f32) -> f32 {
let hue = hue.rem_euclid(1.0);
if hue < 1.0 / 6.0 {
p + (q - p) * 6.0 * hue
} else if hue < 1.0 / 2.0 {
q
} else if hue < 2.0 / 3.0 {
p + (q - p) * ((2.0 / 3.0) - hue) * 6.0
} else {
p
}
}
fn rgb_to_hsl(color: Color, max_value: f32) -> (f32, f32, f32) {
let max_value = max_value.max(0.0001);
let red = (color.red / max_value).clamp(0.0, 1.0);
let green = (color.green / max_value).clamp(0.0, 1.0);
let blue = (color.blue / max_value).clamp(0.0, 1.0);
let max = red.max(green).max(blue);
let min = red.min(green).min(blue);
let lightness = (max + min) * 0.5;
let delta = max - min;
if delta <= f32::EPSILON {
return (0.0, 0.0, lightness * max_value);
}
let hue = if (max - red).abs() <= f32::EPSILON {
(((green - blue) / delta).rem_euclid(6.0)) / 6.0
} else if (max - green).abs() <= f32::EPSILON {
(((blue - red) / delta) + 2.0) / 6.0
} else {
(((red - green) / delta) + 4.0) / 6.0
};
let saturation =
(delta / (1.0 - (2.0 * lightness - 1.0).abs()).max(f32::EPSILON)).clamp(0.0, 1.0);
(hue, saturation, lightness * max_value)
}
fn numeric_text_style(mut style: TextStyle) -> TextStyle {
style.features.enable(FontFeature::TABULAR_FIGURES);
style
}
fn text_token_style(theme: &DefaultTheme, token: ThemeTextToken, color: Color) -> TextStyle {
TextStyle {
font_size: token.size.max(1.0),
line_height: token.line_height.max(1.0),
color,
..theme.body_text_style()
}
}
#[cfg(test)]
mod tests {
use std::{cell::RefCell, collections::BTreeSet, rc::Rc};
use crate::draw::rect_center;
use super::{
ActiveChannel, BrushPreview, BrushPreviewShape, BrushPreviewSpec, ColorPalette,
ColorPaletteSwatch, ColorPicker, ColorPickerColors, ColorPickerSemanticPart, ColorSwatch,
Image, OKLCH_MAX_CHROMA, SignalMeter, SimpleColorPicker, SimpleColorPickerMode,
color_picker_child_semantics_id, format_color, hsl_to_color, hsv_to_rgb, rgb_to_hsl,
rgb_to_hsv, signal_meter_bar_layout,
};
use crate::{DefaultTheme, SemanticTone, ThemeTextToken};
use sui_core::{
Color, ColorSpace, Event, ImageHandle, Oklch, Point, PointerButton, PointerButtons,
PointerEvent, PointerEventKind, Rect, Result, SemanticsAction, SemanticsNode,
SemanticsRole, SemanticsValue, Size, Vector, WidgetId, WindowId,
};
use sui_layout::Padding as Insets;
use sui_runtime::{Application, Runtime, Widget, WindowBuilder};
use sui_scene::{Brush, GradientStop, RegisteredImage, SceneCommand, WidgetShader};
use sui_text::{FontFeature, FontRegistry, TextSystem};
fn build_runtime<W>(root: W) -> (Runtime, sui_core::WindowId)
where
W: Widget + 'static,
{
let runtime = Application::new()
.window(WindowBuilder::new().title("Media widgets").root(root))
.build()
.unwrap();
let window_id = runtime.window_ids()[0];
(runtime, window_id)
}
fn primary_pointer(kind: PointerEventKind, position: Point, pressed: bool) -> Event {
let mut buttons = PointerButtons::NONE;
if pressed {
buttons.insert(PointerButton::Primary);
}
Event::Pointer(PointerEvent {
pointer_id: 1,
kind,
position,
delta: Vector::ZERO,
scroll_delta: None,
button: Some(PointerButton::Primary),
buttons,
modifiers: sui_core::Modifiers::NONE,
pointer_kind: sui_core::PointerKind::Mouse,
is_primary: true,
})
}
fn handle_ready_events(runtime: &mut Runtime) -> Result<usize> {
let ready = runtime.drain_ready_events();
let count = ready.len();
for (window_id, event) in ready {
runtime.handle_event(window_id, event)?;
}
Ok(count)
}
fn solid_fill_colors(output: &sui_runtime::RenderOutput) -> Vec<Color> {
let mut colors = Vec::new();
output.frame.scene.visit_commands(&mut |command| {
if let SceneCommand::FillRect {
brush: Brush::Solid(color),
..
}
| SceneCommand::FillPath {
brush: Brush::Solid(color),
..
} = command
{
colors.push(*color);
}
});
colors
}
fn solid_stroke_colors(output: &sui_runtime::RenderOutput) -> Vec<Color> {
let mut colors = Vec::new();
output.frame.scene.visit_commands(&mut |command| {
if let SceneCommand::StrokeRect {
brush: Brush::Solid(color),
..
}
| SceneCommand::StrokePath {
brush: Brush::Solid(color),
..
} = command
{
colors.push(*color);
}
});
colors
}
fn path_clip_bounds(output: &sui_runtime::RenderOutput) -> Vec<Rect> {
output
.frame
.scene
.commands()
.iter()
.filter_map(|command| match command {
SceneCommand::PushClipPath { path } => Some(path.bounds()),
_ => None,
})
.collect()
}
fn rects_close(left: Rect, right: Rect) -> bool {
(left.x() - right.x()).abs() < 0.001
&& (left.y() - right.y()).abs() < 0.001
&& (left.width() - right.width()).abs() < 0.001
&& (left.height() - right.height()).abs() < 0.001
}
fn stroke_path_bounds_with_color_and_width(
output: &sui_runtime::RenderOutput,
color: Color,
width: f32,
) -> Vec<Rect> {
let mut bounds = Vec::new();
output.frame.scene.visit_commands(&mut |command| {
if let SceneCommand::StrokePath {
path,
brush: Brush::Solid(stroke_color),
stroke,
} = command
&& *stroke_color == color
&& (stroke.width - width).abs() < 0.01
{
bounds.push(path.bounds());
}
});
bounds
}
fn text_run_for(output: &sui_runtime::RenderOutput, text: &str) -> sui_text::TextRun {
output
.frame
.scene
.commands()
.iter()
.find_map(|command| match command {
SceneCommand::DrawText(run) if run.text == text => Some(run.clone()),
SceneCommand::DrawShapedText(run) => run
.resolve(output.frame.text_layout_registry.as_ref())
.filter(|layout| layout.text() == text)
.map(|layout| {
let mut style = layout.style().clone();
if let Some(color) = run.color_override {
style.color = color;
}
sui_text::TextRun {
rect: shaped_text_run_rect(run.origin, layout),
text: layout.text().to_string(),
style,
}
}),
_ => None,
})
.expect("text draw command present")
}
fn shaped_text_run_rect(origin: Point, layout: &sui_text::TextLayout) -> Rect {
let measurement = layout.measurement();
let bounds = measurement.bounds;
let width = if bounds.width().is_finite() && bounds.width() > 0.0 {
bounds.width()
} else {
measurement.width
};
Rect::new(
origin.x + bounds.x(),
origin.y + ((layout.box_size().height - measurement.height).max(0.0) * 0.5),
width,
layout.style().line_height.max(measurement.height),
)
}
fn drawn_layout_for(output: &sui_runtime::RenderOutput, text: &str) -> sui_text::TextLayout {
output
.frame
.scene
.commands()
.iter()
.find_map(|command| match command {
SceneCommand::DrawShapedText(run) => run
.resolve(output.frame.text_layout_registry.as_ref())
.filter(|layout| layout.text() == text)
.cloned(),
_ => None,
})
.expect("shaped text draw command present")
}
fn text_visual_center_for(output: &sui_runtime::RenderOutput, text: &str) -> f32 {
output
.frame
.scene
.commands()
.iter()
.find_map(|command| match command {
SceneCommand::DrawText(run) if run.text == text => {
let layout = TextSystem::new()
.shape_text_run(run, &FontRegistry::new())
.expect("text run should shape");
let line = layout
.lines()
.first()
.expect("text run should contain one line");
Some(run.rect.y() + line.baseline + optical_visual_center(layout.measurement()))
}
SceneCommand::DrawShapedText(run) => {
let layout = run.resolve(output.frame.text_layout_registry.as_ref())?;
if layout.text() != text {
return None;
}
let line = layout
.lines()
.first()
.expect("shaped text should contain one line");
Some(run.origin.y + line.baseline + optical_visual_center(layout.measurement()))
}
_ => None,
})
.expect("text draw command present")
}
fn optical_visual_center(measurement: sui_text::TextMeasurement) -> f32 {
let top = -measurement.cap_height.unwrap_or(measurement.ascent);
let bottom = measurement.descent * 0.5;
(top + bottom) * 0.5
}
fn assert_text_run_uses_token(run: &sui_text::TextRun, token: ThemeTextToken) {
assert_eq!(run.style.font_size, token.size);
assert_eq!(run.style.line_height, token.line_height);
}
#[test]
fn hsv_round_trip_stays_close() {
let color = Color::rgba(0.25, 0.65, 0.82, 0.75);
let (hue, saturation, value) = rgb_to_hsv(color);
let round_trip = hsv_to_rgb(hue, saturation, value, color.alpha);
assert!((round_trip.red - color.red).abs() < 0.02);
assert!((round_trip.green - color.green).abs() < 0.02);
assert!((round_trip.blue - color.blue).abs() < 0.02);
assert!((round_trip.alpha - color.alpha).abs() < f32::EPSILON);
}
#[test]
fn hsl_round_trip_stays_close() {
let color = Color::rgba(0.25, 0.65, 0.82, 0.75);
let (hue, saturation, lightness) = rgb_to_hsl(color, 1.0);
let round_trip = hsl_to_color(color.space, hue, saturation, lightness, color.alpha, 1.0);
assert!((round_trip.red - color.red).abs() < 0.02);
assert!((round_trip.green - color.green).abs() < 0.02);
assert!((round_trip.blue - color.blue).abs() < 0.02);
assert!((round_trip.alpha - color.alpha).abs() < f32::EPSILON);
}
#[test]
fn image_uses_registered_image_size_and_semantics() -> Result<()> {
let handle = ImageHandle::new(7);
let mut application = Application::new();
application.register_image(
handle,
RegisteredImage::from_rgba8(32, 16, vec![255; 32 * 16 * 4])?,
)?;
let mut runtime = application
.window(
WindowBuilder::new()
.title("Image")
.root(Image::new(handle).semantic_name("Preview")),
)
.build()?;
let window_id = runtime.window_ids()[0];
let output = runtime.render(window_id)?;
assert_eq!(output.frame.viewport, Size::new(32.0, 16.0));
let image = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::Image)
.expect("image semantics present");
assert_eq!(image.name.as_deref(), Some("Preview"));
Ok(())
}
#[test]
fn signal_meter_paints_tone_bars_and_semantics() -> Result<()> {
let theme = DefaultTheme::default();
let tone = theme.semantic_tone_color(SemanticTone::Success);
let (mut runtime, window_id) = build_runtime(
SignalMeter::new("Audio input signal")
.description("Microphone activity")
.active(true)
.tone(SemanticTone::Success)
.bars(10)
.size(Size::new(120.0, 18.0))
.theme(theme),
);
let output = runtime.render(window_id)?;
let fills = solid_fill_colors(&output);
assert!(
fills.iter().filter(|color| **color == tone).count() >= 10,
"active signal meter should paint tone-colored bars"
);
let meter = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::GenericContainer
&& node.name.as_deref() == Some("Audio input signal")
})
.expect("signal meter semantics should be present");
assert_eq!(meter.description.as_deref(), Some("Microphone activity"));
assert_eq!(
meter.value.as_ref(),
Some(&SemanticsValue::Text("active".to_string()))
);
Ok(())
}
#[test]
fn signal_meter_level_lights_reached_bars_and_publishes_range() -> Result<()> {
let theme = DefaultTheme::default();
let tone = theme.semantic_tone_color(SemanticTone::Success);
let (mut runtime, window_id) = build_runtime(
SignalMeter::new("Microphone level")
.description("Current microphone input level")
.active(true)
.level_when(|| 0.5)
.tone(SemanticTone::Success)
.bars(10)
.size(Size::new(120.0, 18.0))
.theme(theme),
);
let output = runtime.render(window_id)?;
let fills = solid_fill_colors(&output);
assert_eq!(
fills.iter().filter(|color| **color == tone).count(),
5,
"a half-level signal should light half of the meter bars"
);
let meter = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::ProgressBar
&& node.name.as_deref() == Some("Microphone level")
})
.expect("signal meter semantics should be present");
assert_eq!(
meter.value.as_ref(),
Some(&SemanticsValue::Range {
value: 0.5,
min: 0.0,
max: 1.0,
})
);
Ok(())
}
#[test]
fn signal_meter_level_sanitizes_to_the_normalized_range() {
assert_eq!(
SignalMeter::new("Low").level(-0.25).resolved_level(),
Some(0.0)
);
assert_eq!(
SignalMeter::new("High").level(1.25).resolved_level(),
Some(1.0)
);
assert_eq!(
SignalMeter::new("Invalid")
.level(f32::INFINITY)
.resolved_level(),
Some(0.0)
);
}
#[test]
fn signal_meter_bar_layout_stays_inside_narrow_bounds() {
for width in [0.0_f32, 0.5, 5.0, 10.0, 27.0, 120.0] {
let (bars, gap, bar_width) = signal_meter_bar_layout(width, 18, 3.0);
let painted_width = bar_width * bars as f32 + gap * bars.saturating_sub(1) as f32;
assert!(bars >= 1);
assert!(bar_width >= 0.0);
assert!(painted_width <= width.max(0.0) + f32::EPSILON * 8.0);
}
assert_eq!(signal_meter_bar_layout(10.0, 18, 3.0), (10, 0.0, 1.0));
}
#[test]
fn image_without_border_omits_frame_stroke() -> Result<()> {
let handle = ImageHandle::new(8);
let mut application = Application::new();
application.register_image(
handle,
RegisteredImage::from_rgba8(32, 32, vec![255; 32 * 32 * 4])?,
)?;
let mut runtime = application
.window(
WindowBuilder::new()
.title("Image")
.root(Image::new(handle).show_border(false)),
)
.build()?;
let window_id = runtime.window_ids()[0];
let output = runtime.render(window_id)?;
let mut image_draws = 0;
let mut frame_strokes = 0;
output
.frame
.scene
.visit_commands(&mut |command| match command {
SceneCommand::DrawImage { source, .. } if source.image == handle => {
image_draws += 1;
}
SceneCommand::StrokeRect { .. } | SceneCommand::StrokePath { .. } => {
frame_strokes += 1;
}
_ => {}
});
assert_eq!(image_draws, 1);
assert_eq!(frame_strokes, 0);
Ok(())
}
#[test]
fn image_corner_radius_clips_bitmap_with_rounded_path() -> Result<()> {
let handle = ImageHandle::new(9);
let mut application = Application::new();
application.register_image(
handle,
RegisteredImage::from_rgba8(32, 16, vec![255; 32 * 16 * 4])?,
)?;
let mut runtime = application
.window(
WindowBuilder::new().title("Rounded image").root(
Image::new(handle)
.fit(super::ImageFit::Cover)
.corner_radius(12.0),
),
)
.build()?;
let window_id = runtime.window_ids()[0];
let output = runtime.render(window_id)?;
assert!(
output
.frame
.scene
.commands()
.iter()
.any(|command| matches!(command, SceneCommand::PushClipPath { .. })),
"rounded images should clip bitmap pixels to the rounded frame"
);
Ok(())
}
#[test]
fn color_swatch_click_invokes_callback() -> Result<()> {
let presses = Rc::new(RefCell::new(Vec::new()));
let on_press = Rc::clone(&presses);
let color = Color::rgba(0.2, 0.4, 0.8, 1.0);
let (mut runtime, window_id) = build_runtime(
ColorSwatch::new("Accent", color)
.on_press(move |color| on_press.borrow_mut().push(color)),
);
let _ = runtime.render(window_id)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, Point::new(16.0, 16.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Up, Point::new(16.0, 16.0), false),
)?;
assert_eq!(presses.borrow().as_slice(), &[color]);
let output = runtime.render(window_id)?;
let swatch = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorSwatch)
.expect("color swatch semantics present");
assert_eq!(
swatch.value,
Some(SemanticsValue::Text("#3366CCFF".to_string()))
);
Ok(())
}
#[test]
fn color_swatch_checkerboards_follow_the_rounded_color_shape() -> Result<()> {
let theme = DefaultTheme::dark();
let translucent = Color::rgba(0.16, 0.52, 0.88, 0.55);
let (mut runtime, window_id) =
build_runtime(ColorSwatch::new("Translucent accent", translucent).theme(theme));
let output = runtime.render(window_id)?;
let swatch = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorSwatch)
.expect("standalone color swatch semantics present");
let expected_inner = super::inset_rect(
swatch.bounds,
Insets::all(theme.metrics.color_swatch_inner_inset),
);
let clips = path_clip_bounds(&output);
assert!(
clips.iter().any(|clip| rects_close(*clip, expected_inner)),
"standalone swatch checkerboard should be clipped to its rounded inner color shape; expected {expected_inner:?}, clips={clips:?}"
);
let (mut runtime, window_id) = build_runtime(
ColorPalette::new("Dark palette")
.theme(theme)
.swatch(ColorPaletteSwatch::new("Selected accent", translucent))
.selected(0),
);
let output = runtime.render(window_id)?;
let selected = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::ColorSwatch
&& node.name.as_deref() == Some("Selected accent")
})
.expect("selected palette swatch semantics present");
let expected_inner = super::inset_rect(
selected.bounds,
Insets::all(theme.metrics.color_palette_selected_swatch_inset),
);
let clips = path_clip_bounds(&output);
assert!(
clips.iter().any(|clip| rects_close(*clip, expected_inner)),
"selected palette checkerboard should be clipped to its rounded color shape; expected {expected_inner:?}, clips={clips:?}"
);
Ok(())
}
#[test]
fn color_swatch_hover_and_press_use_theme_motion() -> Result<()> {
let theme = DefaultTheme::default();
let hover_duration = theme.motion.hover_duration();
let press_duration = theme.motion.press_duration();
let expected_hover = theme.palette.control_hover;
let expected_press = super::mix_color(
expected_hover,
theme.palette.control_active,
theme.interaction.pressed_blend,
);
let (mut runtime, window_id) =
build_runtime(ColorSwatch::new("Accent", Color::rgba(0.2, 0.4, 0.8, 1.0)).theme(theme));
let output = runtime.render(window_id)?;
let swatch = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorSwatch)
.expect("color swatch semantics present");
let position = rect_center(swatch.bounds);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, position, false),
)?;
runtime.tick(hover_duration * 0.5);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let mid_hover = runtime.render(window_id)?;
assert!(
!solid_fill_colors(&mid_hover).contains(&expected_hover),
"swatch hover fill should not snap to the settled hover color"
);
runtime.tick(hover_duration);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let settled_hover = runtime.render(window_id)?;
assert!(solid_fill_colors(&settled_hover).contains(&expected_hover));
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, position, true),
)?;
runtime.tick(hover_duration + press_duration * 0.5);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let mid_press = runtime.render(window_id)?;
assert!(
!solid_fill_colors(&mid_press).contains(&expected_press),
"swatch press fill should not snap to the settled pressed color"
);
runtime.tick(hover_duration + press_duration);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let settled_press = runtime.render(window_id)?;
assert!(solid_fill_colors(&settled_press).contains(&expected_press));
Ok(())
}
#[test]
fn color_swatch_read_only_color_when_syncs_external_value() -> Result<()> {
let color = Rc::new(RefCell::new(Color::rgba(0.08, 0.22, 0.78, 1.0)));
let color_reader = Rc::clone(&color);
let (mut runtime, window_id) = build_runtime(
ColorSwatch::new("Current brush color", *color.borrow())
.color_when(move || *color_reader.borrow())
.read_only(true),
);
let output = runtime.render(window_id)?;
let swatch = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::ColorSwatch
&& node.name.as_deref() == Some("Current brush color")
})
.expect("current color swatch semantics present");
assert_eq!(
swatch.value,
Some(SemanticsValue::Text("#1438C7FF".to_string()))
);
assert!(swatch.actions.is_empty());
*color.borrow_mut() = Color::rgba(0.90, 0.32, 0.18, 1.0);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, Point::new(8.0, 8.0), false),
)?;
let output = runtime.render(window_id)?;
let swatch = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::ColorSwatch
&& node.name.as_deref() == Some("Current brush color")
})
.expect("current color swatch semantics still present");
assert_eq!(
swatch.value,
Some(SemanticsValue::Text("#E6522EFF".to_string()))
);
Ok(())
}
#[test]
fn color_palette_exposes_selected_swatch_semantics() -> Result<()> {
let (mut runtime, window_id) = build_runtime(
ColorPalette::new("Brush palette")
.swatches([
ColorPaletteSwatch::new("Ink", Color::rgba(0.08, 0.10, 0.15, 1.0)),
ColorPaletteSwatch::new("Ocean", Color::rgba(0.08, 0.22, 0.78, 1.0)),
ColorPaletteSwatch::new("Mint", Color::rgba(0.28, 0.78, 0.58, 1.0)),
])
.selected(1),
);
let output = runtime.render(window_id)?;
let palette = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::GenericContainer
&& node.name.as_deref() == Some("Brush palette")
})
.expect("palette semantics present");
assert_eq!(
palette.value,
Some(SemanticsValue::Text("Ocean #1438C7FF".to_string()))
);
let selected = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::ColorSwatch && node.name.as_deref() == Some("Ocean")
})
.expect("selected swatch semantics present");
assert!(selected.state.selected);
assert_eq!(
selected.value,
Some(SemanticsValue::Text("#1438C7FF".to_string()))
);
Ok(())
}
#[test]
fn media_widget_defaults_follow_theme_density() -> Result<()> {
let (mut compact_runtime, compact_window) = build_runtime(
ColorSwatch::new("Accent", Color::rgba(0.2, 0.4, 0.8, 1.0))
.theme(DefaultTheme::compact()),
);
let compact = compact_runtime.render(compact_window)?.frame.viewport;
let (mut touch_runtime, touch_window) = build_runtime(
ColorSwatch::new("Accent", Color::rgba(0.2, 0.4, 0.8, 1.0))
.theme(DefaultTheme::touch()),
);
let touch = touch_runtime.render(touch_window)?.frame.viewport;
assert_eq!(
compact,
Size::new(
DefaultTheme::compact().metrics.color_swatch_width,
DefaultTheme::compact().metrics.color_swatch_height,
)
);
assert_eq!(
touch,
Size::new(
DefaultTheme::touch().metrics.color_swatch_width,
DefaultTheme::touch().metrics.color_swatch_height,
)
);
assert!(touch.width > compact.width);
assert!(touch.height > compact.height);
Ok(())
}
#[test]
fn color_palette_defaults_and_overrides_are_density_aware() -> Result<()> {
let swatches = [
ColorPaletteSwatch::new("Ink", Color::rgba(0.08, 0.10, 0.15, 1.0)),
ColorPaletteSwatch::new("Ocean", Color::rgba(0.08, 0.22, 0.78, 1.0)),
];
let (mut compact_runtime, compact_window) = build_runtime(
ColorPalette::new("Brush palette")
.theme(DefaultTheme::compact())
.swatches(swatches.clone()),
);
let compact = compact_runtime.render(compact_window)?.frame.viewport;
let (mut touch_runtime, touch_window) = build_runtime(
ColorPalette::new("Brush palette")
.theme(DefaultTheme::touch())
.swatches(swatches.clone()),
);
let touch = touch_runtime.render(touch_window)?.frame.viewport;
assert!(touch.width > compact.width);
assert_eq!(
compact.height,
DefaultTheme::compact().metrics.color_palette_swatch_size
);
let (mut override_runtime, override_window) = build_runtime(
ColorPalette::new("Brush palette")
.theme(DefaultTheme::touch())
.swatches(swatches)
.swatch_size(18.0)
.gap(2.0),
);
let overridden = override_runtime.render(override_window)?.frame.viewport;
assert_eq!(overridden, Size::new(38.0, 18.0));
Ok(())
}
#[test]
fn color_palette_click_invokes_callback_and_selects_swatch() -> Result<()> {
let changes = Rc::new(RefCell::new(Vec::new()));
let changes_writer = Rc::clone(&changes);
let (mut runtime, window_id) = build_runtime(
ColorPalette::new("Brush palette")
.swatches([
ColorPaletteSwatch::new("Ink", Color::rgba(0.08, 0.10, 0.15, 1.0)),
ColorPaletteSwatch::new("Ocean", Color::rgba(0.08, 0.22, 0.78, 1.0)),
ColorPaletteSwatch::new("Mint", Color::rgba(0.28, 0.78, 0.58, 1.0)),
])
.selected(0)
.on_change(move |index, name, color| {
changes_writer.borrow_mut().push((index, name, color));
}),
);
let output = runtime.render(window_id)?;
let mint = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::ColorSwatch && node.name.as_deref() == Some("Mint")
})
.expect("target swatch semantics present");
let position = Point::new(
mint.bounds.x() + mint.bounds.width() * 0.5,
mint.bounds.y() + mint.bounds.height() * 0.5,
);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, position, true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Up, position, false),
)?;
assert_eq!(
changes.borrow().as_slice(),
&[(2, "Mint".to_string(), Color::rgba(0.28, 0.78, 0.58, 1.0))]
);
let output = runtime.render(window_id)?;
assert!(output.semantics.iter().any(|node| {
node.role == SemanticsRole::ColorSwatch
&& node.name.as_deref() == Some("Mint")
&& node.state.selected
}));
Ok(())
}
#[test]
fn color_palette_hover_and_press_use_theme_motion() -> Result<()> {
let theme = DefaultTheme::default();
let hover_duration = theme.motion.hover_duration();
let press_duration = theme.motion.press_duration();
let expected_hover = super::mix_color(
theme.palette.control,
theme.palette.control_hover,
theme.interaction.hover_blend,
);
let expected_press = super::mix_color(
expected_hover,
theme.palette.control_active,
theme.interaction.pressed_blend,
);
let (mut runtime, window_id) =
build_runtime(ColorPalette::new("Brush palette").theme(theme).swatches([
ColorPaletteSwatch::new("Ink", Color::rgba(0.08, 0.10, 0.15, 1.0)),
ColorPaletteSwatch::new("Ocean", Color::rgba(0.08, 0.22, 0.78, 1.0)),
ColorPaletteSwatch::new("Mint", Color::rgba(0.28, 0.78, 0.58, 1.0)),
]));
let output = runtime.render(window_id)?;
let mint = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::ColorSwatch && node.name.as_deref() == Some("Mint")
})
.expect("target swatch semantics present");
let position = rect_center(mint.bounds);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, position, false),
)?;
runtime.tick(hover_duration * 0.5);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let mid_hover = runtime.render(window_id)?;
assert!(
!solid_fill_colors(&mid_hover).contains(&expected_hover),
"palette swatch hover fill should not snap to the settled hover color"
);
runtime.tick(hover_duration);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let settled_hover = runtime.render(window_id)?;
assert!(solid_fill_colors(&settled_hover).contains(&expected_hover));
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, position, true),
)?;
runtime.tick(hover_duration + press_duration * 0.5);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let mid_press = runtime.render(window_id)?;
assert!(
!solid_fill_colors(&mid_press).contains(&expected_press),
"palette swatch press fill should not snap to the settled pressed color"
);
runtime.tick(hover_duration + press_duration);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let settled_press = runtime.render(window_id)?;
assert!(solid_fill_colors(&settled_press).contains(&expected_press));
Ok(())
}
#[test]
fn brush_preview_exposes_current_brush_semantics_and_paints_sample() -> Result<()> {
let (mut runtime, window_id) = build_runtime(
BrushPreview::new("Brush preview")
.spec(BrushPreviewSpec::new(
Color::rgba(0.10, 0.30, 0.90, 1.0),
22.0,
0.75,
BrushPreviewShape::Square,
))
.size(Size::new(220.0, 64.0)),
);
let output = runtime.render(window_id)?;
let preview = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::Image && node.name.as_deref() == Some("Brush preview")
})
.expect("brush preview semantics present");
assert_eq!(
preview.value,
Some(SemanticsValue::Text(
"Square brush, 22 px, 75% opacity".to_string(),
))
);
let mut fill_count = 0;
output.frame.scene.visit_commands(&mut |command| {
if matches!(
command,
SceneCommand::FillRect { .. } | SceneCommand::FillPath { .. }
) {
fill_count += 1;
}
});
assert!(fill_count > 3);
let value_text = "Square brush, 22 px, 75% opacity";
let actual_visual_center = text_visual_center_for(&output, value_text);
let theme = DefaultTheme::default();
let metrics = theme.metrics;
let bounds = Rect::new(0.0, 0.0, 220.0, 64.0);
let content = super::inset_rect(bounds, metrics.brush_preview_padding);
let swatch_width = metrics.brush_preview_swatch_width.min(content.width());
let sample = Rect::new(
content.x() + swatch_width + metrics.brush_preview_swatch_gap,
content.y(),
(content.width() - swatch_width - metrics.brush_preview_swatch_gap).max(0.0),
content.height(),
);
let text_height = metrics
.brush_preview_text_height
.max(theme.text.xs.line_height);
let text_slot = Rect::new(
sample.x(),
sample.max_y() - text_height,
sample.width(),
text_height,
);
let slot_center = text_slot.y() + (text_slot.height() * 0.5);
assert!((actual_visual_center - slot_center).abs() < 0.75);
Ok(())
}
#[test]
fn brush_preview_value_uses_text_token_and_preserves_slot_centering() -> Result<()> {
let mut theme = DefaultTheme::default();
theme.text.xs = ThemeTextToken {
size: 28.0,
line_height: 10.0,
};
theme.metrics.brush_preview_text_height = 44.0;
theme.metrics.brush_preview_min_width = 420.0;
theme.metrics.brush_preview_min_height = 96.0;
let metrics = theme.metrics;
let value_text = "Round brush, 8 px, 100% opacity";
let (mut runtime, window_id) = build_runtime(
BrushPreview::new("Brush preview")
.theme(theme)
.spec(BrushPreviewSpec::new(
Color::rgba(0.10, 0.30, 0.90, 1.0),
8.0,
1.0,
BrushPreviewShape::Round,
))
.size(Size::new(420.0, 96.0)),
);
let output = runtime.render(window_id)?;
let text = text_run_for(&output, value_text);
let layout = drawn_layout_for(&output, value_text);
let actual_visual_center = text_visual_center_for(&output, value_text);
let bounds = Rect::new(0.0, 0.0, 420.0, 96.0);
let content = super::inset_rect(bounds, metrics.brush_preview_padding);
let swatch_width = metrics.brush_preview_swatch_width.min(content.width());
let sample = Rect::new(
content.x() + swatch_width + metrics.brush_preview_swatch_gap,
content.y(),
(content.width() - swatch_width - metrics.brush_preview_swatch_gap).max(0.0),
content.height(),
);
let text_height = metrics
.brush_preview_text_height
.max(theme.text.xs.line_height);
let text_slot = Rect::new(
sample.x(),
sample.max_y() - text_height,
sample.width(),
text_height,
);
let slot_center = text_slot.y() + (text_slot.height() * 0.5);
assert_eq!(text.style.font_size, 28.0);
assert_eq!(text.style.line_height, 10.0);
assert!(text.rect.height() >= layout.measurement().height - 0.01);
assert!(text.rect.height() > text.style.line_height);
assert!((text.rect.x() - text_slot.x()).abs() < 0.75);
assert!((actual_visual_center - slot_center).abs() < 0.75);
Ok(())
}
#[test]
fn color_picker_pointer_drag_updates_semantics() -> Result<()> {
let changes = Rc::new(RefCell::new(Vec::new()));
let on_change = Rc::clone(&changes);
let (mut runtime, window_id) = build_runtime(
ColorPicker::new("Accent picker")
.on_change(move |color| on_change.borrow_mut().push(color)),
);
let initial = runtime.render(window_id)?;
let map = initial
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Saturation and value"))
.expect("saturation/value semantics present")
.bounds;
let start = rect_center(map);
let end = Point::new(map.x() + map.width() * 0.80, map.y() + map.height() * 0.35);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, start, true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, end, true),
)?;
runtime.handle_event(window_id, primary_pointer(PointerEventKind::Up, end, false))?;
let changed_color = *changes
.borrow()
.last()
.expect("color picker emitted change");
let output = runtime.render(window_id)?;
let picker = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorPicker)
.expect("color picker semantics present");
assert_eq!(
picker.value,
Some(SemanticsValue::Text(format_color(changed_color)))
);
Ok(())
}
#[test]
fn simple_color_picker_hsl_mode_can_hide_alpha_slider() -> Result<()> {
let (mut runtime, window_id) = build_runtime(
SimpleColorPicker::from_color("Simple picker", Color::rgba(0.25, 0.65, 0.82, 0.75))
.mode(SimpleColorPickerMode::Hsl)
.show_alpha(false),
);
let output = runtime.render(window_id)?;
let picker = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::ColorPicker
&& node.name.as_deref() == Some("Simple picker")
})
.expect("simple color picker semantics present");
assert_eq!(
picker.description.as_deref(),
Some("HSL sliders; sRGB editing space; SDR range available")
);
let child = |name: &str| {
output.semantics.iter().find(|node| {
node.parent == Some(picker.id)
&& node.role == SemanticsRole::Slider
&& node.name.as_deref() == Some(name)
})
};
assert!(child("Hue").is_some());
assert!(child("Saturation").is_some());
assert!(child("Lightness").is_some());
assert!(
child("Alpha").is_none(),
"hidden alpha should not expose an alpha slider"
);
Ok(())
}
#[test]
fn simple_color_picker_rgb_drag_updates_color_channels() -> Result<()> {
let changes = Rc::new(RefCell::new(Vec::new()));
let on_change = Rc::clone(&changes);
let (mut runtime, window_id) = build_runtime(
SimpleColorPicker::from_color(
"Simple RGB picker",
Color::new(ColorSpace::LinearSrgb, 0.2, 0.3, 0.4, 1.0),
)
.mode(SimpleColorPickerMode::Rgb)
.on_change(move |color| on_change.borrow_mut().push(color)),
);
let initial = runtime.render(window_id)?;
let red = initial
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Red"))
.expect("red channel semantics present")
.bounds;
let start = Point::new(red.x() + red.width() * 0.20, red.y() + red.height() * 0.5);
let end = Point::new(red.x() + red.width() * 0.80, red.y() + red.height() * 0.5);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, start, true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, end, true),
)?;
runtime.handle_event(window_id, primary_pointer(PointerEventKind::Up, end, false))?;
let changed_color = *changes
.borrow()
.last()
.expect("simple RGB picker emitted change");
assert!(
changed_color.red > 9.0,
"expected HDR red channel after RGB drag, got {}",
changed_color.red
);
assert_eq!(changed_color.space, ColorSpace::LinearSrgb);
Ok(())
}
fn picker_slider(output: &sui_runtime::RenderOutput, name: &str) -> SemanticsNode {
output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::Slider && node.name.as_deref() == Some(name))
.unwrap_or_else(|| panic!("{name} slider semantics present"))
.clone()
}
fn drag_across(runtime: &mut Runtime, window_id: WindowId, from: Point, to: Point) {
for (kind, position, pressed) in [
(PointerEventKind::Down, from, true),
(PointerEventKind::Move, to, true),
(PointerEventKind::Up, to, false),
] {
runtime
.handle_event(window_id, primary_pointer(kind, position, pressed))
.unwrap();
}
}
fn row_point(bounds: Rect, fraction: f32) -> Point {
Point::new(
bounds.x() + bounds.width() * fraction,
bounds.y() + bounds.height() * 0.5,
)
}
fn gradient_fill(output: &sui_runtime::RenderOutput, row: Rect) -> (Rect, Vec<GradientStop>) {
output
.frame
.scene
.commands()
.iter()
.find_map(|command| match command {
SceneCommand::FillRect {
rect,
brush: Brush::LinearGradient { stops, .. },
} if row.contains(Point::new(rect.x() + 0.5, rect.y() + 0.5)) => {
Some((*rect, stops.clone()))
}
_ => None,
})
.unwrap_or_else(|| panic!("a gradient track is painted in {row:?}"))
}
fn gradient_track(output: &sui_runtime::RenderOutput, row: Rect) -> Rect {
gradient_fill(output, row).0
}
#[test]
fn oklch_hue_track_sweeps_the_hue_wheel() -> Result<()> {
let (mut runtime, window_id) = build_runtime(
SimpleColorPicker::from_color(
"Theme color",
Oklch::new(0.7, 0.12, 40.0, 1.0).to_srgb(),
)
.mode(SimpleColorPickerMode::Oklch),
);
let output = runtime.render(window_id)?;
let hue = picker_slider(&output, "Hue").bounds;
let (_, stops) = gradient_fill(&output, hue);
assert!(stops.len() >= 16, "{} stops", stops.len());
assert_eq!(stops.first().map(|stop| stop.offset), Some(0.0));
assert_eq!(stops.last().map(|stop| stop.offset), Some(1.0));
assert!(stops.windows(2).all(|pair| pair[0].offset < pair[1].offset));
let hues = stops
.iter()
.map(|stop| stop.color.to_oklch().hue)
.collect::<Vec<_>>();
let span = hues.iter().copied().fold(f32::MIN, f32::max)
- hues.iter().copied().fold(f32::MAX, f32::min);
assert!(span > 300.0, "the hue track sweeps the wheel: {hues:?}");
Ok(())
}
#[test]
fn simple_color_picker_oklch_mode_exposes_perceptual_channels() -> Result<()> {
let color = Oklch::new(0.62, 0.14, 250.0, 1.0).to_srgb();
let expected = color.to_oklch();
let (mut runtime, window_id) = build_runtime(
SimpleColorPicker::from_color("Theme color", color).mode(SimpleColorPickerMode::Oklch),
);
let output = runtime.render(window_id)?;
let picker = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorPicker)
.expect("color picker semantics present");
assert_eq!(
picker.description.as_deref(),
Some("OKLCH sliders; sRGB editing space; SDR range available")
);
let range = |name: &str| match picker_slider(&output, name).value {
Some(SemanticsValue::Range { value, min, max }) => (value as f32, min, max),
other => panic!("{name} has a range value, got {other:?}"),
};
let (lightness, _, lightness_max) = range("Lightness");
let (chroma, _, chroma_max) = range("Chroma");
let (hue, _, hue_max) = range("Hue");
assert!((lightness - expected.lightness * 100.0).abs() < 0.01);
assert_eq!(lightness_max, 100.0);
assert!((chroma - expected.chroma).abs() < 1.0e-4);
assert!((chroma_max - f64::from(OKLCH_MAX_CHROMA)).abs() < 1.0e-6);
assert!((hue - expected.hue).abs() < 0.01);
assert_eq!(hue_max, 360.0);
picker_slider(&output, "Alpha");
Ok(())
}
#[test]
fn simple_color_picker_oklch_lightness_drag_keeps_hue_and_chroma() -> Result<()> {
let start = Oklch::new(0.55, 0.10, 150.0, 1.0);
let changes = Rc::new(RefCell::new(Vec::new()));
let on_change = Rc::clone(&changes);
let (mut runtime, window_id) = build_runtime(
SimpleColorPicker::from_color("Theme color", start.to_srgb())
.mode(SimpleColorPickerMode::Oklch)
.on_change(move |color| on_change.borrow_mut().push(color)),
);
let output = runtime.render(window_id)?;
let lightness = gradient_track(&output, picker_slider(&output, "Lightness").bounds);
drag_across(
&mut runtime,
window_id,
row_point(lightness, 0.55),
row_point(lightness, 0.75),
);
let changed = changes
.borrow()
.last()
.expect("the drag emits a color")
.to_oklch();
assert!(
(changed.lightness - 0.75).abs() < 0.01,
"lightness follows the track: {changed:?}"
);
assert!((changed.hue - start.hue).abs() < 1.0, "{changed:?}");
assert!((changed.chroma - start.chroma).abs() < 0.005, "{changed:?}");
Ok(())
}
#[test]
fn simple_color_picker_oklch_hue_survives_zero_chroma() -> Result<()> {
let start = Oklch::new(0.6, 0.12, 30.0, 1.0);
let changes = Rc::new(RefCell::new(Vec::new()));
let on_change = Rc::clone(&changes);
let (mut runtime, window_id) = build_runtime(
SimpleColorPicker::from_color("Theme color", start.to_srgb())
.mode(SimpleColorPickerMode::Oklch)
.on_change(move |color| on_change.borrow_mut().push(color)),
);
let output = runtime.render(window_id)?;
let chroma = gradient_track(&output, picker_slider(&output, "Chroma").bounds);
drag_across(
&mut runtime,
window_id,
row_point(chroma, 0.5),
row_point(chroma, 0.0),
);
let gray = changes.borrow().last().copied().expect("a gray is emitted");
assert!(gray.to_oklch().chroma < 0.001, "{:?}", gray.to_oklch());
let _ = runtime.render(window_id)?;
drag_across(
&mut runtime,
window_id,
row_point(chroma, 0.0),
row_point(chroma, 0.4),
);
let restored = changes.borrow().last().expect("chroma returns").to_oklch();
assert!(restored.chroma > 0.05, "{restored:?}");
assert!(
(restored.hue - start.hue).abs() < 1.0,
"the hue is kept through gray: {restored:?}"
);
Ok(())
}
#[test]
fn simple_color_picker_oklch_fits_chroma_into_the_editing_gamut() -> Result<()> {
let start = Oklch::new(0.7, 0.05, 145.0, 1.0);
let changes = Rc::new(RefCell::new(Vec::new()));
let on_change = Rc::clone(&changes);
let (mut runtime, window_id) = build_runtime(
SimpleColorPicker::from_color("Theme color", start.to_srgb())
.mode(SimpleColorPickerMode::Oklch)
.on_change(move |color| on_change.borrow_mut().push(color)),
);
let output = runtime.render(window_id)?;
let chroma = gradient_track(&output, picker_slider(&output, "Chroma").bounds);
drag_across(
&mut runtime,
window_id,
row_point(chroma, 0.5),
row_point(chroma, 1.0),
);
let color = *changes.borrow().last().expect("the drag emits a color");
assert_eq!(color.space, ColorSpace::Srgb);
for channel in [color.red, color.green, color.blue] {
assert!((0.0..=1.0).contains(&channel), "{color:?}");
}
let fitted = color.to_oklch();
assert!(
(fitted.chroma - start.max_srgb_chroma()).abs() < 0.01,
"chroma stops at the sRGB edge: {fitted:?}"
);
assert!((fitted.lightness - start.lightness).abs() < 0.01);
let output = runtime.render(window_id)?;
match picker_slider(&output, "Chroma").value {
Some(SemanticsValue::Range { value, .. }) => assert!(
(value - f64::from(OKLCH_MAX_CHROMA)).abs() < 1.0e-4,
"the slider keeps the requested chroma: {value}"
),
other => panic!("chroma has a range value, got {other:?}"),
}
Ok(())
}
#[test]
fn color_slider_label_and_value_sit_beside_the_track() -> Result<()> {
let (mut runtime, window_id) = build_runtime(
SimpleColorPicker::from_color("Theme color", Color::rgba(1.0, 0.25, 0.5, 1.0))
.mode(SimpleColorPickerMode::Rgb),
);
let output = runtime.render(window_id)?;
let track = output
.frame
.scene
.commands()
.iter()
.find_map(|command| match command {
SceneCommand::DrawShaderRect {
rect,
shader: WidgetShader::ColorPickerRgbChannelBar { channel: 0, .. },
} => Some(*rect),
_ => None,
})
.expect("the red track is painted");
let marker_reach = 7.5;
let label = text_run_for(&output, "R");
let value = text_run_for(&output, "1.000");
assert!(
label.rect.max_x() <= track.x() - marker_reach,
"label {:?} clears the track {track:?}",
label.rect
);
assert!(
value.rect.x() >= track.max_x() + marker_reach,
"value {:?} clears the marker at the track end {track:?}",
value.rect
);
Ok(())
}
#[test]
fn color_slider_drag_maps_across_the_track_not_the_row() -> Result<()> {
let changes = Rc::new(RefCell::new(Vec::new()));
let on_change = Rc::clone(&changes);
let (mut runtime, window_id) = build_runtime(
SimpleColorPicker::from_color("Theme color", Color::rgba(0.5, 0.5, 0.5, 1.0))
.mode(SimpleColorPickerMode::Rgb)
.on_change(move |color| on_change.borrow_mut().push(color)),
);
let output = runtime.render(window_id)?;
let track = output
.frame
.scene
.commands()
.iter()
.find_map(|command| match command {
SceneCommand::DrawShaderRect {
rect,
shader: WidgetShader::ColorPickerRgbChannelBar { channel: 0, .. },
} => Some(*rect),
_ => None,
})
.expect("the red track is painted");
drag_across(
&mut runtime,
window_id,
row_point(track, 0.5),
row_point(track, 0.25),
);
let red = changes.borrow().last().expect("the drag emits a color").red;
assert!((red - 0.25).abs() < 0.01, "red {red}");
Ok(())
}
#[test]
fn color_picker_focus_surface_uses_theme_motion() -> Result<()> {
let theme = DefaultTheme::default();
let focus_duration = theme.motion.focus_duration();
let (mut runtime, window_id) = build_runtime(ColorPicker::new("Accent picker"));
let output = runtime.render(window_id)?;
let focus_target = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Saturation and value"))
.expect("color picker saturation/value semantics present")
.bounds;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, rect_center(focus_target), true),
)?;
let _ = runtime.render(window_id)?;
runtime.tick(focus_duration * 0.5);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let mid = runtime.render(window_id)?;
assert!(
!solid_stroke_colors(&mid).contains(&theme.palette.focus_ring),
"color picker focus ring should not snap to the settled focus color"
);
runtime.tick(focus_duration);
assert_eq!(handle_ready_events(&mut runtime)?, 0);
let settled = runtime.render(window_id)?;
assert!(
solid_stroke_colors(&settled).contains(&theme.palette.focus_ring),
"color picker focus ring should settle to the theme focus color"
);
Ok(())
}
#[test]
fn color_picker_color_when_syncs_external_color() -> Result<()> {
let color = Rc::new(RefCell::new(Color::rgba(0.08, 0.22, 0.78, 1.0)));
let color_reader = Rc::clone(&color);
let (mut runtime, window_id) = build_runtime(
ColorPicker::from_color("Brush color", *color.borrow())
.color_when(move || *color_reader.borrow()),
);
let output = runtime.render(window_id)?;
let picker = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorPicker)
.expect("color picker semantics present");
assert_eq!(
picker.value,
Some(SemanticsValue::Text("#1438C7FF".to_string()))
);
*color.borrow_mut() = Color::rgba(0.90, 0.32, 0.18, 1.0);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, Point::new(8.0, 8.0), false),
)?;
let output = runtime.render(window_id)?;
let picker = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorPicker)
.expect("color picker semantics still present");
assert_eq!(
picker.value,
Some(SemanticsValue::Text("#E6522EFF".to_string()))
);
Ok(())
}
#[test]
fn color_picker_from_color_preserves_hdr_linear_color_space() {
let picker = ColorPicker::from_color(
"HDR accent",
Color::new(ColorSpace::LinearSrgb, 2.0, 0.5, 0.25, 1.0),
);
assert_eq!(picker.current_color().space, ColorSpace::LinearSrgb);
assert!((picker.current_color().red - 2.0).abs() < f32::EPSILON);
assert!((picker.current_color().green - 0.5).abs() < f32::EPSILON);
assert!((picker.current_color().blue - 0.25).abs() < f32::EPSILON);
assert!((picker.current_color().alpha - 1.0).abs() < f32::EPSILON);
}
#[test]
fn color_picker_semantics_describe_hdr_editing_mode() -> Result<()> {
let (mut runtime, window_id) = build_runtime(ColorPicker::from_color(
"HDR accent",
Color::new(ColorSpace::LinearSrgb, 2.0, 0.5, 0.25, 1.0),
));
let output = runtime.render(window_id)?;
let picker = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorPicker)
.expect("color picker semantics present");
assert_eq!(
picker.description.as_deref(),
Some("BT709 Linear editing space; HDR range available")
);
Ok(())
}
#[test]
fn color_picker_semantics_expose_accessible_components() -> Result<()> {
let (mut runtime, window_id) = build_runtime(ColorPicker::from_color(
"Accent picker",
Color::new(ColorSpace::LinearSrgb, 2.0, 0.65, 0.4, 1.0),
));
let output = runtime.render(window_id)?;
let picker = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorPicker)
.expect("color picker semantics present");
let picker_id = picker.id;
let child = |role: SemanticsRole, name: &str| {
output.semantics.iter().find(|node| {
node.parent == Some(picker_id)
&& node.role == role
&& node.name.as_deref() == Some(name)
})
};
assert!(child(SemanticsRole::ColorSwatch, "Current color").is_some());
assert!(child(SemanticsRole::ColorSwatch, "Previous color").is_some());
let range = child(SemanticsRole::ComboBox, "Color range")
.expect("color range selector semantics present");
assert_eq!(
range.value,
Some(SemanticsValue::Text("BT709 Linear".to_string()))
);
assert_eq!(range.state.expanded, Some(false));
for name in [
"Saturation and value",
"Hue",
"Saturation",
"Value",
"Alpha",
"Red",
"Green",
"Blue",
] {
let slider = child(SemanticsRole::Slider, name)
.unwrap_or_else(|| panic!("{name} slider semantics present"));
assert!(
matches!(slider.value, Some(SemanticsValue::Range { .. })),
"{name} slider should expose a range value"
);
}
assert!(
output.semantics.iter().any(|node| {
node.parent == Some(picker_id)
&& node.role == SemanticsRole::Text
&& node
.name
.as_deref()
.is_some_and(|name| name.starts_with("HDR hex unavailable"))
}),
"HDR hex field semantics present"
);
Ok(())
}
#[test]
fn color_picker_synthetic_semantics_ids_are_javascript_safe_and_distinct() {
let parent = WidgetId::new(402);
let parts = [
ColorPickerSemanticPart::CurrentColor,
ColorPickerSemanticPart::PreviousColor,
ColorPickerSemanticPart::ColorRange,
ColorPickerSemanticPart::ColorRangeMenu,
ColorPickerSemanticPart::ColorRangeOption(0),
ColorPickerSemanticPart::ColorRangeOption(1),
ColorPickerSemanticPart::ColorRangeOption(2),
ColorPickerSemanticPart::ColorRangeOption(3),
ColorPickerSemanticPart::SaturationValue,
ColorPickerSemanticPart::Hue,
ColorPickerSemanticPart::Saturation,
ColorPickerSemanticPart::Value,
ColorPickerSemanticPart::Alpha,
ColorPickerSemanticPart::Red,
ColorPickerSemanticPart::Green,
ColorPickerSemanticPart::Blue,
ColorPickerSemanticPart::Hex,
];
let mut ids = BTreeSet::new();
for part in parts {
let id = color_picker_child_semantics_id(parent, part).get();
assert!(id < (1_u64 << 53), "{id} should be JS-safe");
assert!(ids.insert(id), "{id} should be unique");
}
}
#[test]
fn color_picker_columns_are_compact() {
let picker = ColorPicker::new("Accent picker");
let bounds = Rect::new(0.0, 0.0, 434.0, 448.0);
let wheel = picker.color_wheel_rect(bounds);
let map = picker.saturation_value_rect(bounds);
let metrics = DefaultTheme::default().metrics;
assert_eq!(map.x() - wheel.max_x(), metrics.color_picker_panel_gap);
assert_eq!(map.width(), metrics.color_picker_map_size);
assert_eq!(map.y(), wheel.y());
}
#[test]
fn color_picker_compact_mode_uses_smaller_measurement() {
let regular = ColorPicker::new("Accent picker")
.theme(DefaultTheme::comfortable())
.desired_size();
let compact = ColorPicker::new("Accent picker")
.compact(true)
.show_alpha(false)
.desired_size();
let metrics = DefaultTheme::compact().metrics;
assert_eq!(
compact.width,
metrics.color_picker_wheel_size
+ metrics.color_picker_panel_gap
+ metrics.color_picker_right_panel_width
+ metrics.color_picker_content_inset * 2.0
);
assert!(compact.width < regular.width);
assert!(compact.height < regular.height);
}
#[test]
fn color_picker_theme_density_changes_default_measurement() {
let compact = ColorPicker::new("Accent picker")
.theme(DefaultTheme::compact())
.desired_size();
let comfortable = ColorPicker::new("Accent picker")
.theme(DefaultTheme::comfortable())
.desired_size();
let touch = ColorPicker::new("Accent picker")
.theme(DefaultTheme::touch())
.desired_size();
assert!(compact.width < comfortable.width);
assert!(compact.height < comfortable.height);
assert!(touch.width > comfortable.width);
assert!(touch.height > comfortable.height);
}
#[test]
fn color_picker_appearance_overrides_semantic_theme_defaults() -> Result<()> {
let appearance = ColorPickerColors {
checkerboard_light: Some(Color::rgba(0.91, 0.86, 0.78, 1.0)),
checkerboard_dark: Some(Color::rgba(0.66, 0.58, 0.48, 1.0)),
chrome_border: Some(Color::rgba(0.20, 0.30, 0.42, 0.61)),
plane_border: Some(Color::rgba(0.30, 0.20, 0.46, 0.62)),
bar_border: Some(Color::rgba(0.42, 0.25, 0.18, 0.63)),
marker_outer: Some(Color::rgba(0.98, 0.96, 0.90, 0.94)),
marker_dark: Some(Color::rgba(0.05, 0.07, 0.10, 0.88)),
marker_light: None,
sdr_marker: Some(Color::rgba(0.95, 0.92, 0.84, 0.38)),
hdr_divider: None,
};
let (mut runtime, window_id) = build_runtime(
ColorPicker::from_color("Accent picker", Color::rgba(0.84, 0.72, 0.18, 1.0))
.colors(appearance),
);
let output = runtime.render(window_id)?;
let fills = solid_fill_colors(&output);
let strokes = solid_stroke_colors(&output);
assert!(fills.contains(&appearance.checkerboard_light.unwrap()));
assert!(fills.contains(&appearance.checkerboard_dark.unwrap()));
assert!(fills.contains(&appearance.sdr_marker.unwrap()));
assert!(strokes.contains(&appearance.chrome_border.unwrap()));
assert!(strokes.contains(&appearance.plane_border.unwrap()));
assert!(strokes.contains(&appearance.bar_border.unwrap()));
assert!(strokes.contains(&appearance.marker_outer.unwrap()));
assert!(strokes.contains(&appearance.marker_dark.unwrap()));
Ok(())
}
#[test]
fn color_picker_header_contains_encoding_selector() {
let picker = ColorPicker::new("Accent picker");
let bounds = Rect::new(0.0, 0.0, 434.0, 448.0);
let header = picker.header_rect(bounds);
let encoding = picker.encoding_rect(bounds);
let previous = picker.previous_swatch_rect(bounds);
let map = picker.saturation_value_rect(bounds);
let rgb = picker.rgb_row_rect(bounds, 0);
assert!(header.contains(encoding.origin));
assert!(header.contains(Point::new(encoding.max_x(), encoding.max_y())));
assert!(encoding.x() >= previous.max_x() + picker.panel_gap() - 0.01);
assert!(encoding.max_x() <= header.max_x() + 0.01);
assert!(rgb.y() > map.max_y());
}
#[test]
fn color_picker_encoding_chevron_is_centered_in_selector() -> Result<()> {
let picker = ColorPicker::from_color("Accent picker", Color::rgba(0.25, 0.50, 0.75, 0.80));
let (mut runtime, window_id) = build_runtime(picker);
let output = runtime.render(window_id)?;
let bounds = Rect::new(
0.0,
0.0,
output.frame.viewport.width,
output.frame.viewport.height,
);
let layout_picker =
ColorPicker::from_color("Accent picker", Color::rgba(0.25, 0.50, 0.75, 0.80));
let encoding = layout_picker.encoding_rect(bounds);
let theme = DefaultTheme::default();
let chevron =
stroke_path_bounds_with_color_and_width(&output, theme.palette.placeholder, 1.4)
.into_iter()
.find(|rect| {
encoding.contains(Point::new(
rect.x() + rect.width() * 0.5,
rect.y() + rect.height() * 0.5,
)) && (rect.width() - 8.0).abs() < 0.75
&& (rect.height() - 5.0).abs() < 0.75
})
.expect("encoding selector chevron stroke should render");
let chevron_center_y = chevron.y() + chevron.height() * 0.5;
let selector_center_y = encoding.y() + encoding.height() * 0.5;
assert!(
(chevron_center_y - selector_center_y).abs() < 0.75,
"encoding chevron center {chevron_center_y} did not match selector center {selector_center_y}; chevron={chevron:?}, selector={encoding:?}"
);
Ok(())
}
#[test]
fn color_picker_saturation_value_plane_uses_hdr_slider_curve() {
let mut picker = ColorPicker::from_color(
"Accent picker",
Color::new(ColorSpace::LinearSrgb, 2.0, 0.65, 0.4, 1.0),
);
let bounds = Rect::new(0.0, 0.0, 434.0, 448.0);
let map = picker.saturation_value_rect(bounds);
picker.apply_position(
bounds,
ActiveChannel::SaturationValue,
Point::new(map.x() + map.width() * 0.5, map.y() + map.height() * 0.25),
);
assert!(
picker.value > 3.0 && picker.value < 4.0,
"75% HDR slider position should map logarithmically above 1.0, got {}",
picker.value
);
}
#[test]
fn color_picker_rgb_row_drag_updates_color_channels() -> Result<()> {
let changes = Rc::new(RefCell::new(Vec::new()));
let on_change = Rc::clone(&changes);
let (mut runtime, window_id) = build_runtime(
ColorPicker::from_color(
"Accent picker",
Color::new(ColorSpace::LinearSrgb, 2.0, 0.65, 0.4, 1.0),
)
.on_change(move |color| on_change.borrow_mut().push(color)),
);
let initial = runtime.render(window_id)?;
let red = initial
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Red"))
.expect("red channel semantics present")
.bounds;
let start = Point::new(red.x() + red.width() * 0.20, red.y() + red.height() * 0.5);
let end = Point::new(red.x() + red.width() * 0.80, red.y() + red.height() * 0.5);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, start, true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, end, true),
)?;
runtime.handle_event(window_id, primary_pointer(PointerEventKind::Up, end, false))?;
let changed_color = *changes.borrow().last().expect("rgb row emitted change");
assert!(
changed_color.red > 8.0,
"expected HDR red channel after RGB drag, got {}",
changed_color.red
);
assert_eq!(changed_color.space, ColorSpace::LinearSrgb);
Ok(())
}
#[test]
fn color_picker_numeric_rows_use_tabular_figures_and_end_alignment() -> Result<()> {
let color = Color::rgba(0.25, 0.50, 0.75, 0.80);
let (mut runtime, window_id) =
build_runtime(ColorPicker::from_color("Accent picker", color));
let output = runtime.render(window_id)?;
let run = text_run_for(&output, "0.250");
let bounds = Rect::new(
0.0,
0.0,
output.frame.viewport.width,
output.frame.viewport.height,
);
let picker = ColorPicker::from_color("Accent picker", color);
let row = picker.rgb_row_rect(bounds, 0);
let theme = DefaultTheme::default();
let expected_right = row.max_x() - theme.spacing;
assert!(
run.style
.features
.iter()
.any(|feature| feature.tag == FontFeature::TABULAR_FIGURES && feature.value == 1)
);
assert!((run.rect.max_x() - expected_right).abs() < 1.0);
assert!(run.rect.height() <= theme.text.xs.line_height + 0.01);
Ok(())
}
#[test]
fn color_picker_numeric_rows_preserve_tall_measurements_and_row_center() -> Result<()> {
let mut theme = DefaultTheme::default();
theme.text.xs = ThemeTextToken {
size: 28.0,
line_height: 12.0,
};
theme.metrics.color_picker_row_height = 48.0;
let color = Color::rgba(0.25, 0.50, 0.75, 0.80);
let (mut runtime, window_id) =
build_runtime(ColorPicker::from_color("Accent picker", color).theme(theme));
let output = runtime.render(window_id)?;
let label = text_run_for(&output, "R");
let value = text_run_for(&output, "0.250");
let layout = TextSystem::new()
.shape_text_run(&value, &FontRegistry::new())
.expect("color picker numeric value should shape");
let bounds = Rect::new(
0.0,
0.0,
output.frame.viewport.width,
output.frame.viewport.height,
);
let picker = ColorPicker::from_color("Accent picker", color).theme(theme);
let row = picker.rgb_row_rect(bounds, 0);
let row_center = row.y() + row.height() * 0.5;
let value_center = text_visual_center_for(&output, "0.250");
let value_slot = super::ColorSliderRowSlots::new(row, &theme).value;
assert_eq!(drawn_layout_for(&output, "0.250").lines().len(), 1);
assert!(value.rect.width() > value_slot.width());
assert!((value.rect.x() - value_slot.x()).abs() < 1.0);
assert!(
(value_center - row_center).abs() < 0.75,
"color picker numeric value center {value_center} did not match row center {row_center}; value rect {:?}, row {:?}, measurement {:?}",
value.rect,
row,
layout.measurement()
);
let label_center = text_visual_center_for(&output, "R");
let label_layout = TextSystem::new()
.shape_text_run(&label, &FontRegistry::new())
.expect("color picker channel label should shape");
assert!(
(value_center - label_center).abs() < 0.75,
"color picker numeric value center {value_center} and channel label center {label_center} should share a visual baseline; label rect {:?}, label measurement {:?}",
label.rect,
label_layout.measurement()
);
Ok(())
}
#[test]
fn color_picker_text_styles_follow_theme_xs_token() -> Result<()> {
let mut theme = DefaultTheme::default();
theme.text.xs = ThemeTextToken {
size: 13.5,
line_height: 31.0,
};
let (mut runtime, window_id) = build_runtime(
ColorPicker::from_color("Accent picker", Color::rgba(0.25, 0.50, 0.75, 0.80))
.theme(theme),
);
let output = runtime.render(window_id)?;
let label = text_run_for(&output, "R");
let value = text_run_for(&output, "0.250");
assert_text_run_uses_token(&label, theme.text.xs);
assert_text_run_uses_token(&value, theme.text.xs);
assert!(
value
.style
.features
.iter()
.any(|feature| feature.tag == FontFeature::TABULAR_FIGURES && feature.value == 1)
);
Ok(())
}
#[test]
fn color_picker_dropdown_and_fields_preserve_tall_text_measurements() -> Result<()> {
let mut theme = DefaultTheme::default();
theme.text.xs = ThemeTextToken {
size: 28.0,
line_height: 10.0,
};
theme.metrics.color_picker_top_bar_height = 56.0;
theme.metrics.color_picker_field_height = 46.0;
theme.metrics.color_picker_encoding_menu_row_height = 46.0;
let color = Color::new(ColorSpace::LinearSrgb, 2.0, 0.65, 0.4, 1.0);
let (mut runtime, window_id) =
build_runtime(ColorPicker::from_color("Accent picker", color).theme(theme));
let initial = runtime.render(window_id)?;
let color_range = initial
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Color range"))
.expect("color range semantics present")
.bounds;
let color_range_center = rect_center(color_range);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, color_range_center, true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Up, color_range_center, false),
)?;
let output = runtime.render(window_id)?;
let bounds = Rect::new(
0.0,
0.0,
output.frame.viewport.width,
output.frame.viewport.height,
);
let picker = ColorPicker::from_color("Accent picker", color).theme(theme);
let selector = picker.encoding_rect(bounds);
let menu_item = picker.encoding_option_rect(bounds, 0);
let hex = picker.hex_rect(bounds);
let selector_run = text_run_for(&output, "BT709 Linear");
let menu_run = text_run_for(&output, "sRGB");
let hex_run = text_run_for(&output, "HDR hex unavailable");
assert_text_run_uses_token(&selector_run, theme.text.xs);
assert_text_run_uses_token(&menu_run, theme.text.xs);
assert_text_run_uses_token(&hex_run, theme.text.xs);
assert_eq!(hex_run.style.color, theme.palette.placeholder);
assert!(
(text_visual_center_for(&output, "BT709 Linear") - rect_center(selector).y).abs()
< 0.75
);
assert!((text_visual_center_for(&output, "sRGB") - rect_center(menu_item).y).abs() < 0.75);
assert!(
(text_visual_center_for(&output, "HDR hex unavailable") - rect_center(hex).y).abs()
< 0.75
);
Ok(())
}
#[test]
fn color_picker_encoding_selector_uses_dropdown_options() -> Result<()> {
let changes = Rc::new(RefCell::new(Vec::new()));
let on_change = Rc::clone(&changes);
let (mut runtime, window_id) = build_runtime(
ColorPicker::from_color(
"Accent picker",
Color::new(ColorSpace::LinearSrgb, 2.0, 0.65, 0.4, 1.0),
)
.on_change(move |color| on_change.borrow_mut().push(color)),
);
let _ = runtime.render(window_id)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, Point::new(300.0, 40.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Up, Point::new(300.0, 40.0), false),
)?;
assert!(
changes.borrow().is_empty(),
"opening the encoding dropdown should not change color space"
);
let output = runtime.render(window_id)?;
let picker = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorPicker)
.expect("color picker semantics present after opening encoding dropdown");
let range = output
.semantics
.iter()
.find(|node| {
node.parent == Some(picker.id)
&& node.role == SemanticsRole::ComboBox
&& node.name.as_deref() == Some("Color range")
})
.expect("color range selector semantics present after opening dropdown");
assert_eq!(range.state.expanded, Some(true));
assert!(
output.semantics.iter().any(|node| {
node.role == SemanticsRole::MenuItem
&& node.name.as_deref() == Some("Display P3")
&& node.actions.contains(&SemanticsAction::Activate)
}),
"open color range dropdown should expose Display P3 option"
);
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, Point::new(300.0, 129.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Up, Point::new(300.0, 129.0), false),
)?;
let changed_color = *changes
.borrow()
.last()
.expect("encoding selector emitted change");
assert_eq!(changed_color.space, ColorSpace::DisplayP3);
let output = runtime.render(window_id)?;
let picker = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ColorPicker)
.expect("color picker semantics present after encoding change");
assert_eq!(
picker.description.as_deref(),
Some("Display P3 editing space; HDR range available")
);
Ok(())
}
}