mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 20:16:29 -05:00
Merge pull request #19095
Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
commit
e10ef01512
2 files changed
+208
-4
No files matched your search
@@ -0,0 +1,128 @@
|
||||
//! #17726: the panel's diagonal mesh preserves the shared ramp's vertex-color behavior.
|
||||
|
||||
use egui::{Mesh, Pos2, Rect, pos2};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn rect() -> Rect {
|
||||
Rect::from_min_max(pos2(10.0, 20.0), pos2(74.0, 84.0))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn panel_mesh_runs_from_top_left_to_bottom_right_with_equal_color_on_cross_diagonals() {
|
||||
let bounds = rect();
|
||||
let mesh = diagonal_gradient_mesh(bounds, &[(0.0, Srgba::BLACK), (1.0, Srgba::WHITE)]).unwrap();
|
||||
let first = mesh.vertices.first().unwrap();
|
||||
let last = mesh.vertices.last().unwrap();
|
||||
assert_eq!(first.pos, bounds.left_top());
|
||||
assert_eq!(first.color, Color32::BLACK);
|
||||
assert_eq!(last.pos, bounds.right_bottom());
|
||||
assert_eq!(last.color, Color32::WHITE);
|
||||
let (pairs, remainder) = mesh.vertices.as_chunks::<2>();
|
||||
assert!(remainder.is_empty(), "every contour has two vertices");
|
||||
for [a, b] in pairs {
|
||||
assert_eq!(a.color, b.color, "both ends of a diagonal contour share the same color");
|
||||
for vertex in [a, b] {
|
||||
let at = ((vertex.pos.x - bounds.left()) / bounds.width() + (vertex.pos.y - bounds.top()) / bounds.height()) * 0.5;
|
||||
assert!((f32::from(vertex.color.r()) - at * 255.0).abs() <= 1.0);
|
||||
}
|
||||
}
|
||||
let middle: Vec<_> = mesh.vertices.iter().filter(|v| v.pos == bounds.right_top() || v.pos == bounds.left_bottom()).collect();
|
||||
assert_eq!(middle.len(), 2);
|
||||
assert_eq!(middle[0].color, middle[1].color);
|
||||
assert!(middle[0].color.r() > 120 && middle[0].color.r() < 135);
|
||||
assert!((mesh_area(&mesh) - bounds.area()).abs() < 0.01, "the diagonal bands cover the square without gaps");
|
||||
}
|
||||
|
||||
fn mesh_area(mesh: &Mesh) -> f32 {
|
||||
let (triangles, remainder) = mesh.indices.as_chunks::<3>();
|
||||
assert!(remainder.is_empty(), "the mesh contains complete triangles");
|
||||
triangles
|
||||
.iter()
|
||||
.map(|[a, b, c]| {
|
||||
let (a, b, c) = (mesh.vertices[*a as usize].pos, mesh.vertices[*b as usize].pos, mesh.vertices[*c as usize].pos);
|
||||
let (ab, ac) = (b - a, c - a);
|
||||
(ab.x * ac.y - ab.y * ac.x).abs() * 0.5
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transparent_colors_keep_premultiplied_interpolation_and_exact_stop_colors() {
|
||||
let bounds = rect();
|
||||
let red_transparent = Srgba::new(1.0, 0.0, 0.0, 0.0);
|
||||
let blue = Srgba::new(0.0, 0.0, 1.0, 1.0);
|
||||
let colors = [(0.0, red_transparent), (0.25, blue), (1.0, Srgba::WHITE)];
|
||||
let mesh = diagonal_gradient_mesh(bounds, &colors).unwrap();
|
||||
assert_eq!(mesh.vertices.first().unwrap().color, Color32::TRANSPARENT);
|
||||
for vertex in &mesh.vertices {
|
||||
let at = ((vertex.pos.x - bounds.left()) / bounds.width() + (vertex.pos.y - bounds.top()) / bounds.height()) * 0.5;
|
||||
if at <= 0.25 {
|
||||
assert_eq!(vertex.color.r(), 0, "hidden transparent red cannot bleed into blue");
|
||||
assert_eq!(vertex.color.b(), vertex.color.a());
|
||||
}
|
||||
if (at - 0.25).abs() < 1e-6 {
|
||||
assert_eq!(vertex.color, Color32::BLUE, "the existing ramp's stop is exact");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stop_edges_extend_and_hard_steps_remain_distinct() {
|
||||
let bounds = rect();
|
||||
let colors = [(0.2, Srgba::BLACK), (0.5, Srgba::BLACK), (0.5, Srgba::WHITE), (0.8, Srgba::WHITE)];
|
||||
let mesh = diagonal_gradient_mesh(bounds, &colors).unwrap();
|
||||
assert_eq!(mesh.vertices.first().unwrap().color, Color32::BLACK);
|
||||
assert_eq!(mesh.vertices.last().unwrap().color, Color32::WHITE);
|
||||
let at_middle: Vec<_> = mesh.vertices.iter().filter(|v| v.pos == bounds.right_top()).map(|v| v.color).collect();
|
||||
assert_eq!(at_middle, [Color32::BLACK, Color32::WHITE]);
|
||||
assert!((mesh_area(&mesh) - bounds.area()).abs() < 0.01);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_or_invalid_thumbnail_geometry_produces_no_mesh() {
|
||||
let colors = [(0.0, Srgba::BLACK), (1.0, Srgba::WHITE)];
|
||||
assert!(diagonal_gradient_mesh(rect(), &[]).is_none());
|
||||
assert!(diagonal_gradient_mesh(Rect::from_min_max(Pos2::ZERO, Pos2::ZERO), &colors).is_none());
|
||||
assert!(diagonal_gradient_mesh(Rect::from_min_max(Pos2::ZERO, pos2(f32::NAN, 64.0)), &colors).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_gradients_panel_renders_square_cells_and_keeps_named_selection() {
|
||||
use egui_kittest::kittest::Queryable;
|
||||
|
||||
let name = "Diagonal panel test";
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
|
||||
app.session.presets_mut().gradients.clear();
|
||||
app.run("gradient.presets.new", json!({"name": name, "group": "Test", "stops": [[0.0, "#ff0000"], [1.0, "#0000ff"]]})).unwrap();
|
||||
let mut h = crate::input::shortcut_dispatch::tests::app_harness(app);
|
||||
let ctx = h.ctx.clone();
|
||||
crate::menus::invoke(h.state_mut(), &ctx, "window.panel.gradients", json!({})).unwrap();
|
||||
crate::menus::invoke(h.state_mut(), &ctx, "window.panel.float", json!({"panel": "gradients", "x": 400, "y": 140, "width": 360, "height": 400})).unwrap();
|
||||
crate::menus::invoke(h.state_mut(), &ctx, "window.panel.window", json!({"panel": "gradients", "native": false})).unwrap();
|
||||
h.run_steps(6);
|
||||
let row = h.query_by_label_contains(name).expect("the gradient retains its accessible preset name");
|
||||
let bounds = row.rect();
|
||||
row.click();
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.state().ui.presets_ui.selected.get(GRADIENTS.id).map(String::as_str), Some(name));
|
||||
let mesh = h.output().shapes.iter().find_map(|shape| colored_mesh(&shape.shape, bounds)).expect("the visible preset paints its gradient");
|
||||
let painted = mesh.calc_bounds();
|
||||
assert!((painted.width() - painted.height()).abs() < 0.1, "panel thumbnails are square: {painted:?}");
|
||||
}
|
||||
|
||||
fn colored_mesh(shape: &egui::Shape, row: Rect) -> Option<&Mesh> {
|
||||
match shape {
|
||||
egui::Shape::Vec(shapes) => shapes.iter().find_map(|shape| colored_mesh(shape, row)),
|
||||
egui::Shape::PaintChunk(shapes) => shapes.iter().find_map(|shape| colored_mesh(&shape.shape, row)),
|
||||
egui::Shape::Mesh(mesh)
|
||||
if row.contains(mesh.calc_bounds().center())
|
||||
&& mesh.vertices.iter().any(|v| v.color == Color32::RED)
|
||||
&& mesh.vertices.iter().any(|v| v.color == Color32::BLUE) =>
|
||||
{
|
||||
Some(mesh)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -349,21 +349,85 @@ fn swatch_thumb(inputs: &PanelInputs<Vec<Swatch>>, cx: &ContentPainterCx, rect:
|
||||
|
||||
// ------------------------------------------------------------------ Gradients
|
||||
|
||||
/// A gradient chip over `stops`, resolved against the current colours.
|
||||
fn paint_gradient(cx: &ContentPainterCx, rect: Rect, stops: &[(f32, [f32; 4])]) {
|
||||
/// A horizontal gradient chip for the preset popup and list rows.
|
||||
fn paint_gradient_strip(cx: &ContentPainterCx, rect: Rect, stops: &[(f32, [f32; 4])]) {
|
||||
let colors: Vec<(f32, Srgba)> = stops.iter().map(|&(at, c)| (at, Srgba::new(c[0], c[1], c[2], c[3]))).collect();
|
||||
let (radius, edge) = GradientRamp::chip_look(&cx.style_cx());
|
||||
GradientRamp::paint_chip(cx, rect, radius, &colors, edge);
|
||||
}
|
||||
|
||||
/// Reorients the existing ramp's premultiplied vertex colours along the square's main diagonal.
|
||||
fn diagonal_gradient_mesh(rect: Rect, colors: &[(f32, Srgba)]) -> Option<egui::Mesh> {
|
||||
use craft::behavior::Axis;
|
||||
use craft::ui::primitives::ramp;
|
||||
use craft::ui::style::DataColor;
|
||||
|
||||
if !rect.is_finite() || !(rect.width() > 0.0 && rect.height() > 0.0) {
|
||||
return None;
|
||||
}
|
||||
let stops: Vec<_> = colors.iter().map(|&(at, color)| (at, DataColor::new(Color32::from(color)))).collect();
|
||||
let strip = ramp(Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)), egui::CornerRadius::ZERO, &stops, Axis::X).into_iter().find_map(|shape| match shape {
|
||||
egui::Shape::Mesh(mesh) => Some(mesh),
|
||||
_ => None,
|
||||
})?;
|
||||
// The ramp's vertices are pairs; leave an incomplete final pair out of the contour marks.
|
||||
let (pairs, _) = strip.vertices.as_chunks::<2>();
|
||||
let mut marks: Vec<_> = pairs.iter().map(|[first, _]| (first.pos.x, first.color)).collect();
|
||||
let original = marks.clone();
|
||||
// Include the contour's turn at 1/2, and enough edge samples for mesh_in's rounded corners.
|
||||
// Interpolate premultiplied sRGB vertex values just as the shared ramp does.
|
||||
for sample in 1..64 {
|
||||
let at = sample as f32 / 64.0;
|
||||
let between = original.windows(2).find_map(|pair| {
|
||||
let [a, b] = pair else { return None };
|
||||
(a.0 < at && b.0 > at).then_some((*a, *b))
|
||||
});
|
||||
if let Some((a, b)) = between {
|
||||
let color = a.1.lerp_to_gamma(b.1, (at - a.0) / (b.0 - a.0));
|
||||
marks.push((at, color));
|
||||
}
|
||||
}
|
||||
marks.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
let mut mesh = egui::Mesh::default();
|
||||
for (at, color) in marks {
|
||||
let (a, b) = if at <= 0.5 { (pos2(2.0 * at, 0.0), pos2(0.0, 2.0 * at)) } else { (pos2(1.0, 2.0 * at - 1.0), pos2(2.0 * at - 1.0, 1.0)) };
|
||||
let base = u32::try_from(mesh.vertices.len()).ok()?;
|
||||
let second = base.checked_add(1)?;
|
||||
let point = |p: Pos2| pos2(rect.left() + p.x * rect.width(), rect.top() + p.y * rect.height());
|
||||
mesh.colored_vertex(point(a), color);
|
||||
mesh.colored_vertex(point(b), color);
|
||||
if base >= 2 {
|
||||
mesh.add_triangle(base - 2, base - 1, base);
|
||||
mesh.add_triangle(base - 1, second, base);
|
||||
}
|
||||
}
|
||||
Some(mesh)
|
||||
}
|
||||
|
||||
/// A square panel thumbnail over the theme checkerboard, with the shared chip's corners and edge.
|
||||
fn paint_gradient(cx: &ContentPainterCx, rect: Rect, stops: &[(f32, [f32; 4])]) {
|
||||
use craft::ui::primitives::{Checker, PaintShape, StrokeWidth};
|
||||
|
||||
let colors: Vec<_> = stops.iter().map(|&(at, c)| (at, Srgba::new(c[0], c[1], c[2], c[3]))).collect();
|
||||
let (radius, edge) = GradientRamp::chip_look(&cx.style_cx());
|
||||
if let Some(mesh) = diagonal_gradient_mesh(rect, &colors) {
|
||||
if colors.iter().any(|(_, color)| color.a < 1.0) {
|
||||
cx.checker_with(&Checker::new(), rect, radius);
|
||||
}
|
||||
cx.mesh_in(rect, radius, mesh);
|
||||
}
|
||||
cx.with_draw(|draw| draw.stroke(&PaintShape::Rect(rect, radius), StrokeWidth::Thin, edge));
|
||||
}
|
||||
|
||||
/// Window › Gradients: a click selects the gradient for the Gradient tool, a double click or a drop on the canvas
|
||||
/// applies it.
|
||||
pub fn gradients_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
// PLAN: the current-gradient strip above the browser needs a painter component; the browser's selected row shows it.
|
||||
let (names, inputs) = gradient_inputs(app);
|
||||
let source = panel_source(inputs, gradient_thumb);
|
||||
let grid = app.ui.presets_ui.browsers.get(GRADIENTS.id).is_none_or(|state| state.view == PresetView::Grid);
|
||||
let source = if grid { panel_source(inputs, gradient_diagonal_thumb) } else { panel_source(inputs, gradient_thumb) };
|
||||
let zone = canvas_zone(app);
|
||||
let look = Look { cell: Some(CellShape::ratio(32, 18)), ..Look::default() };
|
||||
let look = Look { cell: Some(if grid { CellShape::Square } else { CellShape::ratio(32, 18) }), ..Look::default() };
|
||||
let (events, keys) = show(&mut app.ui.presets_ui, zone, ui, GRADIENTS, &source, look);
|
||||
apply(app, ui.ctx(), GRADIENTS, &names, &keys, events);
|
||||
}
|
||||
@@ -387,6 +451,14 @@ type ResolvedGradient = (String, Vec<(f32, [f32; 4])>);
|
||||
|
||||
/// A gradient panel thumbnail: the preset's stops, resolved against the current colours.
|
||||
fn gradient_thumb(inputs: &PanelInputs<Vec<ResolvedGradient>>, cx: &ContentPainterCx, rect: Rect, info: ThumbInfo<'_, PresetKey>) {
|
||||
if let PresetKey::Item(name) = info.key
|
||||
&& let Some((_, stops)) = inputs.thumbs.iter().find(|(preset, _)| preset == name)
|
||||
{
|
||||
paint_gradient_strip(cx, rect, stops);
|
||||
}
|
||||
}
|
||||
|
||||
fn gradient_diagonal_thumb(inputs: &PanelInputs<Vec<ResolvedGradient>>, cx: &ContentPainterCx, rect: Rect, info: ThumbInfo<'_, PresetKey>) {
|
||||
if let PresetKey::Item(name) = info.key
|
||||
&& let Some((_, stops)) = inputs.thumbs.iter().find(|(preset, _)| preset == name)
|
||||
{
|
||||
@@ -1156,3 +1228,7 @@ mod tests;
|
||||
#[cfg(test)]
|
||||
#[path = "tool_presets_row_icon_tests.rs"]
|
||||
mod tool_presets_row_icon_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "gradients_panel_thumbnail_tests.rs"]
|
||||
mod gradients_panel_thumbnail_tests;
|
||||
Reference in new issue
Block a user