mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
color-ui: ColorDisc::stored_as and HarmonyWheel::stored_as take the host's round trip through its own colour model, so a drag whose colour comes back moved by it (CMYK through a profile) is still the drag's; without it the disc keeps its 8-bit echo check: a host storing colours in another model had its own drags cancelled as outside changes
This commit is contained in:
1 parent
e36d3d0034
commit
efebc0cdac
3 files changed
+84
-16
No files matched your search
@@ -41,7 +41,7 @@ const SECTORS: u32 = 24;
|
||||
const PATCH_RINGS: u32 = 5;
|
||||
const SECTOR_DEGREES: f32 = 15.0;
|
||||
/// How far from where a drag put its marker, in radii, the host's colour may lie and still be the drag's: what an
|
||||
/// 8-bit or another model's round trip moves a colour by, two of the hundredths a drag reports its radial in.
|
||||
/// 8-bit round trip moves a colour by, two of the hundredths a drag reports its radial in.
|
||||
const ECHO_REACH: f32 = 0.02;
|
||||
|
||||
craft_ui::words! {
|
||||
@@ -191,7 +191,8 @@ struct Grab {
|
||||
/// ([`pressed`](ColorDiscOutput::pressed) reports it, for the host to select it); a press elsewhere grabs nothing.
|
||||
/// A drag moves the grabbed marker's hue (the pointer's angle, whole degrees) and saturation (its distance over the
|
||||
/// radius, whole percents, at most 1), one gesture; the marker's brightness stays, the host's slider sets it. A
|
||||
/// click is one `Set`. A colour of the host's that is not what the drag reported cancels the drag.
|
||||
/// click is one `Set`. A colour of the host's that is not what the drag reported, after the host's own round trip
|
||||
/// ([`stored_as`](ColorDisc::stored_as)), cancels the drag.
|
||||
/// - Keys: the markers are one Tab stop, which lands on the selected marker (the first without a selection); a press
|
||||
/// that grabs a marker focuses it. Left and Right change the focused marker's hue by 1 degree, Up and Down its
|
||||
/// saturation by 1 percent; Shift steps ten; each key is a `Set`. During a drag keys change nothing. Esc during a
|
||||
@@ -227,6 +228,7 @@ pub struct ColorDisc<'a> {
|
||||
/// The markers' accessible names, in order; a marker without one is "Color N".
|
||||
#[prop(into)]
|
||||
names: Vec<Label>,
|
||||
stored: Option<&'a dyn Fn(usize, Srgba) -> Srgba>,
|
||||
pressed: Option<usize>,
|
||||
grabbed: Option<usize>,
|
||||
clicked: Option<DiscHit>,
|
||||
@@ -246,6 +248,7 @@ impl<'a> ColorDisc<'a> {
|
||||
look: DiscLook::Smooth,
|
||||
radius: DiscRadius::Saturation,
|
||||
names: Vec::new(),
|
||||
stored: None,
|
||||
pressed: None,
|
||||
grabbed: None,
|
||||
clicked: None,
|
||||
@@ -255,6 +258,14 @@ impl<'a> ColorDisc<'a> {
|
||||
.ltr_axis()
|
||||
}
|
||||
|
||||
/// How the host stores marker `index`'s colours: `color` written in the host's own model (CMYK through a profile)
|
||||
/// and read back. A drag's colour that comes back moved by that round trip is still the drag's, not an outside
|
||||
/// change; without it the host is taken to keep the colour as given, to 8 bits.
|
||||
pub fn stored_as(mut self, stored: &'a dyn Fn(usize, Srgba) -> Srgba) -> Self {
|
||||
self.stored = Some(stored);
|
||||
self
|
||||
}
|
||||
|
||||
/// The disc's centre and radius in the area `rect`, `inset` inside its shorter side.
|
||||
pub fn geometry(rect: egui::Rect, inset: f32) -> (Pos2, f32) {
|
||||
(rect.center(), (rect.width().min(rect.height()) / 2.0 - inset).max(1.0))
|
||||
@@ -277,7 +288,11 @@ impl<'a> ColorDisc<'a> {
|
||||
|
||||
/// Marker `index` where the host has it: the radial channel is the one the radius shows.
|
||||
fn resting(&self, index: usize) -> DiscMove {
|
||||
let [hue, saturation, brightness] = self.hsb(index);
|
||||
self.at(index, self.hsb(index))
|
||||
}
|
||||
|
||||
/// Marker `index` at the hue, saturation and brightness `hsb`: the radial channel is the one the radius shows.
|
||||
fn at(&self, index: usize, [hue, saturation, brightness]: [f32; 3]) -> DiscMove {
|
||||
let radial = match self.radius {
|
||||
DiscRadius::Saturation => saturation,
|
||||
DiscRadius::Brightness => brightness,
|
||||
@@ -399,15 +414,20 @@ impl Component for ColorDisc<'_> {
|
||||
// A colour of the host's that changed and does not lie where the drag put the marker is an outside change. Only
|
||||
// the hue and the radial count: the host keeps the channel the radius does not show as it likes (from the
|
||||
// colour before, or from the press against the drift of 8-bit colours) and stores the colour in its own
|
||||
// model. A colour whose 8 bits lose its hue (black on a saturation disc) is the drag's when it is the 8-bit
|
||||
// colour the drag made of the colour before.
|
||||
// model, whose round trip the host gives. A colour whose 8 bits lose its hue (black on a saturation disc) is
|
||||
// the drag's when it is the 8-bit colour the drag made of the colour before.
|
||||
if running && let Some(marker) = grabbed.filter(|marker| *marker < self.markers.len()) {
|
||||
let last = cx.memory::<Grab>().last.replace(self.markers[marker]);
|
||||
if let Some(moved) = cx.value().filter(|moved| moved.marker == marker)
|
||||
&& let Some(last) = last.filter(|last| *last != self.markers[marker])
|
||||
{
|
||||
let echo = HsbModel.to_srgb(&ModelValues::new("hsb", &self.values_on(last, moved)).values);
|
||||
if !lies_at(self.resting(marker), moved) && !echo.same_rgb8(self.markers[marker]) {
|
||||
let mut echo = HsbModel.to_srgb(&ModelValues::new("hsb", &self.values_on(last, moved)).values);
|
||||
let mut sent = *moved;
|
||||
if let Some(stored) = self.stored {
|
||||
echo = stored(marker, echo);
|
||||
sent = self.at(marker, hsb_of(echo));
|
||||
}
|
||||
if !lies_at(self.resting(marker), &sent) && !echo.same_rgb8(self.markers[marker]) {
|
||||
cx.cancel(CancelReason::HostChanged);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,12 +139,13 @@ pub struct HarmonyWheel<'a> {
|
||||
base: Option<Option<usize>>,
|
||||
look: DiscLook,
|
||||
radius: DiscRadius,
|
||||
stored: Option<&'a dyn Fn(usize, Srgba) -> Srgba>,
|
||||
}
|
||||
|
||||
impl<'a> HarmonyWheel<'a> {
|
||||
/// A wheel named `label` over `colors`.
|
||||
pub fn new(label: impl Into<Label>, colors: &'a [Srgba]) -> Self {
|
||||
HarmonyWheel { label: label.into(), colors, selected: None, base: None, look: DiscLook::Smooth, radius: DiscRadius::Saturation }
|
||||
HarmonyWheel { label: label.into(), colors, selected: None, base: None, look: DiscLook::Smooth, radius: DiscRadius::Saturation, stored: None }
|
||||
}
|
||||
|
||||
/// Marks the colour at this index as selected; none by default.
|
||||
@@ -172,9 +173,16 @@ impl<'a> HarmonyWheel<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// How the host stores colour `index`: `color` written in its own model and read back, so a drag survives that
|
||||
/// round trip ([`ColorDisc::stored_as`]).
|
||||
pub fn stored_as(mut self, stored: &'a dyn Fn(usize, Srgba) -> Srgba) -> Self {
|
||||
self.stored = Some(stored);
|
||||
self
|
||||
}
|
||||
|
||||
/// Shows the wheel and the Brightness row.
|
||||
pub fn show(self, ui: &mut Ui) -> HarmonyWheelOutput {
|
||||
let HarmonyWheel { label, colors, selected, base, look, radius } = self;
|
||||
let HarmonyWheel { label, colors, selected, base, look, radius, stored } = self;
|
||||
let words = Env::of(ui).words::<HarmonyWords>();
|
||||
let selected = selected.filter(|&i| i < colors.len());
|
||||
let base = base.unwrap_or(selected).filter(|&i| i < colors.len());
|
||||
@@ -196,6 +204,9 @@ impl<'a> HarmonyWheel<'a> {
|
||||
if let Some(selected) = selected {
|
||||
disc = disc.chosen(selected);
|
||||
}
|
||||
if let Some(stored) = stored {
|
||||
disc = disc.stored_as(stored);
|
||||
}
|
||||
let disc = disc.look(look).radius(radius).level(level).names(names.clone()).show(ui);
|
||||
(pressed, clicked, context) = (disc.pressed, disc.clicked, disc.context);
|
||||
let grabbed = disc.grabbed;
|
||||
|
||||
@@ -524,16 +524,23 @@ fn eight_bit(color: Srgba) -> Srgba {
|
||||
}
|
||||
|
||||
/// Drags the first marker of a brightness disc 60 points left in 20 steps, the host applying each update and the commit
|
||||
/// as `apply` makes them of the marker's colour at the press and its colour now. Returns how many updates arrived and
|
||||
/// how the drag ended (`None`: committed).
|
||||
fn brightness_disc_drag(apply: impl Fn(Srgba, Srgba, &DiscMove) -> Srgba + 'static) -> (usize, Vec<Option<CancelReason>>) {
|
||||
/// as `apply` makes them of the marker's colour at the press and its colour now, and giving the disc its round trip
|
||||
/// `stored`. Returns how many updates arrived and how the drag ended (`None`: committed).
|
||||
fn brightness_disc_drag(
|
||||
stored: Option<fn(usize, Srgba) -> Srgba>,
|
||||
apply: impl Fn(Srgba, Srgba, &DiscMove) -> Srgba + 'static,
|
||||
) -> (usize, Vec<Option<CancelReason>>) {
|
||||
let colours = Rc::new(RefCell::new(vec![eight_bit(Srgba::rgb(0.9, 0.3, 0.2)), Srgba::rgb(0.2, 0.5, 0.8)]));
|
||||
let start = colours.borrow()[0];
|
||||
let (updates, ends) = (Rc::new(Cell::new(0)), Rc::new(RefCell::new(Vec::new())));
|
||||
let (shown, u, e) = (colours.clone(), updates.clone(), ends.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let markers = shown.borrow().clone();
|
||||
let out = ColorDisc::new("Harmony", &markers, Bind::kept()).radius(DiscRadius::Brightness).show(ui);
|
||||
let mut disc = ColorDisc::new("Harmony", &markers, Bind::kept()).radius(DiscRadius::Brightness);
|
||||
if let Some(stored) = &stored {
|
||||
disc = disc.stored_as(stored);
|
||||
}
|
||||
let out = disc.show(ui);
|
||||
for event in out.edit.events.iter() {
|
||||
let moved = match event {
|
||||
EditEvent::Update { value, .. } => {
|
||||
@@ -575,7 +582,7 @@ const DRAG_STEPS: usize = 20;
|
||||
/// what the drag made of the colour before, not an outside change that cancels it.
|
||||
#[trial]
|
||||
fn a_brightness_disc_drag_the_host_applies_in_8_bit_colours_runs_to_its_release() {
|
||||
let (updates, ends) = brightness_disc_drag(|_, now, moved| {
|
||||
let (updates, ends) = brightness_disc_drag(None, |_, now, moved| {
|
||||
let v = HsbModel.from_srgb(now).values;
|
||||
eight_bit(HsbModel.to_srgb(&ModelValues::new("hsb", &[moved.hue, v[1], moved.radial * 100.0]).values))
|
||||
});
|
||||
@@ -587,7 +594,7 @@ fn a_brightness_disc_drag_the_host_applies_in_8_bit_colours_runs_to_its_release(
|
||||
/// colours, puts the marker where the drag reported it: the drag runs to its release.
|
||||
#[trial]
|
||||
fn a_brightness_disc_drag_whose_host_keeps_the_saturation_from_the_press_runs_to_its_release() {
|
||||
let (updates, ends) = brightness_disc_drag(|start, _, moved| {
|
||||
let (updates, ends) = brightness_disc_drag(None, |start, _, moved| {
|
||||
let v = HsbModel.from_srgb(start).values;
|
||||
eight_bit(HsbModel.to_srgb(&ModelValues::new("hsb", &[moved.hue, v[1], moved.radial * 100.0]).values))
|
||||
});
|
||||
@@ -600,7 +607,7 @@ fn a_brightness_disc_drag_whose_host_keeps_the_saturation_from_the_press_runs_to
|
||||
#[trial]
|
||||
fn a_brightness_disc_drag_whose_host_takes_another_colour_is_cancelled() {
|
||||
let applied = Cell::new(0);
|
||||
let (_, ends) = brightness_disc_drag(move |_, now, moved| {
|
||||
let (_, ends) = brightness_disc_drag(None, move |_, now, moved| {
|
||||
applied.set(applied.get() + 1);
|
||||
if applied.get() < DRAG_STEPS / 2 {
|
||||
let v = HsbModel.from_srgb(now).values;
|
||||
@@ -612,6 +619,36 @@ fn a_brightness_disc_drag_whose_host_takes_another_colour_is_cancelled() {
|
||||
assert_eq!(ends, [Some(CancelReason::HostChanged)], "an outside change cancels the drag");
|
||||
}
|
||||
|
||||
/// A colour as a host stores it whose model's round trip darkens every colour by a tenth (as a CMYK profile's can), to
|
||||
/// 8 bits.
|
||||
fn darkened(_: usize, color: Srgba) -> Srgba {
|
||||
eight_bit(Srgba::rgb(color.r * 0.9, color.g * 0.9, color.b * 0.9))
|
||||
}
|
||||
|
||||
/// Tb-10 (3): a host that stores each colour in another model, whose round trip moves it further than an 8-bit one,
|
||||
/// gives the disc that round trip: its drag runs to its release, an outside change still cancels it, and without the
|
||||
/// round trip the moved colour reads as an outside change.
|
||||
#[trial]
|
||||
fn a_disc_drag_through_the_hosts_lossy_model_runs_to_its_release() {
|
||||
let through_model = |_: Srgba, now: Srgba, moved: &DiscMove| {
|
||||
let v = HsbModel.from_srgb(now).values;
|
||||
darkened(0, HsbModel.to_srgb(&ModelValues::new("hsb", &[moved.hue, v[1], moved.radial * 100.0]).values))
|
||||
};
|
||||
let (updates, ends) = brightness_disc_drag(Some(darkened), through_model);
|
||||
assert!(updates >= DRAG_STEPS / 2, "the drag updated as it moved: {updates}");
|
||||
assert_eq!(ends, [None], "the drag commits, never cancelled");
|
||||
|
||||
let (_, ends) = brightness_disc_drag(None, through_model);
|
||||
assert_eq!(ends, [Some(CancelReason::HostChanged)], "without the host's round trip the stored colour is another");
|
||||
|
||||
let applied = Cell::new(0);
|
||||
let (_, ends) = brightness_disc_drag(Some(darkened), move |start, now, moved| {
|
||||
applied.set(applied.get() + 1);
|
||||
if applied.get() < DRAG_STEPS / 2 { through_model(start, now, moved) } else { Srgba::rgb(0.1, 0.8, 0.3) }
|
||||
});
|
||||
assert_eq!(ends, [Some(CancelReason::HostChanged)], "an outside change still cancels the drag");
|
||||
}
|
||||
|
||||
/// Tb-8 (2): a marker of a linked harmony wheel dragged a quarter turn, a few points at a time, by a host that moves
|
||||
/// every colour from the colours at the drag's start (`move_on_wheel`, linked) and keeps 8-bit colours, as the recolor
|
||||
/// dialog does: the drag runs to its release and the other colour turns with it.
|
||||
|
||||
Reference in new issue
Block a user