Rename the 'Image to Channels' node to 'Into Channels' and defer its channels on any graphic

This commit is contained in:
Keavon Chambers committed 2026-12-05 04:43:43 -08:00
1 parent c465a3ebea
commit 12daaf8ab0
16 files changed
+279 -48

No files matched your search

@@ -86,6 +86,8 @@ pub enum Adjustment {
opacity: f64,
},
Solarize,
/// Shows the alpha as a gray of the same gamma-encoded level, keeping the alpha, so a luminance mask of the gray shows as much as the alpha.
AlphaToGray,
}
impl Adjustment {
@@ -112,6 +114,7 @@ impl Adjustment {
Self::ColorLookup { .. } => EffectKind::ColorLookup,
Self::ColorOverlay { .. } => EffectKind::PaintOverlay,
Self::Solarize => EffectKind::Solarize,
Self::AlphaToGray => EffectKind::AlphaToGray,
}
}
@@ -151,7 +154,7 @@ impl Adjustment {
visitor.parameter("inverse", inverse, false);
}
Self::MakeOpaque { strength } => visitor.parameter("strength", strength, 100.),
Self::Invert | Self::Solarize => {}
Self::Invert | Self::Solarize | Self::AlphaToGray => {}
Self::BrightnessContrast {
brightness,
contrast,
@@ -15,7 +15,7 @@ use core_types::Color;
use core_types::blending::BlendMode;
use core_types::color::SRGBA8;
use core_types::graphene_hash::CacheHash;
use core_types::list::{ATTR_EFFECTS, ATTR_OPACITY, ATTR_OPACITY_FILL, Item, List};
use core_types::list::{ATTR_EFFECTS, ATTR_OPACITY, ATTR_OPACITY_FILL, Item, ItemAttributeValues, List};
use core_types::transfer_curve::{TransferCurve, TransferCurveEvaluator};
use dyn_any::DynAny;
use glam::{DAffine2, DMat2, DVec2, DVec3};
@@ -117,6 +117,8 @@ pub enum EffectKind {
PhotoFilter,
ColorLookup,
Solarize,
/// Shows the content's alpha as a gray, as 'Into Channels' does for its alpha output.
AlphaToGray,
}
impl std::fmt::Display for EffectKind {
@@ -175,6 +177,7 @@ impl std::fmt::Display for EffectKind {
Self::PhotoFilter => "Photo Filter",
Self::ColorLookup => "Color Lookup",
Self::Solarize => "Solarize",
Self::AlphaToGray => "Alpha to Gray",
})
}
}
@@ -846,8 +849,14 @@ pub fn contour_gradient_position(distance: f32, start: f32, span: f32) -> f32 {
/// Appends an effect to the item's list, after those already applied to it, recording the item's transform at that point.
/// Like a stroke's transform, it sets the space the effect's lengths and angles are measured in, so transforms applied later carry and scale the effect.
pub fn stamp_effect<T>(item: &mut Item<T>, effect: Effect) {
let transform = stamped_transform(item.attribute_cloned_or_default(ATTR_TRANSFORM));
item.attribute_mut_or_insert_default::<Effects>(ATTR_EFFECTS)
stamp_effect_on(item.attributes_mut(), effect);
}
/// Like [`stamp_effect`], given the item's attributes.
pub fn stamp_effect_on(attributes: &mut ItemAttributeValues, effect: Effect) {
let transform = stamped_transform(attributes.get::<DAffine2>(ATTR_TRANSFORM).copied().unwrap_or_default());
attributes
.get_or_insert_default_mut::<Effects>(ATTR_EFFECTS)
.0
.push(Item::new_from_element(effect).with_attribute(ATTR_TRANSFORM, transform));
}
@@ -1886,6 +1895,7 @@ impl EffectOperation {
}),
EffectKind::ColorLookup => Self::Adjustment(Adjustment::ColorLookup { lut_file: Default::default() }),
EffectKind::Solarize => Self::Adjustment(Adjustment::Solarize),
EffectKind::AlphaToGray => Self::Adjustment(Adjustment::AlphaToGray),
}
}
@@ -2899,6 +2909,7 @@ mod tests {
PhotoFilter,
ColorLookup,
Solarize,
AlphaToGray,
PaintOverlay,
];
for kind in kinds {
@@ -24,7 +24,7 @@ pub use artboard::Artboard;
pub use blend_if::BlendIfChannel;
pub use effects::{
BevelDirection, BevelStyle, BevelTechnique, Effect, EffectKind, EffectOperation, Effects, GlowSource, GlowTechnique, MinimumMaximumMode, linked_to_content, paint_with_envelope_effects,
stamp_effect, stamped_transform,
stamp_effect, stamp_effect_on, stamped_transform,
};
pub use filters::NoiseDistribution;
pub use generator::Generator;
@@ -30,11 +30,12 @@ pub enum StepKind {
ColorLookup,
ColorOverlay,
Solarize,
AlphaToGray,
}
impl StepKind {
/// Every kind, in the order of their codes.
pub const ALL: [StepKind; 21] = [
pub const ALL: [StepKind; 22] = [
StepKind::Desaturate,
StepKind::GammaCorrection,
StepKind::MakeOpaque,
@@ -56,6 +57,7 @@ impl StepKind {
StepKind::ColorLookup,
StepKind::ColorOverlay,
StepKind::Solarize,
StepKind::AlphaToGray,
];
/// The number a step's header holds for its kind, which the shader picks the step's function by.
@@ -269,6 +271,7 @@ mod tests {
opacity: 50.,
},
Adjustment::Solarize,
Adjustment::AlphaToGray,
];
let steps = AdjustmentSteps::encode(&run.iter().collect::<Vec<_>>());
@@ -226,6 +226,7 @@ pub fn visit_kernel<V: AdjustmentVisitor>(adjustment: &Adjustment, visitor: V) -
}
Adjustment::Invert => visitor.visit(Invert),
Adjustment::Solarize => visitor.visit(Solarize),
Adjustment::AlphaToGray => visitor.visit(AlphaToGray),
Adjustment::Threshold { min_luminance, max_luminance } => visitor.visit(Threshold {
min_luminance: (min_luminance / 100.) as f32,
max_luminance: (max_luminance / 100.) as f32,
@@ -1121,6 +1122,20 @@ impl MapColor for Solarize {
}
}
/// Grays each color to the gamma-encoded level of its alpha, keeping the alpha.
struct AlphaToGray;
impl MapColor for AlphaToGray {
fn map_cpu(&self, color: Color) -> Color {
let gray = srgb_to_linear(color.a());
color.map_rgb(|_| gray)
}
fn encode_step(&self, steps: &mut AdjustmentSteps) {
steps.push(StepKind::AlphaToGray, []);
}
}
struct Threshold {
min_luminance: f32,
max_luminance: f32,
@@ -1755,7 +1755,7 @@ fn svg_expresses_adjustment(adjustment: &Adjustment) -> bool {
use graphic_types::adjustment::DesaturateMethod;
match adjustment {
Adjustment::Threshold { .. } | Adjustment::GradientMap { .. } | Adjustment::ChannelMixer(_) | Adjustment::ColorOverlay { .. } => true,
Adjustment::Threshold { .. } | Adjustment::GradientMap { .. } | Adjustment::ChannelMixer(_) | Adjustment::ColorOverlay { .. } | Adjustment::AlphaToGray => true,
Adjustment::Desaturate { method } => matches!(
method,
DesaturateMethod::LuminanceRec709 | DesaturateMethod::ChannelsAverage | DesaturateMethod::LumaRec709 | DesaturateMethod::LumaRec601
@@ -2095,6 +2095,10 @@ impl SvgFilterPrimitives {
};
self.push("feColorMatrix", &format!(r#"in="{image}" color-interpolation-filters="{space}" {}"#, matrix([weights; 3])), "")
}
Adjustment::AlphaToGray => {
let values = "0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0";
self.push("feColorMatrix", &format!(r#"{} type="matrix" values="{values}""#, in_gamma(image)), "")
}
// TODO: Express the other cross-channel adjustments, which no SVG primitive covers exactly
_ => image.to_string(),
}
@@ -109,6 +109,7 @@ fn step_function(kind: StepKind) -> (&'static str, Option<[Encoding; 2]>) {
StepKind::ColorLookup => ("color_lookup", Some([Gamma, Gamma])),
StepKind::ColorOverlay => ("color_overlay", Some([Gamma, Gamma])),
StepKind::Solarize => ("solarize", Some([Gamma, Gamma])),
StepKind::AlphaToGray => ("alpha_to_gray", Some([Gamma, Gamma])),
}
}
@@ -365,6 +365,11 @@ fn solarize(color: vec4<f32>, at: u32) -> vec4<f32> {
return vec4<f32>(min(color.rgb, 1. - color.rgb), color.a);
}
// The alpha as a gray of the same gamma-encoded level, keeping the alpha
fn alpha_to_gray(color: vec4<f32>, at: u32) -> vec4<f32> {
return vec4<f32>(vec3<f32>(color.a), color.a);
}
// Matches `apply_levels` with the gamma of 1 that every caller here passes, so its power is left out
fn apply_levels(value: f32, input_shadows: f32, input_highlights: f32, output_minimum: f32, output_maximum: f32) -> f32 {
let highlights_minus_shadows = clamp(input_highlights - input_shadows, F32_EPSILON, 1.);
@@ -5279,9 +5279,20 @@ fn gpu_color_adjustments_match_the_cpu_kernels() {
("Photo Filter", photo_filter(warming, 25., true)),
("Photo Filter at full density", photo_filter(cooling, 100., false)),
("Photo Filter at full density preserving luminosity", photo_filter(cooling, 100., true)),
("Alpha to Gray", Adjustment::AlphaToGray),
];
let mut runs: Vec<(String, Vec<Adjustment>)> = runs.into_iter().map(|(name, adjustment)| (name.to_string(), vec![adjustment])).collect();
// The opaque grays of one channel each that 'Into Channels' outputs
let opaque = Adjustment::MakeOpaque { strength: 100. };
let green_gray = Adjustment::ChannelMixer(Box::new(ChannelMixerSettings {
monochrome: true,
monochrome_mix: row(0., 100., 0., 0.),
..Default::default()
}));
runs.push(("Green as an opaque gray".to_string(), vec![green_gray, opaque.clone()]));
runs.push(("Alpha as an opaque gray".to_string(), vec![Adjustment::AlphaToGray, opaque]));
// Every blend mode, overlaying an opaque color and a translucent one, each with a luma apart from every test color's, which Darker Color and Lighter Color compare, and off Hard Mix's steep ramp over them
for blend_mode in BlendMode::list().into_iter().flatten().chain(&[BlendMode::Erase, BlendMode::Restore, BlendMode::MultiplyAlpha]) {
for (color, opacity) in [