Color panel: the RGB rows show and edit the working space's own numbers through a working-RGB colour model the panel takes; the slider test starts its red off the thumb a mid-track click lands on

This commit is contained in:
bflatastic
2026-11-23 11:36:37 +07:00
parent 6e871532e1
commit 14b2eafb4e
2 changed files with 34 additions and 5 deletions
+29 -1
View File
@@ -20,7 +20,7 @@ use craft::color_ui::ui::{
};
use craft::color_ui::web_safe;
use craft::keymap::CommandRef;
use craft::theme::{ModelValues, Srgba};
use craft::theme::{Channel, ColorModel, DefaultOn, MAX_CHANNELS, ModelValues, RgbModel, Srgba};
use craft::ui::contract::{Bind, CommandSet};
use craft::ui::menu::PanelRow;
use craft::ui::runtime::commands::CommandStates;
@@ -101,6 +101,33 @@ fn srgba(c: &Color) -> Srgba {
Srgba::rgb(r, g, b)
}
/// RGB in the active working space, 0 to 255: the panel's RGB rows show and edit the stored components, so a wide
/// working space shows its own numbers rather than sRGB's.
struct WorkingRgb;
impl ColorModel for WorkingRgb {
fn id(&self) -> &'static str {
RgbModel.id()
}
fn name(&self) -> &'static str {
RgbModel.name()
}
fn channels(&self) -> &[Channel] {
static RGB: RgbModel = RgbModel;
RGB.channels()
}
fn from_srgb(&self, c: Srgba) -> ModelValues {
let [r, g, b] = cms::active().srgb_to_rgb([c.r, c.g, c.b]);
model_values(&Color::rgb(r, g, b))
}
fn to_srgb(&self, v: &[f32; MAX_CHANNELS]) -> Srgba {
srgba(&Color::rgb(v[0] / 255.0, v[1] / 255.0, v[2] / 255.0))
}
fn default_on(&self) -> DefaultOn {
RgbModel.default_on()
}
}
/// The colour's values in its own model (working RGB 0 to 255, CMYK %, K %, Lab L*a*b*), which the
/// framework's rows show and edit.
pub fn model_values(c: &Color) -> ModelValues {
@@ -537,6 +564,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
// The drag keeps a recent colour's own model for the Swatches panel.
.recent_payload(move |i| stored.get(i).copied())
.gamut(printable)
.working_rgb(&WorkingRgb)
.show(ui);
// PLAN: a click on a recent chip reports only its sRGB, so a CMYK recent colour paints as RGB; co.K18 needs the edit to name the chip.
apply_with(app, &tgt, &out);
@@ -130,7 +130,8 @@ fn picker_rgb_fields_show_working_values() {
}
}
/// A panel slider click changes one native component and makes one undo step.
/// A panel slider click changes one native component and makes one undo step. The click lands mid-track, where the
/// thumb of a red of 0.5 would sit and move nothing, so the red starts elsewhere.
#[test]
fn color_panel_slider_preserves_native_channels_and_undo() {
use egui_kittest::kittest::Queryable;
@@ -139,15 +140,15 @@ fn color_panel_slider_preserves_native_channels_and_undo() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
app.run("paint.setFill", json!({"color": [0.5, 0.3, 0.2]})).unwrap();
app.run("paint.setFill", json!({"color": [0.4, 0.3, 0.2]})).unwrap();
let mut harness = harness(egui::vec2(320.0, 400.0), app, |ui, app| show(app, ui));
frames(&mut harness, 3);
harness.get_by_role_and_label(egui::accesskit::Role::Slider, "Red").click();
frames(&mut harness, 3);
let color = harness.state().session.proxy_paints().0.color().unwrap();
let Color::Rgb { r, .. } = color else { panic!("expected RGB") };
assert_ne!(r, 0.5, "the slider was edited");
assert_ne!(r, 0.4, "the slider was edited");
native(color, [r, 0.3, 0.2]);
harness.state_mut().run("edit.undo", json!({})).unwrap();
native(harness.state().session.proxy_paints().0.color().unwrap(), [0.5, 0.3, 0.2]);
native(harness.state().session.proxy_paints().0.color().unwrap(), [0.4, 0.3, 0.2]);
}