Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions crates/ui-kit/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,7 @@ fn color_popover_with_opacity(ui: &mut Ui, rgb: &mut [u8; 3], mut opacity: Optio
}
});
} else {
let spin = if linear {
Spin { max: 1.0, step: 0.01, speed: 0.003, decimals: 3 }
} else {
Spin { max: 255.0, step: 1.0, speed: 0.5, decimals: 0 }
};
let spin = Spin::channel(linear);
ui.columns(3, |columns| {
// Named like H, S and V: "R" alone doesn't say red in every language (#10201).
for ((ui, channel), (name, tip)) in
Expand All @@ -229,18 +225,18 @@ fn color_popover_with_opacity(ui: &mut Ui, rgb: &mut [u8; 3], mut opacity: Optio
}
// Only graphical edits convert HSV back to RGB. Typed byte values stay exact.
let mut graphical_edit = false;
// Hue in degrees, saturation and value in percent, of the same gamma HSV the square shows.
const HSV: [(&str, &str, f64); 3] = [("H", "color.hue", 360.0), ("S", "color.saturation", 100.0), ("V", "color.value", 100.0)];
let mut hsv_fields = [hsv.h, hsv.s, hsv.v].iter().zip(HSV).map(|(v, (_, _, max))| f64::from(*v) * max).collect::<Vec<_>>();
// The same gamma HSV the square shows, scaled for the active number format.
let hsv_spins = Spin::hsv(hexadecimal, linear);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let mut hsv_fields = [hsv.h, hsv.s, hsv.v].iter().zip(hsv_spins).map(|(v, (_, _, spin))| f64::from(*v) * spin.max).collect::<Vec<_>>();
let mut hsv_edited = false;
ui.columns(3, |columns| {
for ((ui, value), (name, tip, max)) in columns.iter_mut().zip(hsv_fields.iter_mut()).zip(HSV) {
hsv_edited |= spin_channel(ui, name, Some(solidcraft_ui_text::tr(tip)), value, Spin { max, step: 1.0, speed: 0.5, decimals: 0 });
for ((ui, value), (name, tip, spin)) in columns.iter_mut().zip(hsv_fields.iter_mut()).zip(hsv_spins) {
hsv_edited |= spin_channel(ui, name, Some(solidcraft_ui_text::tr(tip)), value, spin);
}
});
if hsv_edited {
let unit = |at: usize| {
let (value, max) = (hsv_fields.get(at).copied().unwrap_or(0.0), HSV.get(at).map_or(1.0, |h| h.2));
let (value, max) = (hsv_fields.get(at).copied().unwrap_or(0.0), hsv_spins.get(at).map_or(1.0, |h| h.2.max));
if value.is_finite() { (value / max).clamp(0.0, 1.0) as f32 } else { 0.0 }
};
(hsv.h, hsv.s, hsv.v) = (unit(0), unit(1), unit(2));
Expand Down Expand Up @@ -384,6 +380,25 @@ struct Spin {
decimals: usize,
}

impl Spin {
const BYTE: Self = Self { max: 255.0, step: 1.0, speed: 0.5, decimals: 0 };
const UNIT: Self = Self { max: 1.0, step: 0.01, speed: 0.003, decimals: 3 };
const PERCENT: Self = Self { max: 100.0, step: 1.0, speed: 0.5, decimals: 0 };
const DEGREES: Self = Self { max: 360.0, step: 1.0, speed: 0.5, decimals: 0 };

/// R, G and B: bytes in U8, 0 to 1 in F.
fn channel(linear: bool) -> Self {
if linear { Self::UNIT } else { Self::BYTE }
}

/// H, S and V with their tooltip keys. H is in degrees in every format. S and V follow R, G
/// and B; Hex shows no R, G and B numbers, so there S and V are in percent (#10201).
fn hsv(hexadecimal: bool, linear: bool) -> [(&'static str, &'static str, Self); 3] {
let sv = if hexadecimal { Self::PERCENT } else { Self::channel(linear) };
[("H", "color.hue", Self::DEGREES), ("S", "color.saturation", sv), ("V", "color.value", sv)]
}
}

/// One channel of the popover: its name above a drag field (drag, or click and type) and a pair
/// of step buttons for fine adjustment. Returns whether the value changed.
/// Tokens: space_2/3/14, control_height, font_small, form_dim.
Expand Down
35 changes: 30 additions & 5 deletions crates/ui-kit/src/color_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,13 @@ fn step_buttons_nudge_rgb_and_hsv_channels_by_one() {
tall(&ctx, vec![], &mut rgb);
tall_click(&ctx, "color:spin:G:down", &mut rgb);
assert_eq!(rgb, [0, 0, 0]);
// H steps one degree, V one percent, on the same gamma HSV as the square.
// H steps one degree, V one byte in U8, on the same gamma HSV as the square.
let mut rgb = [255, 0, 0];
tall(&ctx, vec![], &mut rgb);
tall_click(&ctx, "color:spin:H:up", &mut rgb);
assert_eq!(rgb, hsv_bytes(1.0 / 360.0, 1.0, 1.0));
tall_click(&ctx, "color:spin:V:down", &mut rgb);
assert_eq!(rgb, hsv_bytes(1.0 / 360.0, 1.0, 0.99));
assert_eq!(rgb, hsv_bytes(1.0 / 360.0, 1.0, 254.0 / 255.0));
}

#[test]
Expand All @@ -413,11 +413,36 @@ fn typed_hsv_values_set_the_colour_within_their_ranges() {
let mut rgb = [255, 0, 0];
tall(&ctx, vec![], &mut rgb);
tall_type(&ctx, "H", "120", &mut rgb);
tall_type(&ctx, "S", "50", &mut rgb);
assert_eq!(rgb, hsv_bytes(1.0 / 3.0, 0.5, 1.0));
tall_type(&ctx, "S", "51", &mut rgb);
assert_eq!(rgb, hsv_bytes(1.0 / 3.0, 0.2, 1.0));
// A hostile typed value is bounded by the field's range.
tall_type(&ctx, "V", "1e300", &mut rgb);
assert_eq!(rgb, hsv_bytes(1.0 / 3.0, 0.5, 1.0));
assert_eq!(rgb, hsv_bytes(1.0 / 3.0, 0.2, 1.0));
}

/// S and V use the active number format, as R, G and B do: bytes in U8, 0 to 1 in F, and percent
/// in Hex. H stays in degrees (#10201).
#[test]
fn saturation_and_value_follow_the_number_format() {
let ctx = crate::test_support::context();
let mut rgb = [255, 0, 0];
tall(&ctx, vec![], &mut rgb);
tall_click(&ctx, "color:format:F", &mut rgb);
tall_type(&ctx, "S", "0.5", &mut rgb);
assert_eq!(rgb, hsv_bytes(0.0, 0.5, 1.0));
tall_click(&ctx, "color:spin:V:down", &mut rgb);
assert_eq!(rgb, hsv_bytes(0.0, 0.5, 0.99));
tall_type(&ctx, "H", "120", &mut rgb);
assert_eq!(rgb, hsv_bytes(1.0 / 3.0, 0.5, 0.99));
// A hostile typed value is bounded by the 0 to 1 range.
tall_type(&ctx, "S", "1e300", &mut rgb);
assert_eq!(rgb, hsv_bytes(1.0 / 3.0, 1.0, 0.99));
let mut rgb = [255, 0, 0];
tall_click(&ctx, "color:format:Hex", &mut rgb);
tall_type(&ctx, "S", "50", &mut rgb);
assert_eq!(rgb, hsv_bytes(0.0, 0.5, 1.0));
tall_click(&ctx, "color:spin:V:down", &mut rgb);
assert_eq!(rgb, hsv_bytes(0.0, 0.5, 0.99));
}

#[test]
Expand Down
Loading