mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
color-ui: the Color panel rounds only what its channel fields show; an edit of one channel leaves the others unrounded
This commit is contained in:
@@ -1031,7 +1031,7 @@ impl Body<'_> {
|
||||
});
|
||||
}
|
||||
|
||||
/// The model's values for the edited colour, whole numbers as Photoshop's fields.
|
||||
/// The model's values for the edited colour, unrounded: an edit of one channel leaves the others as they are.
|
||||
fn values(&self, model: &dyn ColorModel) -> ModelValues {
|
||||
let color = self.color();
|
||||
// The host's own values of the colour (typed CMYK, a working space's numbers) stand when they give the colour.
|
||||
@@ -1040,12 +1040,7 @@ impl Body<'_> {
|
||||
.iter()
|
||||
.chain(given.as_ref())
|
||||
.find(|v| v.model == model.id() && model.to_srgb(&v.values).same_rgb8(color));
|
||||
let mut values = kept.copied().unwrap_or_else(|| self.read(model, color));
|
||||
for (slot, ch) in values.values.iter_mut().zip(model.channels()) {
|
||||
let scale = 10f32.powi(i32::from(ch.decimals));
|
||||
*slot = if slot.is_finite() { (*slot * scale).round() / scale } else { 0.0 };
|
||||
}
|
||||
values
|
||||
finite_values(kept.copied().unwrap_or_else(|| self.read(model, color)))
|
||||
}
|
||||
|
||||
/// `color` in `model`. HSB takes the hue of a grey, and the hue and saturation of black, from the last HSB values
|
||||
@@ -1136,11 +1131,10 @@ impl Body<'_> {
|
||||
track_values.values[2] = 100.0;
|
||||
}
|
||||
let track = model.track(i, &track_values);
|
||||
let row = ChannelRow::new(channel_name(model, i, &[model]), Bind::given(f64::from(current.values[i]) * scale), rule)
|
||||
.short(ch.label)
|
||||
.unit(unit)
|
||||
.track(track)
|
||||
.show(ui);
|
||||
// The field shows the channel's places, as Photoshop's; the edits start from the unrounded values.
|
||||
let places = 10f64.powi(i32::from(rule.decimals));
|
||||
let shown = (f64::from(current.values[i]) * scale * places).round() / places;
|
||||
let row = ChannelRow::new(channel_name(model, i, &[model]), Bind::given(shown), rule).short(ch.label).unit(unit).track(track).show(ui);
|
||||
for event in row.into_iter().flat_map(|row| row.events) {
|
||||
self.channel_event(model, current, i, event.map(|x| x / scale));
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ use craft_color_ui::ui::{
|
||||
SwatchLibrary, SwatchPalette, SwatchesCommand, SwatchesMenu, SwatchesPanel, SwatchesState, SwatchesWords, swatch_rows,
|
||||
};
|
||||
use craft_host::target::Os;
|
||||
use craft_theme::Srgba;
|
||||
use craft_theme::{ColorModel as _, HsbModel, Srgba};
|
||||
use craft_ui::choice::SwatchTable;
|
||||
use craft_ui::contract::Role;
|
||||
use craft_ui::contract::tooltip::DELAY;
|
||||
@@ -176,6 +176,24 @@ fn the_slider_modes_show_a_channel_on_one_line() {
|
||||
assert!(k(&rig).bottom() <= short.y + 0.5 && k(&rig).top() >= 0.0, "CMYK in {short:?}: the K row scrolls into view ({:?})", k(&rig));
|
||||
}
|
||||
|
||||
/// A step of one channel leaves the others as they were: the fields round what they show, the edit does not.
|
||||
#[trial]
|
||||
fn a_channel_step_keeps_the_other_channels_unrounded() {
|
||||
let start = Srgba::rgb(0.2, 0.5, 0.7);
|
||||
let mut rig = PanelRig::new(ColorPanelMode::HsbSliders);
|
||||
rig.set_colors([start, BLUE]);
|
||||
let hue = rig.fixture.by_role(Role::SLIDER).into_iter().next().expect("the hue slider");
|
||||
rig.fixture.access(AccessActionKind::Focus, &hue).settle();
|
||||
rig.fixture.key(Key::ArrowRight, Modifiers::NONE).settle();
|
||||
let edits = rig.edits.borrow();
|
||||
let edited = edits.iter().rev().find_map(|(_, edit)| edit.value()).expect("the step's edit").color;
|
||||
let [before, after] = [start, edited].map(|color| HsbModel.from_srgb(color).values);
|
||||
assert_ne!(before[0], after[0], "the hue moved");
|
||||
for channel in [1, 2] {
|
||||
assert!((before[channel] - after[channel]).abs() < 0.01, "channel {channel} kept ({before:?}, {after:?})");
|
||||
}
|
||||
}
|
||||
|
||||
/// The rect of the FillStroke spectrum in Web Color for a foreground of `color`.
|
||||
fn spectrum_rect(color: Srgba) -> egui::Rect {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
|
||||
Reference in New Issue
Block a user