Apply Classic gradient opacity to the preview swatch

This commit is contained in:
yoriswind committed 2026-12-01 01:30:00 -08:00
1 parent fa3b374b3c
commit 586196d256
2 files changed
+58 -1

No files matched your search

@@ -0,0 +1,47 @@
//! #18644: The Classic gradient swatch follows the options bar's Opacity.
use craft::theme::Srgba;
use egui::{Color32, Pos2, Rect, Shape, vec2};
use photocraft_engine::presets::{PresetsExt, gradients::GradientPreset};
use serde_json::json;
use crate::{PhotocraftApp, state::Tool};
fn mesh_has_color(shape: &Shape, color: Color32) -> bool {
match shape {
Shape::Vec(shapes) => shapes.iter().any(|shape| mesh_has_color(shape, color)),
Shape::PaintChunk(shapes) => shapes.iter().any(|clipped| mesh_has_color(&clipped.shape, color)),
Shape::Mesh(mesh) => mesh.vertices.iter().any(|vertex| vertex.color == color),
_ => false,
}
}
#[test]
fn classic_gradient_opacity_changes_the_painted_swatch_only_in_classic_mode() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
app.run("file.new", json!({"width": 50, "height": 50})).unwrap();
app.ui.tool = Tool::Gradient;
app.ui.tool_options.gradient_classic = true;
app.session.presets_mut().gradient = GradientPreset::new("Swatch test", &["#ff00ff", "#ff00ff"]);
let ctx = app.ctx().clone();
PhotocraftApp::setup_context(&ctx, crate::theme::AppTheme::pro());
let draw = |app: &mut PhotocraftApp, alpha: f32| {
let rect = Rect::from_min_size(Pos2::ZERO, vec2(1400.0, 180.0));
let output = ctx.run_ui(egui::RawInput { screen_rect: Some(rect), ..Default::default() }, |ui| {
ui.horizontal(|ui| super::bar(app, ui));
});
let expected = Color32::from(Srgba::new(1.0, 0.0, 1.0, alpha));
assert!(output.shapes.iter().any(|clipped| mesh_has_color(&clipped.shape, expected)), "the swatch paints at alpha {alpha}");
};
app.ui.tool_options.fill_opacity = 100.0;
draw(&mut app, 1.0);
app.ui.tool_options.fill_opacity = 10.0;
draw(&mut app, 0.1);
app.session.presets_mut().gradient.opacity = vec![(0.0, 0.5), (1.0, 0.5)];
draw(&mut app, 0.05);
app.ui.tool_options.gradient_classic = false;
draw(&mut app, 0.5);
}
+11 -1
View File
@@ -3,6 +3,7 @@
//! and Dither.
use craft::behavior::IconId;
use craft::color_ui::ui::gradient::GradientStops;
use craft::draw::ui::gradient::{GradientFillWords, GradientStyle, GradientSwatch};
use craft::ui::buttons::IconButton;
use craft::ui::choice::{Checkbox, Segmented};
@@ -55,7 +56,7 @@ pub(crate) fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let locked_live = fill_layer_active(app);
let (fg, bg) = (app.session.tools.foreground, app.session.tools.background);
let tool_stops = stops::of_preset(&app.session.presets().gradient, fg, bg);
let sampler = stops::sampler(fg, bg);
let sample = stops::sampler(fg, bg);
let words = Env::of(ui).words::<GradientFillWords>();
let styles: Vec<Entry<GradientStyle>> = GradientStyle::ALL.iter().map(|style| Entry::new(*style, style.label(&words)).icon(style.icon())).collect();
let modes = [Entry::new(false, tl!("Gradient").to_owned()), Entry::new(true, tl!("Classic gradient").to_owned())];
@@ -72,6 +73,11 @@ pub(crate) fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
if !locked_live && classic != app.ui.tool_options.gradient_classic {
app.ui.tool_options.gradient_classic = classic;
}
let opacity = if classic { app.ui.tool_options.fill_opacity.clamp(0.0, 100.0) / 100.0 } else { 1.0 };
let sampler = |stops: &GradientStops, at| {
let color = sample(stops, at);
color.with_alpha(color.a * opacity)
};
let swatch = GradientSwatch::new(tl!("Gradient picker").to_string(), &tool_stops).sampler(&sampler).show(ui);
open_editor = swatch.clicked;
presets = preset_arrow(ui, swatch.response.rect);
@@ -120,6 +126,10 @@ pub(crate) fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
options_changed(app, &before);
}
#[cfg(test)]
#[path = "../../gradient_preview_swatch_opacity_tests.rs"]
mod gradient_preview_swatch_opacity_tests;
/// The gradient preset picker's open state between passes, kept in egui's memory: it is the bar's popup, not a setting.
#[derive(Clone, Copy, Default)]
struct PresetPopup {