mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-05 19:12:08 -10:00
New node: 'Blend If' (#5065)
Add Blend If, hiding content by its own tones and those beneath it
This commit is contained in:
1 parent
c8e8fbf8d1
commit
e30bf2e987
31 files changed
+4121
-233
No files matched your search
@@ -0,0 +1,259 @@
|
||||
use core_types::list::Item;
|
||||
use core_types::{Color, Ctx};
|
||||
use graphic_types::blend_if::{BlendIfRanges, BlendRange, ChannelRanges};
|
||||
use graphic_types::raster_types::{CPU, GPU, Raster};
|
||||
use graphic_types::{BlendIfChannel, Effect, EffectOperation, Generator, Graphic, Vector, stamp_effect};
|
||||
use vector_types::Gradient;
|
||||
|
||||
/// Shows the content only where the levels of its own colors, and those of what lies beneath it, fall within ranges, fading it out across each range's falloffs.
|
||||
/// The content's own ranges fade it like its fill does, leaving the shadows and glows cast from it, while the ranges beneath fade everything it draws like its opacity does. The gray, red, green, and blue channels' ranges all apply together.
|
||||
#[node_macro::node(category("Graphic"), inspector("blend_if_inspector"))]
|
||||
fn blend_if<T>(
|
||||
_: impl Ctx,
|
||||
/// The content that will be composited when rendering.
|
||||
#[implementations(Graphic, Vector, Raster<CPU>, Raster<GPU>, Color, Gradient, String, Generator)]
|
||||
content: Item<T>,
|
||||
/// Hides the content where its gray level falls below this, fading it in from here to the range start.
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
content_falloff_start: Item<f64>,
|
||||
/// Shows the content fully where its gray level is at least this, up to the range end.
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
content_range_start: Item<f64>,
|
||||
/// Shows the content fully where its gray level is at most this, down to the range start.
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
content_range_end: Item<f64>,
|
||||
/// Hides the content where its gray level rises above this, fading it in from here to the range end.
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
content_falloff_end: Item<f64>,
|
||||
/// Hides the content and its effects where the gray level of what lies beneath it falls below this, fading it in from here to the range start.
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
underlying_falloff_start: Item<f64>,
|
||||
/// Shows the content and its effects fully where the gray level of what lies beneath it is at least this, up to the range end.
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
underlying_range_start: Item<f64>,
|
||||
/// Shows the content and its effects fully where the gray level of what lies beneath it is at most this, down to the range start.
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
underlying_range_end: Item<f64>,
|
||||
/// Hides the content and its effects where the gray level of what lies beneath it rises above this, fading it in from here to the range end.
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
underlying_falloff_end: Item<f64>,
|
||||
/// Hides the content where its red level falls below this, fading it in from here to the range start.
|
||||
#[name("(Red) Content Falloff Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
red_content_falloff_start: Item<f64>,
|
||||
/// Shows the content fully where its red level is at least this, up to the range end.
|
||||
#[name("(Red) Content Range Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
red_content_range_start: Item<f64>,
|
||||
/// Shows the content fully where its red level is at most this, down to the range start.
|
||||
#[name("(Red) Content Range End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
red_content_range_end: Item<f64>,
|
||||
/// Hides the content where its red level rises above this, fading it in from here to the range end.
|
||||
#[name("(Red) Content Falloff End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
red_content_falloff_end: Item<f64>,
|
||||
/// Hides the content and its effects where the red level of what lies beneath it falls below this, fading it in from here to the range start.
|
||||
#[name("(Red) Underlying Falloff Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
red_underlying_falloff_start: Item<f64>,
|
||||
/// Shows the content and its effects fully where the red level of what lies beneath it is at least this, up to the range end.
|
||||
#[name("(Red) Underlying Range Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
red_underlying_range_start: Item<f64>,
|
||||
/// Shows the content and its effects fully where the red level of what lies beneath it is at most this, down to the range start.
|
||||
#[name("(Red) Underlying Range End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
red_underlying_range_end: Item<f64>,
|
||||
/// Hides the content and its effects where the red level of what lies beneath it rises above this, fading it in from here to the range end.
|
||||
#[name("(Red) Underlying Falloff End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
red_underlying_falloff_end: Item<f64>,
|
||||
/// Hides the content where its green level falls below this, fading it in from here to the range start.
|
||||
#[name("(Green) Content Falloff Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
green_content_falloff_start: Item<f64>,
|
||||
/// Shows the content fully where its green level is at least this, up to the range end.
|
||||
#[name("(Green) Content Range Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
green_content_range_start: Item<f64>,
|
||||
/// Shows the content fully where its green level is at most this, down to the range start.
|
||||
#[name("(Green) Content Range End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
green_content_range_end: Item<f64>,
|
||||
/// Hides the content where its green level rises above this, fading it in from here to the range end.
|
||||
#[name("(Green) Content Falloff End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
green_content_falloff_end: Item<f64>,
|
||||
/// Hides the content and its effects where the green level of what lies beneath it falls below this, fading it in from here to the range start.
|
||||
#[name("(Green) Underlying Falloff Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
green_underlying_falloff_start: Item<f64>,
|
||||
/// Shows the content and its effects fully where the green level of what lies beneath it is at least this, up to the range end.
|
||||
#[name("(Green) Underlying Range Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
green_underlying_range_start: Item<f64>,
|
||||
/// Shows the content and its effects fully where the green level of what lies beneath it is at most this, down to the range start.
|
||||
#[name("(Green) Underlying Range End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
green_underlying_range_end: Item<f64>,
|
||||
/// Hides the content and its effects where the green level of what lies beneath it rises above this, fading it in from here to the range end.
|
||||
#[name("(Green) Underlying Falloff End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
green_underlying_falloff_end: Item<f64>,
|
||||
/// Hides the content where its blue level falls below this, fading it in from here to the range start.
|
||||
#[name("(Blue) Content Falloff Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
blue_content_falloff_start: Item<f64>,
|
||||
/// Shows the content fully where its blue level is at least this, up to the range end.
|
||||
#[name("(Blue) Content Range Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
blue_content_range_start: Item<f64>,
|
||||
/// Shows the content fully where its blue level is at most this, down to the range start.
|
||||
#[name("(Blue) Content Range End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
blue_content_range_end: Item<f64>,
|
||||
/// Hides the content where its blue level rises above this, fading it in from here to the range end.
|
||||
#[name("(Blue) Content Falloff End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
blue_content_falloff_end: Item<f64>,
|
||||
/// Hides the content and its effects where the blue level of what lies beneath it falls below this, fading it in from here to the range start.
|
||||
#[name("(Blue) Underlying Falloff Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
blue_underlying_falloff_start: Item<f64>,
|
||||
/// Shows the content and its effects fully where the blue level of what lies beneath it is at least this, up to the range end.
|
||||
#[name("(Blue) Underlying Range Start")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(0.)]
|
||||
blue_underlying_range_start: Item<f64>,
|
||||
/// Shows the content and its effects fully where the blue level of what lies beneath it is at most this, down to the range start.
|
||||
#[name("(Blue) Underlying Range End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
blue_underlying_range_end: Item<f64>,
|
||||
/// Hides the content and its effects where the blue level of what lies beneath it rises above this, fading it in from here to the range end.
|
||||
#[name("(Blue) Underlying Falloff End")]
|
||||
#[unit("%")]
|
||||
#[range]
|
||||
#[hard(0..100)]
|
||||
#[default(100.)]
|
||||
blue_underlying_falloff_end: Item<f64>,
|
||||
/// Which channel's ranges are shown for editing.
|
||||
_channel: Item<BlendIfChannel>,
|
||||
) -> Item<T> {
|
||||
let range = |falloff_start: Item<f64>, range_start: Item<f64>, range_end: Item<f64>, falloff_end: Item<f64>| BlendRange {
|
||||
falloff_start: falloff_start.into_element(),
|
||||
range_start: range_start.into_element(),
|
||||
range_end: range_end.into_element(),
|
||||
falloff_end: falloff_end.into_element(),
|
||||
};
|
||||
let ranges = BlendIfRanges {
|
||||
content: ChannelRanges {
|
||||
gray: range(content_falloff_start, content_range_start, content_range_end, content_falloff_end),
|
||||
red: range(red_content_falloff_start, red_content_range_start, red_content_range_end, red_content_falloff_end),
|
||||
green: range(green_content_falloff_start, green_content_range_start, green_content_range_end, green_content_falloff_end),
|
||||
blue: range(blue_content_falloff_start, blue_content_range_start, blue_content_range_end, blue_content_falloff_end),
|
||||
},
|
||||
underlying: ChannelRanges {
|
||||
gray: range(underlying_falloff_start, underlying_range_start, underlying_range_end, underlying_falloff_end),
|
||||
red: range(red_underlying_falloff_start, red_underlying_range_start, red_underlying_range_end, red_underlying_falloff_end),
|
||||
green: range(green_underlying_falloff_start, green_underlying_range_start, green_underlying_range_end, green_underlying_falloff_end),
|
||||
blue: range(blue_underlying_falloff_start, blue_underlying_range_start, blue_underlying_range_end, blue_underlying_falloff_end),
|
||||
},
|
||||
};
|
||||
|
||||
let mut content = content;
|
||||
stamp_effect(&mut content, Effect::new(&EffectOperation::BlendIf(Box::new(ranges))));
|
||||
content
|
||||
}
|
||||
@@ -971,4 +971,30 @@ mod test {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paint_overlay_fades_a_color_paint_by_its_own_blend_ifs() {
|
||||
use graphic_types::blend_if::BlendIfRanges;
|
||||
use graphic_types::{Effect, EffectOperation, stamp_effect};
|
||||
|
||||
// A black paint shown only where its gray is at least half, so it paints nothing
|
||||
let mut hides_darks = BlendIfRanges::default();
|
||||
hides_darks.content.gray.falloff_start = 50.;
|
||||
hides_darks.content.gray.range_start = 50.;
|
||||
let mut paint = Item::new_from_element(Color::BLACK);
|
||||
stamp_effect(&mut paint, Effect::new(&EffectOperation::BlendIf(Box::new(hides_darks))));
|
||||
|
||||
let result = super::paint_overlay(
|
||||
(),
|
||||
Item::new_from_element(Raster::new_cpu(Image::new(1, 1, Color::WHITE))),
|
||||
Item::new_from_element(Graphic::from(paint)),
|
||||
BlendMode::Normal.into(),
|
||||
100_f64.into(),
|
||||
false.into(),
|
||||
DAffine2::IDENTITY.into(),
|
||||
);
|
||||
let color = raster_kernels::evaluate_raster_effects(result).into_element().data[0];
|
||||
|
||||
assert!(color.r() > 0.99, "the hidden black paint leaves {color:?}");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod backdrop;
|
||||
pub mod blend_if;
|
||||
pub mod distortions;
|
||||
pub mod effects;
|
||||
pub mod filters;
|
||||
|
||||
@@ -9,7 +9,6 @@ use graphic_types::graphic::{Graphic, IntoGraphicList, is_lone_anonymous_leaf};
|
||||
use graphic_types::{Artboard, Effects, Generator, Vector};
|
||||
use rand::SeedableRng;
|
||||
use rand::seq::SliceRandom;
|
||||
use raster_kernels::adjustments::PreparedAdjustment;
|
||||
use raster_kernels::value_effects;
|
||||
use raster_types::{CPU, GPU, Raster};
|
||||
use std::cmp::Ordering;
|
||||
@@ -1168,13 +1167,14 @@ fn colors_to_gradient(_: impl Ctx, colors: List<Graphic>) -> Item<Gradient> {
|
||||
/// Unwraps a gradient into a `Color[]` of its stops, keeping any `position` and `midpoint` attributes that place them along the ramp. Attributes belonging to the gradient as a whole (like spread and interpolation), rather than its individual color stops, are not preserved.
|
||||
#[node_macro::node(category("Gradient"), icon("NodeGradientToColors"), name("Gradient to Colors"))]
|
||||
fn gradient_to_colors(_: impl Ctx, gradient: Item<Gradient>) -> List<Color> {
|
||||
// The adjustments the gradient carries change each stop's color
|
||||
let adjustments = gradient.attribute::<Effects>(ATTR_EFFECTS).map(value_effects::adjustments).unwrap_or_default();
|
||||
let adjustments: Vec<PreparedAdjustment> = adjustments.iter().map(PreparedAdjustment::new).collect();
|
||||
// The effects the gradient carries, like adjustments and Blend Ifs, change each stop's color
|
||||
let after_effects = gradient.attribute::<Effects>(ATTR_EFFECTS).map(value_effects::prepared_color_after_effects);
|
||||
|
||||
let mut colors = gradient.into_element().into_color_list();
|
||||
for color in colors.iter_element_values_mut() {
|
||||
*color = adjustments.iter().fold(*color, |color, adjustment| adjustment.adjust(color));
|
||||
if let Some(after_effects) = after_effects {
|
||||
for color in colors.iter_element_values_mut() {
|
||||
*color = after_effects(*color);
|
||||
}
|
||||
}
|
||||
colors
|
||||
}
|
||||
@@ -1421,4 +1421,25 @@ mod test {
|
||||
let restored = colors_to_gradient((), embed_colors(gradient_to_colors((), Item::new_from_element(gradient.clone()))));
|
||||
assert_eq!(restored.element(), &gradient, "a default ramp should round trip without gaining attributes it never had");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gradient_to_colors_fades_each_stop_by_the_blend_ifs_the_gradient_carries() {
|
||||
use graphic_types::EffectOperation;
|
||||
use graphic_types::blend_if::BlendIfRanges;
|
||||
|
||||
// Shown only where the gray is at least half
|
||||
let mut hides_darks = BlendIfRanges::default();
|
||||
hides_darks.content.gray.falloff_start = 50.;
|
||||
hides_darks.content.gray.range_start = 50.;
|
||||
let effects = Effects::from_operations([EffectOperation::BlendIf(Box::new(hides_darks))]);
|
||||
|
||||
let mut gradient = Gradient::from(vec![Color::BLACK, Color::WHITE]);
|
||||
gradient.set_positions(&[0.25, 0.75]);
|
||||
let colors = gradient_to_colors((), Item::new_from_element(gradient).with_attribute(ATTR_EFFECTS, effects));
|
||||
assert_eq!(elements(&colors), [Color::BLACK.with_alpha(0.), Color::WHITE], "the dark stop should be hidden and the light one shown");
|
||||
|
||||
let mut expected = Gradient::from(vec![Color::BLACK.with_alpha(0.), Color::WHITE]);
|
||||
expected.set_positions(&[0.25, 0.75]);
|
||||
assert_eq!(colors_to_gradient((), embed_colors(colors)).element(), &expected, "each stop should keep its place");
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,8 @@ pub use graphene_core;
|
||||
pub use graphene_core::debug;
|
||||
pub use graphic_nodes;
|
||||
pub use graphic_types::{
|
||||
Appearance, Artboard, BevelDirection, BevelStyle, BevelTechnique, Cover, CoverPlacement, Coverage, Effect, EffectKind, EffectOperation, Effects, GlowSource, GlowTechnique, Graphic, MaskMode,
|
||||
MinimumMaximumMode, NoiseDistribution, Vector, stamp_coverage,
|
||||
Appearance, Artboard, BevelDirection, BevelStyle, BevelTechnique, BlendIfChannel, Cover, CoverPlacement, Coverage, Effect, EffectKind, EffectOperation, Effects, GlowSource, GlowTechnique,
|
||||
Graphic, MaskMode, MinimumMaximumMode, NoiseDistribution, Vector, stamp_coverage,
|
||||
};
|
||||
pub use graphic_types::{DisplaceMode, RadialBlurMethod, WarpStyle, WaveType};
|
||||
pub use math_nodes;
|
||||
|
||||
Reference in new issue
Block a user