use gpui::{
AnyElement, Context, ElementId, KeyDownEvent, MouseButton, MouseDownEvent, Window, div, point,
prelude::*, px,
};
use crate::color::oklch_to_color;
use crate::controls::{SliderTrack, slider, track_probe};
use crate::keyboard::{self, Key, Orientation};
use crate::lighting;
use crate::palette::Palette;
use crate::state::{ComboId, ControlHost, MOVE, TrackAxis};
pub const MAX_CHROMA: f64 = 0.16;
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Oklch {
pub hue: f64,
pub chroma: f64,
pub lightness: f64,
}
impl Oklch {
pub fn new(hue: f64, chroma: f64, lightness: f64) -> Self {
Self {
hue: hue.rem_euclid(360.0),
chroma: chroma.clamp(0.0, MAX_CHROMA),
lightness: lightness.clamp(0.0, 1.0),
}
}
pub fn to_rgba(self) -> gpui::Rgba {
oklch_to_color(self.lightness, self.chroma, self.hue)
}
}
pub fn hue_slider<V: ControlHost>(
id: ComboId,
hue: f64,
palette: Palette,
view: &V,
cx: &mut Context<V>,
) -> impl IntoElement {
slider(
id,
(hue.rem_euclid(360.0) / 360.0) as f32,
SliderTrack::Continuous,
palette,
view,
cx,
)
}
pub fn color_pad<V: ControlHost>(
id: ComboId,
color: Oklch,
height: f32,
palette: Palette,
view: &V,
cx: &mut Context<V>,
) -> impl IntoElement {
const COLUMNS: usize = 40;
const ROWS: usize = 12;
let weak = cx.entity().downgrade();
let state = view.control_state();
let hue = color.hue;
let marker_x_key = (color.chroma / MAX_CHROMA) as f32;
let marker_y_key = 1.0 - color.lightness as f32;
let arrows = cx.listener(move |this, event: &KeyDownEvent, _window, cx| {
let Some(key) = keyboard::key(event) else {
return;
};
let across = keyboard::nudge(key, Orientation::Horizontal, marker_x_key, 0.025);
let up = keyboard::nudge(key, Orientation::Vertical, 1.0 - marker_y_key, 0.025);
let Some(at) = (match (across, up) {
(Some(x), _) => Some(point(x, marker_y_key)),
(_, Some(up)) => Some(point(marker_x_key, 1.0 - up)),
_ => None,
}) else {
return;
};
cx.stop_propagation();
this.track_dragged(id, at, cx);
cx.notify();
});
let mut columns: Vec<AnyElement> = Vec::with_capacity(COLUMNS);
for column in 0..COLUMNS {
let chroma = (column as f64 + 0.5) / COLUMNS as f64 * MAX_CHROMA;
let mut cells: Vec<AnyElement> = Vec::with_capacity(ROWS);
for row in 0..ROWS {
let top = 1.0 - row as f64 / ROWS as f64;
let bottom = 1.0 - (row + 1) as f64 / ROWS as f64;
cells.push(
div()
.flex_1()
.w_full()
.bg(gpui::linear_gradient(
180.0,
gpui::linear_color_stop(oklch_to_color(top, chroma, hue), 0.0),
gpui::linear_color_stop(oklch_to_color(bottom, chroma, hue), 1.0),
))
.into_any_element(),
);
}
columns.push(
div()
.flex_1()
.h_full()
.flex()
.flex_col()
.children(cells)
.into_any_element(),
);
}
let (marker_x, marker_y) = if state.is_dragging(id) {
(
state.snap(format!("{id}-marker-x"), marker_x_key),
state.snap(format!("{id}-marker-y"), marker_y_key),
)
} else {
(
state.tween(format!("{id}-marker-x"), marker_x_key, MOVE),
state.tween(format!("{id}-marker-y"), marker_y_key, MOVE),
)
};
div()
.id(id)
.h(px(height))
.w_full()
.relative()
.overflow_hidden()
.rounded(px(6.0))
.border_1()
.border_color(palette.field_border)
.shadow(lighting::recessed(palette.is_dark))
.cursor_crosshair()
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, event: &MouseDownEvent, _window, cx| {
this.control_state_mut()
.begin_track_drag(id, TrackAxis::Both, None);
if let Some((_, at)) = this.control_state().track_ratio_at(event.position) {
this.track_dragged(id, at, cx);
}
cx.notify();
}),
)
.child(track_probe::<V>(id, weak))
.child(div().absolute().inset_0().flex().children(columns))
.child(
div()
.relative()
.child(
keyboard::ring(ElementId::Name(format!("{id}-marker").into()), 6.0, palette)
.on_key_down(arrows),
)
.absolute()
.left(gpui::relative(marker_x))
.top(gpui::relative(marker_y))
.ml(px(-6.0))
.mt(px(-6.0))
.w(px(12.0))
.h(px(12.0))
.rounded_full()
.border_2()
.border_color(gpui::white())
.shadow(vec![lighting::glow(
crate::color::rgba(0.0, 0.0, 0.0, 1.0),
0.45,
2.0,
)]),
)
}
pub fn swatch_grid<V: ControlHost>(
id: &'static str,
swatches: &[Oklch],
selected: Option<Oklch>,
size: f32,
palette: Palette,
cx: &mut Context<V>,
on_pick: impl Fn(&mut V, Oklch, &mut Window, &mut Context<V>) + 'static,
) -> impl IntoElement {
let on_pick = std::rc::Rc::new(on_pick);
let mut cells: Vec<AnyElement> = Vec::with_capacity(swatches.len());
for (index, swatch) in swatches.iter().enumerate() {
let cx: &mut Context<V> = &mut *cx;
let on_pick = on_pick.clone();
let key_pick = on_pick.clone();
let swatch = *swatch;
let active = selected.is_some_and(|current| {
(current.hue - swatch.hue).abs() < 0.5
&& (current.chroma - swatch.chroma).abs() < 0.005
&& (current.lightness - swatch.lightness).abs() < 0.005
});
cells.push(
div()
.id(ElementId::NamedInteger(
format!("{id}-swatch").into(),
index as u64,
))
.w(px(size))
.h(px(size))
.flex_none()
.rounded(px(5.0))
.bg(swatch.to_rgba())
.border_2()
.border_color(if active {
palette.accent
} else {
lighting::rim(swatch.to_rgba(), palette.is_dark)
})
.shadow(lighting::raised(palette.is_dark))
.relative()
.child(
keyboard::ring(
ElementId::NamedInteger(format!("{id}-swatch-ring").into(), index as u64),
5.0,
palette,
)
.on_key_down(cx.listener(
move |this, event: &KeyDownEvent, window, cx| {
if keyboard::key(event) == Some(Key::Activate) {
cx.stop_propagation();
key_pick(this, swatch, window, cx);
cx.notify();
}
},
)),
)
.cursor_pointer()
.on_click(cx.listener(move |this, _event, window, cx| {
on_pick(this, swatch, window, cx);
cx.notify();
}))
.into_any_element(),
);
}
div().flex().flex_wrap().gap(px(6.0)).children(cells)
}
pub fn hue_wheel(lightness: f64, chroma: f64) -> Vec<Oklch> {
(0..12)
.map(|step| Oklch::new(step as f64 * 30.0, chroma, lightness))
.collect()
}