mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
color-ui: a disc drag takes the host's colour as its echo when it lies where the drag put the marker, so a host that keeps the unshown channel from the press or stores its own colour model is not cancelled as an outside change
This commit is contained in:
@@ -40,6 +40,9 @@ const FAR: f32 = 10.0;
|
||||
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.
|
||||
const ECHO_REACH: f32 = 0.02;
|
||||
|
||||
craft_ui::words! {
|
||||
/// The words of colour discs.
|
||||
@@ -393,16 +396,18 @@ impl Component for ColorDisc<'_> {
|
||||
let grabbed = cx.memory::<Grab>().marker;
|
||||
self.grabbed = grabbed;
|
||||
let running = cx.gesture().is_some();
|
||||
// A colour of the host's that changed and is not what the drag reported makes of the colour before is an
|
||||
// outside change. The channel the radius does not show comes from the colour before, as the host's own
|
||||
// change keeps it: read back from an 8-bit colour, saturation drifts.
|
||||
// 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.
|
||||
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 !echo.same_rgb8(self.markers[marker]) {
|
||||
if !lies_at(self.resting(marker), moved) && !echo.same_rgb8(self.markers[marker]) {
|
||||
cx.cancel(CancelReason::HostChanged);
|
||||
}
|
||||
}
|
||||
@@ -564,6 +569,12 @@ fn moved_to(base: DiscMove, center: Pos2, radius: f32, pos: Pos2) -> DiscMove {
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether `at` lies within `ECHO_REACH` of `moved` on the disc.
|
||||
fn lies_at(at: DiscMove, moved: &DiscMove) -> bool {
|
||||
let point = |m: &DiscMove| Vec2::angled(m.hue.to_radians()) * m.radial;
|
||||
(point(&at) - point(moved)).length() <= ECHO_REACH
|
||||
}
|
||||
|
||||
/// Hue, saturation and brightness (0 to 1) of `color`.
|
||||
fn hsb_of(color: Srgba) -> [f32; 3] {
|
||||
let v = HsbModel.from_srgb(color).values;
|
||||
|
||||
@@ -517,15 +517,18 @@ fn color_hud_picks_at_an_injected_move() {
|
||||
assert_eq!(count(|event| matches!(event, EditEvent::Commit { .. })), 1, "the injected release commits");
|
||||
}
|
||||
|
||||
/// A drag on a brightness disc whose host keeps 8-bit colours runs to its release: the host's colour, read back, is
|
||||
/// 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 eight_bit = |color: Srgba| {
|
||||
let [r, g, b, _] = color.to_rgba().0;
|
||||
Srgba::rgb(f32::from(r) / 255.0, f32::from(g) / 255.0, f32::from(b) / 255.0)
|
||||
};
|
||||
/// A colour as a host that keeps 8-bit colours stores it.
|
||||
fn eight_bit(color: Srgba) -> Srgba {
|
||||
let [r, g, b, _] = color.to_rgba().0;
|
||||
Srgba::rgb(f32::from(r) / 255.0, f32::from(g) / 255.0, f32::from(b) / 255.0)
|
||||
}
|
||||
|
||||
/// 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>>) {
|
||||
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| {
|
||||
@@ -548,9 +551,7 @@ fn a_brightness_disc_drag_the_host_applies_in_8_bit_colours_runs_to_its_release(
|
||||
_ => continue,
|
||||
};
|
||||
let mut all = shown.borrow_mut();
|
||||
let v = HsbModel.from_srgb(all[moved.marker]).values;
|
||||
let hsb = [moved.hue, v[1], moved.radial * 100.0];
|
||||
all[moved.marker] = eight_bit(HsbModel.to_srgb(&ModelValues::new("hsb", &hsb).values));
|
||||
all[moved.marker] = apply(start, all[moved.marker], moved);
|
||||
}
|
||||
});
|
||||
fixture.room(vec2(320.0, 240.0));
|
||||
@@ -558,12 +559,55 @@ fn a_brightness_disc_drag_the_host_applies_in_8_bit_colours_runs_to_its_release(
|
||||
let from = fixture.by_label("Color 1").expect("the first marker").center();
|
||||
fixture.frame(vec![craft_ui::testing::pointer::moved(from)]);
|
||||
fixture.frame(vec![craft_ui::testing::pointer::press(from)]);
|
||||
let steps = 20;
|
||||
for step in 1..=steps {
|
||||
for step in 1..=DRAG_STEPS {
|
||||
fixture.frame(vec![craft_ui::testing::pointer::moved(from - vec2(3.0 * step as f32, 0.0))]);
|
||||
}
|
||||
fixture.frame(vec![craft_ui::testing::pointer::release(from - vec2(3.0 * steps as f32, 0.0))]);
|
||||
fixture.frame(vec![craft_ui::testing::pointer::release(from - vec2(3.0 * DRAG_STEPS as f32, 0.0))]);
|
||||
fixture.settle();
|
||||
assert!(updates.get() >= steps / 2, "the drag updated as it moved: {}", updates.get());
|
||||
assert_eq!(*ends.borrow(), [None], "the drag commits, never cancelled");
|
||||
let ends = ends.borrow().clone();
|
||||
(updates.get(), ends)
|
||||
}
|
||||
|
||||
/// The moves of `brightness_disc_drag`.
|
||||
const DRAG_STEPS: usize = 20;
|
||||
|
||||
/// A drag on a brightness disc whose host keeps 8-bit colours runs to its release: the host's colour, read back, is
|
||||
/// 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 v = HsbModel.from_srgb(now).values;
|
||||
eight_bit(HsbModel.to_srgb(&ModelValues::new("hsb", &[moved.hue, v[1], moved.radial * 100.0]).values))
|
||||
});
|
||||
assert!(updates >= DRAG_STEPS / 2, "the drag updated as it moved: {updates}");
|
||||
assert_eq!(ends, [None], "the drag commits, never cancelled");
|
||||
}
|
||||
|
||||
/// A host that keeps the channel the radius does not show from the colour at the press, against the drift of 8-bit
|
||||
/// 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 v = HsbModel.from_srgb(start).values;
|
||||
eight_bit(HsbModel.to_srgb(&ModelValues::new("hsb", &[moved.hue, v[1], moved.radial * 100.0]).values))
|
||||
});
|
||||
assert!(updates >= DRAG_STEPS / 2, "the drag updated as it moved: {updates}");
|
||||
assert_eq!(ends, [None], "the drag commits, never cancelled");
|
||||
}
|
||||
|
||||
/// A host whose colour goes somewhere other than where the drag put the marker, partway through the drag, changed it
|
||||
/// from outside: the drag is cancelled.
|
||||
#[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| {
|
||||
applied.set(applied.get() + 1);
|
||||
if applied.get() < DRAG_STEPS / 2 {
|
||||
let v = HsbModel.from_srgb(now).values;
|
||||
eight_bit(HsbModel.to_srgb(&ModelValues::new("hsb", &[moved.hue, v[1], moved.radial * 100.0]).values))
|
||||
} else {
|
||||
Srgba::rgb(0.1, 0.8, 0.3)
|
||||
}
|
||||
});
|
||||
assert_eq!(ends, [Some(CancelReason::HostChanged)], "an outside change cancels the drag");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user