mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-05 19:12:08 -10:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16774db9c2 | ||
|
|
a4c0cb5c31 |
No files matched your search
@@ -18,4 +18,4 @@ pub mod warp_kernels;
|
||||
|
||||
pub use kernel::*;
|
||||
pub use raster_effects::evaluate_raster_effects;
|
||||
pub use value_effects::{evaluate_color_effects, evaluate_gradient_effects};
|
||||
pub use value_effects::{evaluate_color_effects, evaluate_color_parameter, evaluate_gradient_effects};
|
||||
@@ -5,7 +5,7 @@ use crate::adjustments::{PreparedAdjustment, adjust_color};
|
||||
use crate::blending::overlay_paint;
|
||||
use core_types::Color;
|
||||
use core_types::graphene_hash::CacheHash;
|
||||
use core_types::list::{ATTR_EFFECTS, ATTR_GRADIENT_HUE_DIRECTION, ATTR_GRADIENT_INTERPOLATION, ATTR_GRADIENT_SPACE, Item};
|
||||
use core_types::list::{ATTR_EFFECTS, ATTR_GRADIENT_HUE_DIRECTION, ATTR_GRADIENT_INTERPOLATION, ATTR_GRADIENT_SPACE, ATTR_OPACITY, ATTR_OPACITY_FILL, Item};
|
||||
use graphic_types::adjustment::Adjustment;
|
||||
use graphic_types::blend_if::{ChannelRanges, blend_ifs_acting_before, fades_content};
|
||||
use graphic_types::effects::{EffectOperation, Effects};
|
||||
@@ -273,7 +273,7 @@ pub fn gradient_after_effects(gradient: &Gradient, settings: GradientSettings, e
|
||||
adjusted_gradient_with_blend_ifs(gradient, settings, &adjustments(effects), &content_blend_ifs(effects), 1.)
|
||||
}
|
||||
|
||||
/// The color with its effects carried out as [`color_after_effects`] does and consumed, for nodes that read the color.
|
||||
/// The color with its effects carried out as [`color_after_effects`] does and consumed, though not its opacity (see [`evaluate_color_parameter`]).
|
||||
/// A color without the effects attribute is returned as it is.
|
||||
pub fn evaluate_color_effects(item: Item<Color>) -> Item<Color> {
|
||||
let Some(color) = item.attribute::<Effects>(ATTR_EFFECTS).map(|effects| color_after_effects(*item.element(), effects)) else {
|
||||
@@ -286,6 +286,27 @@ pub fn evaluate_color_effects(item: Item<Color>) -> Item<Color> {
|
||||
item
|
||||
}
|
||||
|
||||
/// The color as the renderers show it, for a node reading it as a parameter: its effects carried out over it faded by its fill, then its opacity fading it all, each consumed.
|
||||
/// Every node reading one color goes through this, so those attributes mean the same to all of them.
|
||||
pub fn evaluate_color_parameter(item: Item<Color>) -> Item<Color> {
|
||||
let fill = item.attribute_cloned_or::<f64>(ATTR_OPACITY_FILL, 1.) as f32;
|
||||
let opacity = item.attribute_cloned_or::<f64>(ATTR_OPACITY, 1.) as f32;
|
||||
let (adjustments, blend_ifs) = item
|
||||
.attribute::<Effects>(ATTR_EFFECTS)
|
||||
.map(|effects| (adjustments(effects), content_blend_ifs(effects)))
|
||||
.unwrap_or_default();
|
||||
|
||||
let shown = filled_color_with_blend_ifs(*item.element(), &adjustments, &blend_ifs, fill);
|
||||
let mut item = item;
|
||||
*item.element_mut() = shown.with_alpha(shown.a() * opacity);
|
||||
|
||||
// Consumed so a node passing the item on doesn't get them carried out twice where it's drawn
|
||||
item.remove_attribute::<Effects>(ATTR_EFFECTS);
|
||||
item.remove_attribute::<f64>(ATTR_OPACITY);
|
||||
item.remove_attribute::<f64>(ATTR_OPACITY_FILL);
|
||||
item
|
||||
}
|
||||
|
||||
/// The gradient with its effects carried out along its ramp as [`gradient_after_effects`] does and consumed, for nodes that read its colors.
|
||||
/// A gradient without the effects attribute is returned as it is.
|
||||
pub fn evaluate_gradient_effects(item: Item<Gradient>) -> Item<Gradient> {
|
||||
@@ -351,6 +372,21 @@ mod tests {
|
||||
assert!(close(gamma(filled_color(Color::BLACK, &half_white, 0.5)), [2. / 3., 2. / 3., 2. / 3., 0.75]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_color_parameters_fill_fades_it_beneath_its_paint_overlays_and_its_opacity_over_them() {
|
||||
let red = EffectOperation::Adjustment(Adjustment::ColorOverlay {
|
||||
color: Color::RED,
|
||||
blend_mode: BlendMode::Normal,
|
||||
opacity: 100.,
|
||||
});
|
||||
let unfilled = Item::new_from_element(Color::BLACK)
|
||||
.with_attribute(ATTR_EFFECTS, Effects::from_operations([red]))
|
||||
.with_attribute(ATTR_OPACITY_FILL, 0_f64);
|
||||
|
||||
assert!(close(gamma(*evaluate_color_parameter(unfilled.clone()).element()), [1., 0., 0., 1.]));
|
||||
assert!(close(gamma(*evaluate_color_parameter(unfilled.with_attribute(ATTR_OPACITY, 0.5_f64)).element()), [1., 0., 0., 0.5]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn consumed_effects_fade_values_by_their_blend_ifs_where_they_read_the_colors() {
|
||||
use graphic_types::blend_if::BlendIfRanges;
|
||||
|
||||
@@ -3,13 +3,13 @@ use core_types::blending::BlendMode;
|
||||
use core_types::bounds::{BoundingBox, RenderBoundingBox};
|
||||
use core_types::list::{Item, List};
|
||||
use core_types::transfer_curve::TransferCurve;
|
||||
use core_types::{ATTR_OPACITY, ATTR_OPACITY_FILL, ATTR_TRANSFORM, Color, Ctx};
|
||||
use core_types::{ATTR_TRANSFORM, Color, Ctx};
|
||||
use glam::{DAffine2, DVec2};
|
||||
use graphic_types::raster_types::{CPU, GPU, Raster};
|
||||
use graphic_types::{
|
||||
BevelDirection, BevelStyle, BevelTechnique, Effect, EffectOperation, Generator, GlowSource, GlowTechnique, Graphic, Vector, linked_to_content, paint_with_envelope_effects, stamp_effect,
|
||||
};
|
||||
use raster_kernels::{evaluate_color_effects, evaluate_gradient_effects};
|
||||
use raster_kernels::{evaluate_color_effects, evaluate_color_parameter, evaluate_gradient_effects};
|
||||
use vector_types::Gradient;
|
||||
use vector_types::vector::style::{StrokeAlign, initial_gradient_transform_for_bounding_box};
|
||||
|
||||
@@ -99,7 +99,7 @@ fn drop_shadow<T>(
|
||||
stamp_effect(
|
||||
&mut content,
|
||||
Effect::new(&EffectOperation::DropShadow {
|
||||
color: evaluate_color_effects(color).into_element(),
|
||||
color: evaluate_color_parameter(color).into_element(),
|
||||
blend_mode: *blend_mode.element(),
|
||||
angle: *angle.element(),
|
||||
distance: *distance.element(),
|
||||
@@ -155,7 +155,7 @@ fn inner_shadow<T>(
|
||||
stamp_effect(
|
||||
&mut content,
|
||||
Effect::new(&EffectOperation::InnerShadow {
|
||||
color: evaluate_color_effects(color).into_element(),
|
||||
color: evaluate_color_parameter(color).into_element(),
|
||||
blend_mode: *blend_mode.element(),
|
||||
angle: *angle.element(),
|
||||
distance: *distance.element(),
|
||||
@@ -468,7 +468,7 @@ fn satin<T>(
|
||||
stamp_effect(
|
||||
&mut content,
|
||||
Effect::new(&EffectOperation::Satin {
|
||||
color: evaluate_color_effects(color).into_element(),
|
||||
color: evaluate_color_parameter(color).into_element(),
|
||||
blend_mode: *blend_mode.element(),
|
||||
angle: *angle.element(),
|
||||
distance: *distance.element(),
|
||||
@@ -620,9 +620,9 @@ fn bevel_and_emboss<T: PaintFitBounds>(
|
||||
soften: *soften.element(),
|
||||
angle: *angle.element(),
|
||||
altitude: *altitude.element(),
|
||||
highlight_color: evaluate_color_effects(highlight_color).into_element(),
|
||||
highlight_color: evaluate_color_parameter(highlight_color).into_element(),
|
||||
highlight_blend_mode: *highlight_blend_mode.element(),
|
||||
shadow_color: evaluate_color_effects(shadow_color).into_element(),
|
||||
shadow_color: evaluate_color_parameter(shadow_color).into_element(),
|
||||
shadow_blend_mode: *shadow_blend_mode.element(),
|
||||
texture,
|
||||
texture_depth: *texture_depth.element(),
|
||||
@@ -661,11 +661,7 @@ fn paint_overlay<T: PaintFitBounds>(
|
||||
let paint = match paint_with_envelope_effects(paint) {
|
||||
Graphic::None(_) | Graphic::NoneList(_) => return content,
|
||||
// A solid paint becomes a bare color, so its own opacity fades the color's alpha
|
||||
Graphic::Color(color) => {
|
||||
let opacity = color.attribute_cloned_or::<f64>(ATTR_OPACITY, 1.) * color.attribute_cloned_or::<f64>(ATTR_OPACITY_FILL, 1.);
|
||||
let color = evaluate_color_effects(color).into_element();
|
||||
Graphic::from(color.with_alpha(color.a() * opacity as f32))
|
||||
}
|
||||
Graphic::Color(color) => Graphic::from(evaluate_color_parameter(color).into_element()),
|
||||
paint => paint_with_placement(&content, paint, _has_transform, _transform),
|
||||
};
|
||||
|
||||
@@ -775,6 +771,33 @@ mod test {
|
||||
assert!(color.r() > 0.05 && color.r() < 0.95, "half the black paint leaves {color:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_shadow_fades_its_color_by_the_colors_own_opacity() {
|
||||
use core_types::ATTR_OPACITY;
|
||||
use core_types::transfer_curve::TransferCurve;
|
||||
use graphic_types::EffectOperation;
|
||||
|
||||
let number = |value: f64| Item::new_from_element(value);
|
||||
let shadowed = super::drop_shadow(
|
||||
(),
|
||||
Item::new_from_element(Raster::new_cpu(Image::new(1, 1, Color::WHITE))),
|
||||
Item::new_from_element(Color::BLACK).with_attribute(ATTR_OPACITY, 0.5_f64),
|
||||
BlendMode::Normal.into(),
|
||||
number(120.),
|
||||
number(5.),
|
||||
number(0.),
|
||||
number(5.),
|
||||
Item::new_from_element(TransferCurve::default()),
|
||||
true.into(),
|
||||
);
|
||||
|
||||
let operation = shadowed.attribute::<Effects>(ATTR_EFFECTS).and_then(|effects| effects.operations().next());
|
||||
let Some(EffectOperation::DropShadow { color, .. }) = operation else {
|
||||
panic!("expected a drop shadow, got {operation:?}")
|
||||
};
|
||||
assert_eq!(color, Color::BLACK.with_alpha(0.5));
|
||||
}
|
||||
|
||||
type PaintEffectNode<T> = fn(Item<T>, Graphic, bool, DAffine2) -> Item<T>;
|
||||
|
||||
fn paint_effect_nodes<T: super::PaintFitBounds>() -> [(&'static str, PaintEffectNode<T>); 3] {
|
||||
|
||||
@@ -53,7 +53,7 @@ fn create_artboard<T: IntoGraphicList>(
|
||||
let normalized_location = location.min(location + dimensions);
|
||||
let normalized_dimensions = dimensions.abs().max(DVec2::ONE);
|
||||
|
||||
let background = raster_kernels::evaluate_color_effects(background).into_element();
|
||||
let background = raster_kernels::evaluate_color_parameter(background).into_element();
|
||||
|
||||
// Name is not stored here, it's resolved live from the parent layer's display name
|
||||
Ok(Item::new_from_element(Artboard::new(content))
|
||||
|
||||
@@ -1142,7 +1142,9 @@ fn flatten_gradient(_: impl Ctx, content: List<Graphic>) -> List<Gradient> {
|
||||
/// Constructs a gradient from a `Color[]`, where each color becomes a gradient stop. A `position` attribute on the colors places their stops along the ramp and a `midpoint` attribute skews each transition, while colors carrying neither are distributed evenly across the 0 to 1 range.
|
||||
#[node_macro::node(category("Gradient"), icon("NodeColorsToGradient"), name("Colors to Gradient"))]
|
||||
fn colors_to_gradient(_: impl Ctx, colors: List<Graphic>) -> Item<Gradient> {
|
||||
Item::new_from_element(Gradient::from(colors.into_flattened_list::<Color>()))
|
||||
// Each color's own effects and opacity go into it before it becomes a stop
|
||||
let colors: List<Color> = colors.into_flattened_list::<Color>().into_iter().map(value_effects::evaluate_color_parameter).collect();
|
||||
Item::new_from_element(Gradient::from(colors))
|
||||
}
|
||||
|
||||
/// 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.
|
||||
@@ -1423,4 +1425,18 @@ mod test {
|
||||
expected.set_positions(&[0.25, 0.75]);
|
||||
assert_eq!(colors_to_gradient((), embed_colors(colors)).element(), &expected, "each stop should keep its place");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn colors_to_gradient_carries_out_each_colors_effects_and_opacity() {
|
||||
use core_types::ATTR_OPACITY;
|
||||
use graphic_types::{Adjustment, EffectOperation};
|
||||
|
||||
let inverted = Item::new_from_element(Color::BLACK).with_attribute(ATTR_EFFECTS, Effects::from_operations([EffectOperation::Adjustment(Adjustment::Invert)]));
|
||||
let half_opacity = Item::new_from_element(Color::RED).with_attribute(ATTR_OPACITY, 0.5_f64);
|
||||
let colors: List<Color> = [inverted, half_opacity].into_iter().collect();
|
||||
|
||||
let gradient = colors_to_gradient((), embed_colors(colors));
|
||||
let expected = Gradient::from(vec![Color::WHITE, Color::RED.with_alpha(0.5)]);
|
||||
assert_eq!(gradient.element(), &expected, "the inverted black should become a white stop, and the half-opaque red a half-alpha one");
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ use graphic_types::adjustment::{
|
||||
pub use graphic_types::adjustment::{DesaturateMethod, RelativeAbsolute};
|
||||
pub use graphic_types::generator::{CellularDistanceFunction, CellularReturnType, DomainWarpType, FractalType, MandelbrotExterior, MandelbrotFractal, MandelbrotInterior, NoiseType};
|
||||
use graphic_types::{Effect, EffectOperation, Generator, Graphic, Vector, stamp_effect};
|
||||
use raster_kernels::{evaluate_color_effects, evaluate_gradient_effects};
|
||||
use raster_kernels::{evaluate_color_parameter, evaluate_gradient_effects};
|
||||
use raster_types::{CPU, GPU, Image, Raster};
|
||||
use vector_types::Gradient;
|
||||
|
||||
@@ -414,7 +414,7 @@ fn black_and_white<T>(
|
||||
) -> Item<T> {
|
||||
let settings = BlackAndWhiteSettings {
|
||||
use_tint: use_tint.into_element(),
|
||||
tint: evaluate_color_effects(tint).into_element(),
|
||||
tint: evaluate_color_parameter(tint).into_element(),
|
||||
reds: reds.into_element(),
|
||||
yellows: yellows.into_element(),
|
||||
greens: greens.into_element(),
|
||||
@@ -1417,7 +1417,7 @@ fn photo_filter<T>(
|
||||
adjust(
|
||||
image,
|
||||
Adjustment::PhotoFilter {
|
||||
color: evaluate_color_effects(color).into_element(),
|
||||
color: evaluate_color_parameter(color).into_element(),
|
||||
density: density.into_element(),
|
||||
preserve_luminosity: preserve_luminosity.into_element(),
|
||||
},
|
||||
@@ -1460,6 +1460,7 @@ mod _graphene_hash_impls {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use raster_kernels::evaluate_color_effects;
|
||||
|
||||
/// Matched to within one 8-bit level.
|
||||
fn assert_close(actual: [f32; 3], expected: [f32; 3]) {
|
||||
|
||||
@@ -18,8 +18,8 @@ fn mix_colors(
|
||||
#[default(100.)]
|
||||
opacity: Item<f64>,
|
||||
) -> Item<Color> {
|
||||
let mut under = raster_kernels::evaluate_color_effects(under);
|
||||
let over = raster_kernels::evaluate_color_effects(over);
|
||||
let mut under = raster_kernels::evaluate_color_parameter(under);
|
||||
let over = raster_kernels::evaluate_color_parameter(over);
|
||||
let blend_mode = blend_mode.into_element();
|
||||
let opacity = (opacity.into_element() / 100.) as f32;
|
||||
|
||||
@@ -41,4 +41,20 @@ mod test {
|
||||
|
||||
assert_eq!(mixed, Color::from_rgbaf32_unchecked(0.25, 0., 0.75, 1.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mix_colors_fades_each_color_by_its_opacity() {
|
||||
use core_types::ATTR_OPACITY;
|
||||
use core_types::list::Item;
|
||||
|
||||
let half_opacity = |color: Color| Item::new_from_element(color).with_attribute(ATTR_OPACITY, 0.5_f64);
|
||||
let mix = |under: Item<Color>, over: Item<Color>| super::mix_colors((), under, over, BlendMode::Normal.into(), 100_f64.into());
|
||||
|
||||
assert_eq!(*mix(Color::TRANSPARENT.into(), half_opacity(Color::RED)).element(), Color::RED.with_alpha(0.5));
|
||||
|
||||
// The under color's opacity goes into its alpha, so the result carries none to fade it twice
|
||||
let mixed = mix(half_opacity(Color::BLUE), Color::TRANSPARENT.into());
|
||||
assert_eq!(*mixed.element(), Color::BLUE.with_alpha(0.5));
|
||||
assert_eq!(mixed.attribute_cloned_or::<f64>(ATTR_OPACITY, 1.), 1.);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user