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
@@ -8,7 +8,7 @@ use crate::generators::pattern_sampler;
|
||||
use crate::layer_blending::blend_layer;
|
||||
use crate::masks::{feather_strength, mask_strength, masked, masked_box_blur_luminances, masked_radius_blur, masked_radius_blur_codes, variable_box_blur};
|
||||
use crate::resample_kernels::{MotionBlurShear, displaced, frame_warped, halftoned, motion_blurred, pixelated, radial_blurred};
|
||||
use crate::value_effects::{adjusted_gradient, adjustments, evaluate_gradient_effects, filled_color};
|
||||
use crate::value_effects::{adjusted_gradient_with_blend_ifs, adjustments, content_blend_ifs, evaluate_gradient_effects, filled_color_with_blend_ifs};
|
||||
use crate::warp_kernels::WarpRasterizer;
|
||||
use core_types::FallibleVec2Operations;
|
||||
use core_types::blending::BlendMode;
|
||||
@@ -19,6 +19,7 @@ use core_types::{ATTR_BLEND_MODE, ATTR_OPACITY, ATTR_OPACITY_FILL, ATTR_TRANSFOR
|
||||
use glam::{DAffine2, DMat2, DVec2};
|
||||
use graphic_types::Graphic;
|
||||
use graphic_types::adjustment::Adjustment;
|
||||
use graphic_types::blend_if::{ChannelRanges, blend_ifs_acting_before, paint_overlay_folds};
|
||||
use graphic_types::effect_layers::{EffectLayers, LayerPart, PiecePlace};
|
||||
use graphic_types::effects::{
|
||||
BevelDirection, BevelStyle, BevelTechnique, EffectCapture, EffectOperation, Effects, GlowFill, GlowSource, GlowTechnique, OutlineFill, bevel_light, contour_gradient_span, effect_space,
|
||||
@@ -31,6 +32,7 @@ use graphic_types::resampling::{DisplacementField, EffectFrame, FrameWarp, Halft
|
||||
use raster_types::parallel_rows::fill_rows;
|
||||
use raster_types::{CPU, Image, Raster};
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Range;
|
||||
use vector_types::gradient::{Gradient, GradientForm, GradientSettings, GradientSpread};
|
||||
|
||||
mod styles;
|
||||
@@ -118,9 +120,22 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Each row's run and its place in it, which Blend Ifs act within as the renderers' runs do
|
||||
let operations: Vec<EffectOperation> = rows.iter().map(|(operation, ..)| operation.clone()).collect();
|
||||
let runs: Vec<Range<usize>> = effects.runs().into_iter().map(|(_, rows)| rows).collect();
|
||||
let in_run = |index: usize| match runs.iter().find(|rows| rows.contains(&index)) {
|
||||
Some(rows) => (&operations[rows.clone()], index - rows.start),
|
||||
None => (&operations[..], index),
|
||||
};
|
||||
|
||||
// Unless a fill fades them first, leading adjustments flattened into the image change its own colors, keeping even transparent pixels' colors
|
||||
let layers = EffectLayers::plan_runs(effects, BlendMode::Normal, fill, true);
|
||||
let leading_adjustments = rows.iter().take(layers.flattened).take_while(|_| fill == 1.).map_while(|(operation, ..)| match operation {
|
||||
let overlay_folds = |index: usize| {
|
||||
let (run, row) = in_run(index);
|
||||
paint_overlay_folds(run, row)
|
||||
};
|
||||
let leading_adjustments = (rows.iter().enumerate().take(layers.flattened).take_while(|_| fill == 1.)).map_while(|(index, (operation, ..))| match operation {
|
||||
EffectOperation::Adjustment(Adjustment::ColorOverlay { .. }) if !overlay_folds(index) => None,
|
||||
EffectOperation::Adjustment(adjustment) => Some(adjustment),
|
||||
_ => None,
|
||||
});
|
||||
@@ -181,9 +196,13 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
let mut pieces: HashMap<EffectCapture, Plane<[f32; 4]>> = HashMap::new();
|
||||
// How much the trailing masks together show of each finished layer
|
||||
let mut layer_fade: Option<Plane<f32>> = None;
|
||||
// How strongly the Blend Ifs so far show the content, which fades it like a fill varying across it
|
||||
let mut blend_if_weight: Option<Plane<f32>> = None;
|
||||
|
||||
// Each run after the first takes the result of those before it as its content at full strength, like the renderers' nested runs
|
||||
let mut run_fill = fill;
|
||||
// The leading adjustments carried out on the image's colors still start stretches that Blend Ifs act at the start of
|
||||
let mut rows_reached = 0;
|
||||
for (index, &(ref operation, effect_to_pixels, starts_later_run)) in rows.iter().enumerate().skip(leading) {
|
||||
if starts_later_run {
|
||||
full = match (filled.take(), beneath.take()) {
|
||||
@@ -192,7 +211,23 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
(None, None) => full,
|
||||
};
|
||||
run_fill = 1.;
|
||||
blend_if_weight = None;
|
||||
}
|
||||
|
||||
let acting_blend_ifs = (rows_reached..=index).flat_map(|row| {
|
||||
let (run, row) = in_run(row);
|
||||
blend_ifs_acting_before(run, row)
|
||||
});
|
||||
for ranges in acting_blend_ifs.filter(|ranges| !ranges.content.show_everything()) {
|
||||
let weight = blend_if_weights(&full, &ranges.content);
|
||||
filled = Some(filled.as_ref().unwrap_or(&full).zip(&weight, |pixel, weight| pixel.map(|channel| channel * weight)));
|
||||
blend_if_weight = Some(match blend_if_weight {
|
||||
Some(previous) => previous.zip(&weight, |previous, weight| previous * weight),
|
||||
None => weight,
|
||||
});
|
||||
}
|
||||
rows_reached = index + 1;
|
||||
|
||||
if operation.changes_nothing() {
|
||||
continue;
|
||||
}
|
||||
@@ -210,6 +245,7 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
let layered = index >= layers.flattened;
|
||||
let piece = |plane: usize| layered.then_some(EffectCapture { index, plane });
|
||||
let knockout_fill = if layered { 1. } else { run_fill };
|
||||
let knockout_weight = blend_if_weight.as_ref().filter(|_| !layered);
|
||||
|
||||
match *operation {
|
||||
EffectOperation::Blur { radius, box_blur, gamma, .. } if !operation.varies_blur_radius() => {
|
||||
@@ -273,7 +309,7 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
let silhouette = full.map(|[_, _, _, alpha]| alpha).shifted(offset.x, offset.y);
|
||||
let strength = cast(silhouette, size, spread, GlowTechnique::Softer, 100., contour, pixels_per_length);
|
||||
let shadow = tinted(&strength, color);
|
||||
let shadow = if knockout { knocked_out(shadow, &full, knockout_fill) } else { shadow };
|
||||
let shadow = if knockout { knocked_out(shadow, &full, knockout_fill, knockout_weight) } else { shadow };
|
||||
place_beneath(&mut pieces, piece(0), &mut beneath, shadow);
|
||||
}
|
||||
EffectOperation::OuterGlow {
|
||||
@@ -293,7 +329,7 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
let strength = shaped(raw.clone(), range, contour);
|
||||
let grain = styles::GlowGrain::new(&paint, noise, jitter, seed, pixels_to_effect);
|
||||
let fades = matches!(paint, GlowFill::Gradient { .. }) || noise > 0.;
|
||||
let glow = knocked_out(glow_tint(&paint, &strength, &raw, fades, grain.as_ref()), &full, knockout_fill);
|
||||
let glow = knocked_out(glow_tint(&paint, &strength, &raw, fades, grain.as_ref()), &full, knockout_fill, knockout_weight);
|
||||
place_beneath(&mut pieces, piece(0), &mut beneath, glow);
|
||||
}
|
||||
EffectOperation::InnerShadow {
|
||||
@@ -411,7 +447,7 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
|
||||
// The band outside lies beneath the content like a shadow, and the band inside over it like an inner shadow
|
||||
if let Some(band) = &bands.outside {
|
||||
let band = knocked_out(painted(band), &full, knockout_fill);
|
||||
let band = knocked_out(painted(band), &full, knockout_fill, knockout_weight);
|
||||
place_beneath(&mut pieces, piece(0), &mut beneath, band);
|
||||
}
|
||||
if let Some(band) = &bands.inside {
|
||||
@@ -492,7 +528,7 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
place_within(&mut pieces, piece(plane), &mut full, &mut filled, tinted(&amount, color), blend_mode);
|
||||
}
|
||||
for (plane, (amount, (color, _))) in outside.into_iter().flatten().zip(colors).enumerate() {
|
||||
let slope = knocked_out(tinted(&amount, color), &full, knockout_fill);
|
||||
let slope = knocked_out(tinted(&amount, color), &full, knockout_fill, knockout_weight);
|
||||
place_beneath(&mut pieces, piece(plane + 2), &mut beneath, slope);
|
||||
}
|
||||
}
|
||||
@@ -511,8 +547,8 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
};
|
||||
place_within(&mut pieces, piece(0), &mut full, &mut filled, painted, blend_mode);
|
||||
}
|
||||
// An image of its own has nothing beneath it to blur
|
||||
EffectOperation::BackgroundBlur { .. } => {}
|
||||
// An image of its own has nothing beneath it to blur, or for Blend If to read, which fades the content where it acts
|
||||
EffectOperation::BackgroundBlur { .. } | EffectOperation::BlendIf(_) => {}
|
||||
// A solid paint overlay paints within the content alone, like any other paint overlay
|
||||
EffectOperation::Adjustment(Adjustment::ColorOverlay { color, blend_mode, opacity }) => {
|
||||
let painted = premultiplied_gamma_channels(color.with_alpha(color.a() * (opacity / 100.) as f32));
|
||||
@@ -672,7 +708,7 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
|
||||
let Some(frame) = &frame else { continue };
|
||||
let Some(screens) = HalftoneScreen::of(operation, frame) else { continue };
|
||||
|
||||
// Each pixel's share of the content that the fill left fades the printed dots, rather than printing the faded content's fainter dots
|
||||
// Each pixel's share of the content that the fill and the Blend Ifs left fades the printed dots, rather than printing the faded content's fainter dots
|
||||
merge_beneath(&mut full, &mut filled, &mut beneath);
|
||||
let shares = filled.as_ref().map(|filled| crate::filter_kernels::fill_shares(&full, filled));
|
||||
let unfaded = filled.map(|filled| crate::filter_kernels::at_full_coverage(&filled, &full));
|
||||
@@ -881,19 +917,31 @@ fn tinted(coverage: &Plane<f32>, color: Color) -> Plane<[f32; 4]> {
|
||||
coverage.map(|coverage| color.map(|channel| channel * coverage))
|
||||
}
|
||||
|
||||
/// A tinted image of what lies beneath, hidden behind the full-strength content where the content covers it as strongly as `fill`.
|
||||
fn knocked_out(tinted: Plane<[f32; 4]>, full_content: &Plane<[f32; 4]>, fill: f32) -> Plane<[f32; 4]> {
|
||||
if fill >= 1. {
|
||||
/// A tinted image of what lies beneath, hidden behind the full-strength content where the content covers it as strongly as `fill`, times the Blend If weight if any fades it.
|
||||
fn knocked_out(tinted: Plane<[f32; 4]>, full_content: &Plane<[f32; 4]>, fill: f32, blend_if_weight: Option<&Plane<f32>>) -> Plane<[f32; 4]> {
|
||||
if fill >= 1. && blend_if_weight.is_none() {
|
||||
return tinted;
|
||||
}
|
||||
|
||||
// The faded content shows what's beneath through 1 - fill × alpha of it, of which only 1 - alpha may show this
|
||||
tinted.zip(full_content, |tinted, content| {
|
||||
let shown = (1. - content[3]) / (1. - fill * content[3]).max(f32::EPSILON);
|
||||
let covering = match blend_if_weight {
|
||||
Some(weight) => full_content.zip(weight, |content, weight| [content[3], weight * fill, 0., 0.]),
|
||||
None => full_content.map(|content| [content[3], fill, 0., 0.]),
|
||||
};
|
||||
tinted.zip(&covering, |tinted, [alpha, fill, ..]| {
|
||||
let shown = (1. - alpha) / (1. - fill * alpha).max(f32::EPSILON);
|
||||
tinted.map(|channel| channel * shown)
|
||||
})
|
||||
}
|
||||
|
||||
/// How strongly Blend If's `ranges` show each pixel of a premultiplied gamma-encoded `image`, by its straight color.
|
||||
fn blend_if_weights(image: &Plane<[f32; 4]>, ranges: &ChannelRanges) -> Plane<f32> {
|
||||
image.map(|[red, green, blue, alpha]| {
|
||||
let straight = if alpha > 0. { [red, green, blue].map(|channel| (channel / alpha) as f64) } else { [0.; 3] };
|
||||
ranges.weight(straight) as f32
|
||||
})
|
||||
}
|
||||
|
||||
/// Places a tinted image beneath the content: as the layered `piece` if it names one, or else stacked over what lies `beneath` the content so far.
|
||||
fn place_beneath(pieces: &mut HashMap<EffectCapture, Plane<[f32; 4]>>, piece: Option<EffectCapture>, beneath: &mut Option<Plane<[f32; 4]>>, tinted: Plane<[f32; 4]>) {
|
||||
match piece {
|
||||
@@ -1192,8 +1240,9 @@ fn paint_sampler(paint: &Graphic) -> Option<Box<dyn Fn(DVec2) -> Color + Sync +
|
||||
// The fill fades the color beneath its paint overlays, which paint over it at full strength, as the renderers fill an endless color
|
||||
let opacity = item.attribute_cloned_or::<f64>(ATTR_OPACITY, 1.) as f32;
|
||||
let fill = item.attribute_cloned_or::<f64>(ATTR_OPACITY_FILL, 1.) as f32;
|
||||
let adjustments = item.attribute::<Effects>(ATTR_EFFECTS).map(adjustments).unwrap_or_default();
|
||||
let color = filled_color(*item.element(), &adjustments, fill);
|
||||
let effects = item.attribute::<Effects>(ATTR_EFFECTS);
|
||||
let (adjustments, blend_ifs) = (effects.map(adjustments).unwrap_or_default(), effects.map(content_blend_ifs).unwrap_or_default());
|
||||
let color = filled_color_with_blend_ifs(*item.element(), &adjustments, &blend_ifs, fill);
|
||||
Some(Box::new(move |_| faded(color, opacity)))
|
||||
}
|
||||
Graphic::RasterCPU(item) => {
|
||||
@@ -1344,12 +1393,13 @@ fn displacement_map_sampler(map: &Graphic) -> Option<Box<dyn Fn(DVec2) -> [f32;
|
||||
Some(Box::new(move |point| sample_gamma(&image, to_pixels.transform_point2(point)).map(|channel| channel * opacity)))
|
||||
}
|
||||
Graphic::Gradient(item) => {
|
||||
// The GPU fades a gradient by its fill in the layer of its opacity, unless adjustments act on the ramp the fill fades
|
||||
let adjustments = item.attribute::<Effects>(ATTR_EFFECTS).filter(|effects| !effects.change_nothing()).map(adjustments).unwrap_or_default();
|
||||
// The GPU fades a gradient by its fill in the layer of its opacity, unless effects act on the ramp the fill fades
|
||||
let effects = item.attribute::<Effects>(ATTR_EFFECTS).filter(|effects| !effects.change_nothing());
|
||||
let (adjustments, blend_ifs) = (effects.map(adjustments).unwrap_or_default(), effects.map(content_blend_ifs).unwrap_or_default());
|
||||
let opacity = item.attribute_cloned_or::<f64>(ATTR_OPACITY, 1.) as f32;
|
||||
let fill = item.attribute_cloned_or::<f64>(ATTR_OPACITY_FILL, 1.) as f32;
|
||||
let settings = GradientSettings::from_item_attributes(item);
|
||||
let (gradient, settings, opacity) = match adjusted_gradient(item.element(), settings, &adjustments, fill) {
|
||||
let (gradient, settings, opacity) = match adjusted_gradient_with_blend_ifs(item.element(), settings, &adjustments, &blend_ifs, fill) {
|
||||
Some((adjusted, adjusted_settings)) => (std::borrow::Cow::Owned(adjusted), adjusted_settings, opacity),
|
||||
None => (std::borrow::Cow::Borrowed(item.element()), settings, opacity * fill),
|
||||
};
|
||||
@@ -1363,12 +1413,16 @@ fn displacement_map_sampler(map: &Graphic) -> Option<Box<dyn Fn(DVec2) -> [f32;
|
||||
}))
|
||||
}
|
||||
Graphic::Color(item) => {
|
||||
// The GPU fills a solid color from 8-bit channels in a layer of its opacity, which the fill fades too unless adjustments act on the faded color
|
||||
let adjustments = item.attribute::<Effects>(ATTR_EFFECTS).filter(|effects| !effects.change_nothing()).map(adjustments).unwrap_or_default();
|
||||
// The GPU fills a solid color from 8-bit channels in a layer of its opacity, which the fill fades too unless effects act on the faded color
|
||||
let effects = item.attribute::<Effects>(ATTR_EFFECTS).filter(|effects| !effects.change_nothing());
|
||||
let (adjustments, blend_ifs) = (effects.map(adjustments).unwrap_or_default(), effects.map(content_blend_ifs).unwrap_or_default());
|
||||
let fill = item.attribute_cloned_or::<f64>(ATTR_OPACITY_FILL, 1.) as f32;
|
||||
let (color, opacity) = match adjustments.is_empty() {
|
||||
let (color, opacity) = match adjustments.is_empty() && blend_ifs.is_empty() {
|
||||
true => (*item.element(), paint_opacity(item)),
|
||||
false => (filled_color(*item.element(), &adjustments, fill), item.attribute_cloned_or::<f64>(ATTR_OPACITY, 1.) as f32),
|
||||
false => (
|
||||
filled_color_with_blend_ifs(*item.element(), &adjustments, &blend_ifs, fill),
|
||||
item.attribute_cloned_or::<f64>(ATTR_OPACITY, 1.) as f32,
|
||||
),
|
||||
};
|
||||
let (color, opacity) = (core_types::color::SRGBA8::from(color), layer_opacity(opacity));
|
||||
|
||||
@@ -2449,6 +2503,60 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn halftones_keep_the_fade_of_blend_ifs_before_them() {
|
||||
use graphic_types::blend_if::{BlendIfRanges, BlendRange};
|
||||
|
||||
// A Blend If hiding the white content stays hiding it once a halftone prints it, which white leaves as white
|
||||
let mut ranges = BlendIfRanges::default();
|
||||
ranges.content.gray = BlendRange {
|
||||
falloff_start: 0.,
|
||||
range_start: 0.,
|
||||
range_end: 50.,
|
||||
falloff_end: 50.,
|
||||
};
|
||||
let halftone = EffectOperation::ColorHalftone {
|
||||
radius: 4.,
|
||||
red_angle: 45.,
|
||||
green_angle: 45.,
|
||||
blue_angle: 45.,
|
||||
offset: DVec2::ZERO,
|
||||
phase: 0.,
|
||||
};
|
||||
let mut item = Item::new_from_element(Raster::new_cpu(Image::new(24, 24, Color::WHITE))).with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::splat(24.)));
|
||||
[EffectOperation::BlendIf(Box::new(ranges)), halftone]
|
||||
.iter()
|
||||
.for_each(|operation| graphic_types::stamp_effect(&mut item, graphic_types::Effect::new(operation)));
|
||||
|
||||
let evaluated = evaluate_raster_effects(item);
|
||||
assert!(evaluated.element().data.iter().all(|color| color.a() < 1e-5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blend_ifs_still_act_after_leading_adjustments_that_change_nothing() {
|
||||
use graphic_types::blend_if::{BlendIfRanges, BlendRange};
|
||||
|
||||
// A Blend If hiding the white content hides it before the invert, though the image's own colors take the Make Opaque of no strength ahead of it
|
||||
let mut ranges = BlendIfRanges::default();
|
||||
ranges.content.gray = BlendRange {
|
||||
falloff_start: 0.,
|
||||
range_start: 0.,
|
||||
range_end: 50.,
|
||||
falloff_end: 50.,
|
||||
};
|
||||
let mut item = Item::new_from_element(Raster::new_cpu(Image::new(8, 8, Color::WHITE))).with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::splat(8.)));
|
||||
[
|
||||
EffectOperation::Adjustment(Adjustment::MakeOpaque { strength: 0. }),
|
||||
EffectOperation::BlendIf(Box::new(ranges)),
|
||||
EffectOperation::Adjustment(Adjustment::Invert),
|
||||
]
|
||||
.iter()
|
||||
.for_each(|operation| graphic_types::stamp_effect(&mut item, graphic_types::Effect::new(operation)));
|
||||
|
||||
let evaluated = evaluate_raster_effects(item);
|
||||
assert!(evaluated.element().data.iter().all(|color| color.a() < 1e-5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn halftones_too_fine_to_print_show_their_tone() {
|
||||
// Dots a fraction of a pixel apart print the tone they average to, which for opaque content is the content
|
||||
|
||||
@@ -7,6 +7,7 @@ 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 graphic_types::adjustment::Adjustment;
|
||||
use graphic_types::blend_if::{ChannelRanges, blend_ifs_acting_before, fades_content};
|
||||
use graphic_types::effects::{EffectOperation, Effects};
|
||||
use std::hash::{DefaultHasher, Hasher};
|
||||
use std::sync::{Mutex, PoisonError};
|
||||
@@ -29,6 +30,47 @@ pub fn adjustments(effects: &Effects) -> Vec<Adjustment> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A step a value's Blend Ifs take among its adjustments (see [`content_blend_ifs`]).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum BlendIfStep {
|
||||
/// A Blend If reads the value's colors and fades the value by these content ranges, as a fill does.
|
||||
Fade(ChannelRanges),
|
||||
/// An effect run after the first starts (see [`Effects::runs`]), taking the value as the runs before it leave it as its content at full strength.
|
||||
NextRun,
|
||||
}
|
||||
|
||||
/// Where each Blend If among the effects that fades a value by its own colors reads it, after how many of the [`adjustments`], with where each effect run after the first starts.
|
||||
/// Empty if no Blend If fades the value.
|
||||
pub fn content_blend_ifs(effects: &Effects) -> Vec<(usize, BlendIfStep)> {
|
||||
let operations: Vec<EffectOperation> = effects.operations().collect();
|
||||
if !fades_content(&operations) {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
// Each run's Blend Ifs act from its own start, as the renderers draw each run's effects on the result of those before it
|
||||
let mut adjustments_before = 0;
|
||||
let mut steps = Vec::new();
|
||||
for (run_index, (_, rows)) in effects.runs().into_iter().enumerate() {
|
||||
if run_index > 0 {
|
||||
steps.push((adjustments_before, BlendIfStep::NextRun));
|
||||
}
|
||||
|
||||
let run = &operations[rows];
|
||||
for (row, operation) in run.iter().enumerate() {
|
||||
let acting = blend_ifs_acting_before(run, row).into_iter().filter(|ranges| !ranges.content.show_everything());
|
||||
steps.extend(acting.map(|ranges| (adjustments_before, BlendIfStep::Fade(ranges.content))));
|
||||
|
||||
adjustments_before += usize::from(matches!(operation, EffectOperation::Adjustment(_)));
|
||||
}
|
||||
}
|
||||
steps
|
||||
}
|
||||
|
||||
/// Whether a Blend If fades a value with effects in more than one run, so later runs read and paint within the result of those before them.
|
||||
pub fn blend_ifs_span_runs(effects: &Effects) -> bool {
|
||||
content_blend_ifs(effects).iter().any(|(_, step)| *step == BlendIfStep::NextRun)
|
||||
}
|
||||
|
||||
/// The color with `adjustments` carried out on it in order.
|
||||
pub fn adjusted_color(color: Color, adjustments: &[Adjustment]) -> Color {
|
||||
adjustments.iter().fold(color, |color, adjustment| adjust_color(adjustment, color))
|
||||
@@ -37,19 +79,60 @@ pub fn adjusted_color(color: Color, adjustments: &[Adjustment]) -> Color {
|
||||
/// The color an endless paint of `color` shows with `adjustments` carried out over it and its `fill` fading it, though not the paint overlays painted over it.
|
||||
/// Without edges, each paint overlay blends with the paint itself, which is exact where the paint is opaque.
|
||||
pub fn filled_color(color: Color, adjustments: &[Adjustment], fill: f32) -> Color {
|
||||
if fill >= 1. {
|
||||
return adjusted_color(color, adjustments);
|
||||
}
|
||||
fill_through(color, adjustments, fill, |index, color| adjust_color(&adjustments[index], color))
|
||||
filled_color_with_blend_ifs(color, adjustments, &[], fill)
|
||||
}
|
||||
|
||||
/// Carries out `adjustments` over a paint faded by `fill` as [`filled_color`] does, with `adjust` carrying out each one besides a paint overlay, given its index.
|
||||
fn fill_through(color: Color, adjustments: &[Adjustment], fill: f32, adjust: impl Fn(usize, Color) -> Color) -> Color {
|
||||
/// Like [`filled_color`], with the steps of the `blend_ifs` (see [`content_blend_ifs`]) also fading the paint by each one's content ranges where it reads the paint's colors.
|
||||
pub fn filled_color_with_blend_ifs(color: Color, adjustments: &[Adjustment], blend_ifs: &[(usize, BlendIfStep)], fill: f32) -> Color {
|
||||
if fill >= 1. && blend_ifs.is_empty() {
|
||||
return adjusted_color(color, adjustments);
|
||||
}
|
||||
fill_through(color, adjustments, blend_ifs, fill, |index, color| adjust_color(&adjustments[index], color), ChannelRanges::weight)
|
||||
}
|
||||
|
||||
/// Carries out `adjustments` over a paint faded by `fill` and the `blend_ifs` as [`filled_color_with_blend_ifs`] does, with `adjust` carrying out each one besides a paint overlay, given its index.
|
||||
/// `weigh` gives how strongly a Blend If's ranges show the gamma-encoded channels it reads.
|
||||
fn fill_through(
|
||||
color: Color,
|
||||
adjustments: &[Adjustment],
|
||||
blend_ifs: &[(usize, BlendIfStep)],
|
||||
fill: f32,
|
||||
adjust: impl Fn(usize, Color) -> Color,
|
||||
mut weigh: impl FnMut(&ChannelRanges, [f64; 3]) -> f64,
|
||||
) -> Color {
|
||||
// Each Blend If fades the faded color by how strongly it shows the full-strength one, which each later run resets to the faded color
|
||||
let mut steps = blend_ifs.iter().peekable();
|
||||
let mut take_steps = |full: &mut Color, filled: &mut Color, adjusted_count: usize| {
|
||||
let mut weight = 1.;
|
||||
let mut faded = false;
|
||||
while let Some((_, step)) = steps.next_if(|(before, _)| *before <= adjusted_count) {
|
||||
match step {
|
||||
BlendIfStep::Fade(ranges) => {
|
||||
let [red, green, blue, _] = full.to_gamma_srgb_channels();
|
||||
weight *= weigh(ranges, [red, green, blue].map(f64::from));
|
||||
faded = true;
|
||||
}
|
||||
BlendIfStep::NextRun => {
|
||||
*filled = filled.with_alpha(filled.a() * std::mem::replace(&mut weight, 1.) as f32);
|
||||
*full = *filled;
|
||||
}
|
||||
}
|
||||
}
|
||||
*filled = filled.with_alpha(filled.a() * weight as f32);
|
||||
faded
|
||||
};
|
||||
|
||||
// The color at full strength, which each paint overlay blends with, and the color the fill fades, which it paints over at full strength
|
||||
let (mut full, mut filled) = (color, color.with_alpha(color.a() * fill));
|
||||
|
||||
// Once a Blend If fades them, adjustments keep the colors empty where they're empty, as the renderers keep empty pixels
|
||||
let mut faded = false;
|
||||
for (index, adjustment) in adjustments.iter().enumerate() {
|
||||
faded |= take_steps(&mut full, &mut filled, index);
|
||||
|
||||
let Adjustment::ColorOverlay { color: paint, blend_mode, opacity } = *adjustment else {
|
||||
(full, filled) = (adjust(index, full), adjust(index, filled));
|
||||
let adjusted = |color: Color| if !faded || color.a() > 0. { adjust(index, color) } else { color };
|
||||
(full, filled) = (adjusted(full), adjusted(filled));
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -64,18 +147,39 @@ fn fill_through(color: Color, adjustments: &[Adjustment], fill: f32, adjust: imp
|
||||
filled = Color::from_gamma_srgb_channels(mix(red, under_red), mix(green, under_green), mix(blue, under_blue), alpha);
|
||||
full = overlay_paint(full, paint, blend_mode, (opacity / 100.) as f32);
|
||||
}
|
||||
take_steps(&mut full, &mut filled, adjustments.len());
|
||||
filled
|
||||
}
|
||||
|
||||
/// Carries out `adjustments` in order on any number of colors with the `fill` fading them, with each one's kernel prepared once.
|
||||
fn prepared(adjustments: &[Adjustment], fill: f32) -> Box<dyn Fn(Color) -> Color> {
|
||||
/// Carries out `adjustments` in order on any number of colors with the `fill` and the `blend_ifs` fading them, with each one's kernel prepared once.
|
||||
fn prepared(adjustments: &[Adjustment], blend_ifs: &[(usize, BlendIfStep)], fill: f32) -> Box<dyn Fn(Color) -> Color> {
|
||||
let kernels: Vec<PreparedAdjustment> = adjustments.iter().map(PreparedAdjustment::new).collect();
|
||||
if fill >= 1. {
|
||||
if fill >= 1. && blend_ifs.is_empty() {
|
||||
return Box::new(move |color| kernels.iter().fold(color, |color, kernel| kernel.adjust(color)));
|
||||
}
|
||||
|
||||
let adjustments = adjustments.to_vec();
|
||||
Box::new(move |color| fill_through(color, &adjustments, fill, |index, color| kernels[index].adjust(color)))
|
||||
let (adjustments, blend_ifs) = (adjustments.to_vec(), blend_ifs.to_vec());
|
||||
Box::new(move |color| fill_through(color, &adjustments, &blend_ifs, fill, |index, color| kernels[index].adjust(color), ChannelRanges::weight))
|
||||
}
|
||||
|
||||
/// Tells apart the colors each channel's range of each of the `blend_ifs` shows fully, partly, or not at all, as it reads them where [`fill_through`] fades by it.
|
||||
fn blend_if_regions(adjustments: &[Adjustment], blend_ifs: &[(usize, BlendIfStep)], fill: f32) -> impl Fn(Color) -> u64 {
|
||||
let kernels: Vec<PreparedAdjustment> = adjustments.iter().map(PreparedAdjustment::new).collect();
|
||||
let (adjustments, blend_ifs) = (adjustments.to_vec(), blend_ifs.to_vec());
|
||||
|
||||
// Each channel's state (hidden, partly shown, or shown) is a digit in base 3, so up to ten Blend Ifs stay apart exactly
|
||||
move |color| {
|
||||
let mut region = 0_u64;
|
||||
let weigh = |ranges: &ChannelRanges, channels: [f64; 3]| {
|
||||
for weight in ranges.channel_weights(channels) {
|
||||
let state = u64::from(weight > 0.) + u64::from(weight >= 1.);
|
||||
region = region.wrapping_mul(3).wrapping_add(state);
|
||||
}
|
||||
ranges.weight(channels)
|
||||
};
|
||||
fill_through(color, &adjustments, &blend_ifs, fill, |index, color| kernels[index].adjust(color), weigh);
|
||||
region
|
||||
}
|
||||
}
|
||||
|
||||
/// The gradient with `adjustments` carried out along its ramp, with the settings to read the result with, or `None` if there are no adjustments.
|
||||
@@ -83,17 +187,28 @@ fn prepared(adjustments: &[Adjustment], fill: f32) -> Box<dyn Fn(Color) -> Color
|
||||
/// A stepped ramp holds each stop's color, so adjusting the stops is exact. Elsewhere an adjustment can bend the ramp between stops,
|
||||
/// so the adjusted ramp is resampled into stops that interpolate linearly in gamma.
|
||||
pub fn adjusted_gradient(gradient: &Gradient, settings: GradientSettings, adjustments: &[Adjustment], fill: f32) -> Option<(Gradient, GradientSettings)> {
|
||||
if adjustments.is_empty() {
|
||||
adjusted_gradient_with_blend_ifs(gradient, settings, adjustments, &[], fill)
|
||||
}
|
||||
|
||||
/// Like [`adjusted_gradient`], with the `blend_ifs` also fading the ramp as [`filled_color_with_blend_ifs`] fades a color, or `None` if there are neither adjustments nor Blend Ifs.
|
||||
pub fn adjusted_gradient_with_blend_ifs(
|
||||
gradient: &Gradient,
|
||||
settings: GradientSettings,
|
||||
adjustments: &[Adjustment],
|
||||
blend_ifs: &[(usize, BlendIfStep)],
|
||||
fill: f32,
|
||||
) -> Option<(Gradient, GradientSettings)> {
|
||||
if adjustments.is_empty() && blend_ifs.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// A gradient without stops reads as black, so its adjusted ramp is one stop of the adjusted black
|
||||
if gradient.is_empty() {
|
||||
return Some((Gradient::from(vec![prepared(adjustments, fill)(Color::BLACK)]), settings));
|
||||
return Some((Gradient::from(vec![prepared(adjustments, blend_ifs, fill)(Color::BLACK)]), settings));
|
||||
}
|
||||
|
||||
if settings.interpolation == GradientInterpolation::Stepped {
|
||||
let adjust = prepared(adjustments, fill);
|
||||
let adjust = prepared(adjustments, blend_ifs, fill);
|
||||
return Some((gradient.map_colors(|color| adjust(*color)), settings));
|
||||
}
|
||||
|
||||
@@ -102,6 +217,16 @@ pub fn adjusted_gradient(gradient: &Gradient, settings: GradientSettings, adjust
|
||||
gradient.cache_hash(&mut hasher);
|
||||
settings.cache_hash(&mut hasher);
|
||||
adjustments.iter().for_each(|adjustment| adjustment.cache_hash(&mut hasher));
|
||||
for (before, step) in blend_ifs {
|
||||
hasher.write_usize(*before);
|
||||
match step {
|
||||
BlendIfStep::Fade(ranges) => {
|
||||
hasher.write_u8(0);
|
||||
ranges.cache_hash(&mut hasher);
|
||||
}
|
||||
BlendIfStep::NextRun => hasher.write_u8(1),
|
||||
}
|
||||
}
|
||||
hasher.write_u32(fill.to_bits());
|
||||
let key = hasher.finish();
|
||||
|
||||
@@ -114,7 +239,11 @@ pub fn adjusted_gradient(gradient: &Gradient, settings: GradientSettings, adjust
|
||||
return Some(result);
|
||||
}
|
||||
|
||||
let resampled = gradient.map_ramp(settings, prepared(adjustments, fill));
|
||||
// A Blend If cuts the ramp off wherever the colors it reads cross its ranges, which may lie between the resampling's probes
|
||||
let resampled = match blend_ifs.is_empty() {
|
||||
true => gradient.map_ramp(settings, prepared(adjustments, blend_ifs, fill)),
|
||||
false => gradient.map_ramp_by_regions(settings, prepared(adjustments, blend_ifs, fill), blend_if_regions(adjustments, blend_ifs, fill)),
|
||||
};
|
||||
let resampled_settings = GradientSettings {
|
||||
space: GradientSpace::RgbGamma,
|
||||
interpolation: GradientInterpolation::Linear,
|
||||
@@ -129,29 +258,43 @@ pub fn adjusted_gradient(gradient: &Gradient, settings: GradientSettings, adjust
|
||||
Some((resampled, resampled_settings))
|
||||
}
|
||||
|
||||
/// The color with the adjustments among its effects carried out and the effects consumed, for nodes that read the color.
|
||||
/// The color with the adjustments among the effects carried out on it, faded by each Blend If where it reads the color, for the paints and nodes that read the value.
|
||||
pub fn color_after_effects(color: Color, effects: &Effects) -> Color {
|
||||
filled_color_with_blend_ifs(color, &adjustments(effects), &content_blend_ifs(effects), 1.)
|
||||
}
|
||||
|
||||
/// Carries out the effects on any number of colors as [`color_after_effects`] does on one, with each adjustment's kernel prepared once.
|
||||
pub fn prepared_color_after_effects(effects: &Effects) -> Box<dyn Fn(Color) -> Color> {
|
||||
prepared(&adjustments(effects), &content_blend_ifs(effects), 1.)
|
||||
}
|
||||
|
||||
/// The gradient with the effects carried out along its ramp as [`color_after_effects`] carries them out on a color, with the settings to read the result with, or `None` with nothing to carry out.
|
||||
pub fn gradient_after_effects(gradient: &Gradient, settings: GradientSettings, effects: &Effects) -> Option<(Gradient, GradientSettings)> {
|
||||
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.
|
||||
/// A color without the effects attribute is returned as it is.
|
||||
pub fn evaluate_color_effects(item: Item<Color>) -> Item<Color> {
|
||||
let Some(adjustments) = item.attribute::<Effects>(ATTR_EFFECTS).map(adjustments) else {
|
||||
let Some(color) = item.attribute::<Effects>(ATTR_EFFECTS).map(|effects| color_after_effects(*item.element(), effects)) else {
|
||||
return item;
|
||||
};
|
||||
|
||||
let mut item = item;
|
||||
*item.element_mut() = adjusted_color(*item.element(), &adjustments);
|
||||
*item.element_mut() = color;
|
||||
item.remove_attribute::<Effects>(ATTR_EFFECTS);
|
||||
item
|
||||
}
|
||||
|
||||
/// The gradient with the adjustments among its effects carried out along its ramp and the effects consumed, for nodes that read its colors.
|
||||
/// 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> {
|
||||
let Some(adjustments) = item.attribute::<Effects>(ATTR_EFFECTS).map(adjustments) else {
|
||||
return item;
|
||||
};
|
||||
let Some(effects) = item.attribute::<Effects>(ATTR_EFFECTS) else { return item };
|
||||
let settings = GradientSettings::from_item_attributes(&item);
|
||||
let adjusted = gradient_after_effects(item.element(), settings, effects);
|
||||
|
||||
let mut item = item;
|
||||
let settings = match adjusted_gradient(item.element(), settings, &adjustments, 1.) {
|
||||
let settings = match adjusted {
|
||||
Some((gradient, adjusted_settings)) => {
|
||||
*item.element_mut() = gradient;
|
||||
adjusted_settings
|
||||
@@ -180,6 +323,16 @@ mod tests {
|
||||
actual.iter().zip(expected).all(|(actual, expected)| (actual - expected).abs() < 1e-4)
|
||||
}
|
||||
|
||||
/// The `element` with each row stamped on it while it's placed by the row's scale, so a row stamped at a new scale starts a run of its own.
|
||||
fn stamped<T>(element: T, rows: &[(EffectOperation, f64)]) -> Item<T> {
|
||||
let mut item = Item::new_from_element(element);
|
||||
for (operation, scale) in rows {
|
||||
item.set_attribute(core_types::ATTR_TRANSFORM, glam::DAffine2::from_scale(glam::DVec2::splat(*scale)));
|
||||
graphic_types::stamp_effect(&mut item, graphic_types::Effect::new(operation));
|
||||
}
|
||||
item
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paint_overlays_mix_in_gamma_unfaded_by_the_fill() {
|
||||
let half_white = [Adjustment::ColorOverlay {
|
||||
@@ -198,6 +351,162 @@ mod tests {
|
||||
assert!(close(gamma(filled_color(Color::BLACK, &half_white, 0.5)), [2. / 3., 2. / 3., 2. / 3., 0.75]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn consumed_effects_fade_values_by_their_blend_ifs_where_they_read_the_colors() {
|
||||
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 blend_if = EffectOperation::BlendIf(Box::new(hides_darks));
|
||||
let invert = EffectOperation::Adjustment(Adjustment::Invert);
|
||||
let red = EffectOperation::Adjustment(Adjustment::ColorOverlay {
|
||||
color: Color::RED,
|
||||
blend_mode: BlendMode::Normal,
|
||||
opacity: 100.,
|
||||
});
|
||||
let black_through = |operations: Vec<EffectOperation>| evaluate_color_effects(Item::new_from_element(Color::BLACK).with_attribute(ATTR_EFFECTS, Effects::from_operations(operations)));
|
||||
|
||||
// Black is hidden, whether inverted after the Blend If reads it or not, but shows once inverted before
|
||||
assert_eq!(black_through(vec![blend_if.clone()]).element().a(), 0.);
|
||||
assert_eq!(black_through(vec![blend_if.clone(), invert.clone()]).element().a(), 0.);
|
||||
assert!(close(gamma(*black_through(vec![invert, blend_if.clone()]).element()), [1.; 4]));
|
||||
|
||||
// An overlay after it paints over the hidden color at full strength, as over a fill of none, and the effects are consumed
|
||||
let overlaid = black_through(vec![blend_if.clone(), red]);
|
||||
assert!(close(gamma(*overlaid.element()), [1., 0., 0., 1.]));
|
||||
assert!(overlaid.attribute::<Effects>(ATTR_EFFECTS).is_none());
|
||||
|
||||
// A gradient's dark start is hidden and its light end shown
|
||||
let gradient = Item::new_from_element(Gradient::from(vec![Color::BLACK, Color::WHITE])).with_attribute(ATTR_EFFECTS, Effects::from_operations([blend_if]));
|
||||
let evaluated = evaluate_gradient_effects(gradient);
|
||||
let settings = GradientSettings::from_item_attributes(&evaluated);
|
||||
assert!(evaluated.element().evaluate(0., settings).a() < 1e-6);
|
||||
assert!(close(gamma(evaluated.element().evaluate(1., settings)), [1.; 4]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn consumed_values_fade_by_blend_ifs_reading_each_effect_run_as_the_runs_before_it_leave_it() {
|
||||
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 blend_if = EffectOperation::BlendIf(Box::new(hides_darks));
|
||||
let overlay = |color: Color| {
|
||||
EffectOperation::Adjustment(Adjustment::ColorOverlay {
|
||||
color,
|
||||
blend_mode: BlendMode::Normal,
|
||||
opacity: 100.,
|
||||
})
|
||||
};
|
||||
|
||||
// White painted blue is read as blue by a Blend If after a scale, which hides it, but in the same run the Blend If reads the white beneath the overlay
|
||||
let color_through = |color: Color, rows: &[(EffectOperation, f64)]| *evaluate_color_effects(stamped(color, rows)).element();
|
||||
assert_eq!(color_through(Color::WHITE, &[(overlay(Color::BLUE), 1.), (blend_if.clone(), 2.)]).a(), 0.);
|
||||
assert!(close(gamma(color_through(Color::WHITE, &[(overlay(Color::BLUE), 1.), (blend_if.clone(), 1.)])), [0., 0., 1., 1.]));
|
||||
|
||||
// Black hidden before a scale leaves nothing for an overlay after it to paint within
|
||||
assert_eq!(color_through(Color::BLACK, &[(blend_if.clone(), 1.), (overlay(Color::RED), 2.)]).a(), 0.);
|
||||
assert!(close(gamma(color_through(Color::BLACK, &[(blend_if.clone(), 1.), (overlay(Color::RED), 1.)])), [1., 0., 0., 1.]));
|
||||
|
||||
// A ramp painted blue is hidden all along by a Blend If after a scale
|
||||
let ramp = stamped(Gradient::from(vec![Color::BLACK, Color::WHITE]), &[(overlay(Color::BLUE), 1.), (blend_if, 2.)]);
|
||||
let evaluated = evaluate_gradient_effects(ramp);
|
||||
let settings = GradientSettings::from_item_attributes(&evaluated);
|
||||
for t in [0., 0.25, 0.75, 1.] {
|
||||
let shown = evaluated.element().evaluate(t, settings);
|
||||
assert!(shown.a() < 1e-6, "hidden at {t}: {:?}", gamma(shown));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values_a_blend_if_hides_stay_hidden_through_adjustments_raising_alpha_as_rasters_do() {
|
||||
use crate::raster_effects::{evaluate_raster_effects, premultiplied_gamma_channels};
|
||||
use graphic_types::EffectKind;
|
||||
use graphic_types::blend_if::BlendIfRanges;
|
||||
use raster_types::{Image, Raster};
|
||||
|
||||
// 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 blend_if = EffectOperation::BlendIf(Box::new(hides_darks));
|
||||
let make_opaque = EffectOperation::default_for(EffectKind::MakeOpaque);
|
||||
let red = EffectOperation::Adjustment(Adjustment::ColorOverlay {
|
||||
color: Color::RED,
|
||||
blend_mode: BlendMode::Normal,
|
||||
opacity: 100.,
|
||||
});
|
||||
|
||||
// Black consumed as a color, which agrees with a raster's pixel of it
|
||||
let black_through = |name: &str, rows: &[(EffectOperation, f64)]| {
|
||||
let color = *evaluate_color_effects(stamped(Color::BLACK, rows)).element();
|
||||
let raster = evaluate_raster_effects(stamped(Raster::new_cpu(Image::new(1, 1, Color::BLACK)), rows));
|
||||
let pixel = raster.element().data.first().copied().unwrap_or(Color::TRANSPARENT);
|
||||
let (color_channels, pixel_channels) = (premultiplied_gamma_channels(color), premultiplied_gamma_channels(pixel));
|
||||
assert!(close(color_channels, pixel_channels), "black {name}: {color_channels:?} vs the raster's {pixel_channels:?}");
|
||||
color
|
||||
};
|
||||
|
||||
// Hidden black stays hidden when made opaque, in its run or the next, where an overlay finds nothing to paint within either
|
||||
let hidden = [
|
||||
("made opaque", vec![(blend_if.clone(), 1.), (make_opaque.clone(), 1.)]),
|
||||
("made opaque in the next run", vec![(blend_if.clone(), 1.), (make_opaque.clone(), 2.)]),
|
||||
("made opaque and painted in the next run", vec![(blend_if.clone(), 1.), (make_opaque.clone(), 2.), (red.clone(), 2.)]),
|
||||
];
|
||||
for (name, rows) in hidden {
|
||||
assert_eq!(black_through(name, &rows).a(), 0., "black {name}");
|
||||
}
|
||||
|
||||
// In its own run, an overlay still paints within the full-strength black
|
||||
let painted = black_through("made opaque and painted", &[(blend_if.clone(), 1.), (make_opaque.clone(), 1.), (red, 1.)]);
|
||||
assert!(close(gamma(painted), [1., 0., 0., 1.]), "{:?}", gamma(painted));
|
||||
|
||||
// A ramp made opaque stays hidden where it's dark, in the Blend If's run or the next
|
||||
for scale in [1., 2.] {
|
||||
let ramp = stamped(Gradient::from(vec![Color::BLACK, Color::WHITE]), &[(blend_if.clone(), 1.), (make_opaque.clone(), scale)]);
|
||||
let evaluated = evaluate_gradient_effects(ramp);
|
||||
let settings = GradientSettings::from_item_attributes(&evaluated);
|
||||
for (t, alpha) in [(0., 0.), (0.25, 0.), (0.75, 1.), (1., 1.)] {
|
||||
let shown = evaluated.element().evaluate(t, settings).a();
|
||||
assert!((shown - alpha).abs() < 1e-6, "the alpha at {t} with the scale {scale} is {shown}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ramp_keeps_the_band_a_blend_if_shows_between_where_resampling_probes_it() {
|
||||
use graphic_types::blend_if::{BlendIfRanges, BlendRange};
|
||||
|
||||
// Shown only where the gray lies from 30% to 40%, which neither end of the ramp nor any of its quarter points lands within
|
||||
let mut band = BlendIfRanges::default();
|
||||
band.content.gray = BlendRange {
|
||||
falloff_start: 30.,
|
||||
range_start: 30.,
|
||||
range_end: 40.,
|
||||
falloff_end: 40.,
|
||||
};
|
||||
let effects = Effects::from_operations([EffectOperation::BlendIf(Box::new(band))]);
|
||||
let gamma_settings = GradientSettings {
|
||||
space: GradientSpace::RgbGamma,
|
||||
interpolation: GradientInterpolation::Linear,
|
||||
..Default::default()
|
||||
};
|
||||
let (resampled, settings) = gradient_after_effects(&Gradient::from(vec![Color::BLACK, Color::WHITE]), gamma_settings, &effects).expect("the Blend If fades the ramp");
|
||||
|
||||
// The band shows its own grays from level 77 to 102, which lie from 30% to just past 40% along the ramp, with sharp edges
|
||||
for t in [0.301, 0.35, 0.4] {
|
||||
let shown = resampled.evaluate(t, settings);
|
||||
assert!(close(gamma(shown), [t as f32, t as f32, t as f32, 1.]), "the band shows at {t}: {:?}", gamma(shown));
|
||||
}
|
||||
for t in [0., 0.25, 0.299, 0.403, 0.5, 0.75, 1.] {
|
||||
assert!(resampled.evaluate(t, settings).a() < 1e-6, "hidden at {t}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_gradient_without_stops_adjusts_the_black_it_reads_as() {
|
||||
let empty = Gradient::from(Vec::<Color>::new());
|
||||
|
||||
Reference in new issue
Block a user