mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Make gradient evaluation follow the ramp's interpolation space attribute (#4414)
This commit is contained in:
committed by
Dennis Kobert
parent
3678826a48
commit
11dfc27645
@@ -550,8 +550,8 @@ impl Gradient {
|
||||
/// Insert a new stop at the given position, sampling the gradient at that position to determine the new stop's color.
|
||||
/// The new stop's midpoint is inherited from the interval it splits (or `0.5` if inserting at the very start).
|
||||
/// Returns the index where the new stop was inserted.
|
||||
pub fn insert_stop(&mut self, position: f64) -> usize {
|
||||
let color = self.evaluate(position, Default::default());
|
||||
pub fn insert_stop(&mut self, position: f64, gradient_interpolation: GradientInterpolation) -> usize {
|
||||
let color = self.evaluate(position, Default::default(), gradient_interpolation);
|
||||
let index = (0..self.len()).position(|i| self.position(i) > position).unwrap_or(self.len());
|
||||
let midpoint = if index > 0 { self.midpoint(index - 1) } else { 0.5 };
|
||||
self.insert_stop_values(position, midpoint, color)
|
||||
@@ -632,7 +632,7 @@ impl Gradient {
|
||||
}
|
||||
|
||||
/// Samples the gradient's color at `t`. Given a `t` outside the 0 to 1 range, the `gradient_spread` determines how the gradient extends.
|
||||
pub fn evaluate(&self, t: f64, gradient_spread: GradientSpread) -> Color {
|
||||
pub fn evaluate(&self, t: f64, gradient_spread: GradientSpread, gradient_interpolation: GradientInterpolation) -> Color {
|
||||
let t = match gradient_spread {
|
||||
GradientSpread::Pad => t.clamp(0., 1.),
|
||||
GradientSpread::Repeat => t.rem_euclid(1.),
|
||||
@@ -662,8 +662,7 @@ impl Gradient {
|
||||
if t >= a.position && t <= b.position {
|
||||
let normalized_t = (t - a.position) / (b.position - a.position);
|
||||
let adjusted_t = apply_midpoint(normalized_t, a.midpoint);
|
||||
// Sampling deliberately stays in linear light; the ramp's interpolation space attribute only shapes what the renderers draw
|
||||
return a.color.lerp(&b.color, adjusted_t as f32);
|
||||
return interpolate_stop_colors(a.color, b.color, adjusted_t as f32, gradient_interpolation);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -967,7 +966,7 @@ mod tests {
|
||||
fn default_is_empty_and_black_to_white_is_the_artist_starting_gradient() {
|
||||
assert!(Gradient::default().is_empty());
|
||||
assert_eq!(Gradient::black_to_white().positions(), vec![0., 1.]);
|
||||
assert_eq!(Gradient::default().evaluate(0.5, Default::default()), Color::BLACK);
|
||||
assert_eq!(Gradient::default().evaluate(0.5, Default::default(), Default::default()), Color::BLACK);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1157,18 +1156,30 @@ mod tests {
|
||||
fn clear_spread_evaluates_to_transparency_outside_the_unit_range() {
|
||||
let gradient = Gradient::from(vec![Color::BLACK, Color::WHITE]);
|
||||
|
||||
assert_eq!(gradient.evaluate(-0.25, GradientSpread::Clear), Color::TRANSPARENT);
|
||||
assert_eq!(gradient.evaluate(1.25, GradientSpread::Clear), Color::TRANSPARENT);
|
||||
assert_eq!(gradient.evaluate(-0.25, GradientSpread::Clear, Default::default()), Color::TRANSPARENT);
|
||||
assert_eq!(gradient.evaluate(1.25, GradientSpread::Clear, Default::default()), Color::TRANSPARENT);
|
||||
|
||||
for t in [0., 0.25, 1.] {
|
||||
assert_eq!(
|
||||
gradient.evaluate(t, GradientSpread::Clear),
|
||||
gradient.evaluate(t, GradientSpread::Pad),
|
||||
gradient.evaluate(t, GradientSpread::Clear, Default::default()),
|
||||
gradient.evaluate(t, GradientSpread::Pad, Default::default()),
|
||||
"inside the range Clear must match Pad at t = {t}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evaluate_follows_the_interpolation_space() {
|
||||
let gradient = Gradient::from(vec![Color::BLACK, Color::WHITE]);
|
||||
|
||||
let linear = gradient.evaluate(0.5, Default::default(), GradientInterpolation::SrgbLinear);
|
||||
let gamma = gradient.evaluate(0.5, Default::default(), GradientInterpolation::SrgbGamma);
|
||||
|
||||
assert_eq!(linear, Color::BLACK.lerp(&Color::WHITE, 0.5));
|
||||
assert_eq!(gamma, Color::BLACK.lerp_gamma_srgb(&Color::WHITE, 0.5));
|
||||
assert_ne!(linear, gamma, "the two spaces must produce different mid colors between black and white");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gradient_ui_write_back_elides_default_attributes() {
|
||||
let mut gradient = Gradient::from(vec![Color::BLACK, Color::WHITE, Color::RED]);
|
||||
@@ -1209,8 +1220,8 @@ mod tests {
|
||||
assert_eq!(sample_positions.first(), Some(&0.));
|
||||
assert_eq!(sample_positions.last(), Some(&1.));
|
||||
|
||||
assert_eq!(gradient.evaluate(0., Default::default()), Color::RED);
|
||||
assert_eq!(gradient.evaluate(1., Default::default()), Color::WHITE);
|
||||
assert_eq!(gradient.evaluate(0., Default::default(), Default::default()), Color::RED);
|
||||
assert_eq!(gradient.evaluate(1., Default::default(), Default::default()), Color::WHITE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1220,8 +1231,8 @@ mod tests {
|
||||
|
||||
let sample_positions: Vec<f64> = gradient.interpolated_samples(GradientInterpolation::SrgbGamma).iter().map(|(position, ..)| *position).collect();
|
||||
assert_eq!(sample_positions, vec![0., 1.]);
|
||||
assert_eq!(gradient.evaluate(0., Default::default()), Color::BLACK);
|
||||
assert_eq!(gradient.evaluate(1., Default::default()), Color::WHITE);
|
||||
assert_eq!(gradient.evaluate(0., Default::default(), Default::default()), Color::BLACK);
|
||||
assert_eq!(gradient.evaluate(1., Default::default(), Default::default()), Color::WHITE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1231,7 +1242,7 @@ mod tests {
|
||||
|
||||
let sample_positions: Vec<f64> = gradient.interpolated_samples(GradientInterpolation::SrgbGamma).iter().map(|(position, ..)| *position).collect();
|
||||
assert_eq!(sample_positions, vec![0., 1.]);
|
||||
assert_eq!(gradient.evaluate(0.5, Default::default()), Color::WHITE.lerp(&Color::RED, 0.5));
|
||||
assert_eq!(gradient.evaluate(0.5, Default::default(), Default::default()), Color::WHITE.lerp(&Color::RED, 0.5));
|
||||
|
||||
// A non-finite position is preserved as nondefault so write-back elision cannot resurrect the dropped stop
|
||||
assert!(gradient.nondefault_positions().is_some());
|
||||
@@ -1240,7 +1251,7 @@ mod tests {
|
||||
let mut gradient = Gradient::from(vec![Color::WHITE, Color::RED]);
|
||||
gradient.set_positions(&[f64::NAN, f64::NAN]);
|
||||
assert!(gradient.interpolated_samples(GradientInterpolation::SrgbGamma).is_empty());
|
||||
assert_eq!(gradient.evaluate(0.5, Default::default()), Color::BLACK);
|
||||
assert_eq!(gradient.evaluate(0.5, Default::default(), Default::default()), Color::BLACK);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1255,10 +1266,10 @@ mod tests {
|
||||
#[test]
|
||||
fn nan_midpoints_read_as_linear() {
|
||||
let mut gradient = Gradient::from(vec![Color::BLACK, Color::WHITE]);
|
||||
let linear_result = gradient.evaluate(0.25, Default::default());
|
||||
let linear_result = gradient.evaluate(0.25, Default::default(), Default::default());
|
||||
|
||||
gradient.set_midpoints(&[f64::NAN, f64::NAN]);
|
||||
assert_eq!(gradient.evaluate(0.25, Default::default()), linear_result);
|
||||
assert_eq!(gradient.evaluate(0.25, Default::default(), Default::default()), linear_result);
|
||||
let no_nan_annotations = gradient
|
||||
.interpolated_samples(GradientInterpolation::SrgbGamma)
|
||||
.iter()
|
||||
|
||||
@@ -1242,7 +1242,7 @@ fn gradient_midpoints(_: impl Ctx, mut gradient: Gradient, midpoints: List<f64>)
|
||||
gradient
|
||||
}
|
||||
|
||||
/// Evaluates the color at the specified position along the gradient, given a position from 0 (left) to 1 (right). Positions beyond that range follow the gradient's `gradient_spread` attribute: Pad (default), Reflect, Repeat, or Clear.
|
||||
/// Evaluates the color at the specified position along the gradient, given a position from 0 (left) to 1 (right). Positions beyond that range follow the gradient's `gradient_spread` attribute: Pad (default), Reflect, Repeat, or Clear. Colors between stops blend in the gradient's `gradient_interpolation` color space.
|
||||
#[node_macro::node(category("Color"))]
|
||||
fn sample_gradient(
|
||||
ctx: impl Ctx + ExtractIndex + InjectIndex + Copy,
|
||||
@@ -1255,8 +1255,9 @@ fn sample_gradient(
|
||||
return Err(GraphError::past_end().into());
|
||||
}
|
||||
|
||||
let spread_method = gradient.lane(0).attr::<GradientSpreadAttr>();
|
||||
Ok(gradient.element_ref(0).evaluate(position, spread_method))
|
||||
let gradient_spread = gradient.lane(0).attr::<GradientSpreadAttr>();
|
||||
let gradient_interpolation = gradient.lane(0).attr::<GradientInterpolationAttr>();
|
||||
Ok(gradient.element_ref(0).evaluate(position, gradient_spread, gradient_interpolation))
|
||||
}
|
||||
|
||||
/// Constructs a footprint value which may be set to any transformation of a unit square describing a render area, and a render resolution at least 1x1 integer pixels.
|
||||
|
||||
@@ -37,13 +37,16 @@ mod blend_std {
|
||||
}
|
||||
|
||||
impl Blend<Color> for Gradient {
|
||||
// TODO: This joining is unfaithful in several ways: it samples only at stop positions so midpoint curves flatten away;
|
||||
// it evaluates both sources with the default spread and interpolation rather than their own attributes (which this
|
||||
// element-level impl cannot read); and the output keeps over's attributes despite being sampled in the default space
|
||||
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
|
||||
let mut combined_stops = self.positions().into_iter().chain(under.positions()).collect::<Vec<_>>();
|
||||
combined_stops.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
|
||||
combined_stops.dedup_by(|a, b| (*a - *b).abs() < 1e-6);
|
||||
let stops = combined_stops.into_iter().map(|position| {
|
||||
let over_color = self.evaluate(position, Default::default());
|
||||
let under_color = under.evaluate(position, Default::default());
|
||||
let over_color = self.evaluate(position, Default::default(), Default::default());
|
||||
let under_color = under.evaluate(position, Default::default(), Default::default());
|
||||
let color = blend_fn(over_color, under_color);
|
||||
GradientStop { position, midpoint: 0.5, color }
|
||||
});
|
||||
|
||||
@@ -24,12 +24,13 @@ fn gradient_map<T: Adjust<Color> + Clone + Send + Sync + core_types::CacheHash +
|
||||
return image;
|
||||
}
|
||||
let gradient_spread = gradient.lane(0).attr::<vector_types::markers::GradientSpread>();
|
||||
let gradient_interpolation = gradient.lane(0).attr::<vector_types::markers::GradientInterpolation>();
|
||||
let gradient = gradient.element_ref(0);
|
||||
|
||||
image.adjust(|color| {
|
||||
let intensity = color.luminance_rec_709();
|
||||
let intensity = if reverse { 1. - intensity } else { intensity };
|
||||
gradient.evaluate(intensity as f64, gradient_spread)
|
||||
gradient.evaluate(intensity as f64, gradient_spread, gradient_interpolation)
|
||||
});
|
||||
|
||||
image
|
||||
|
||||
@@ -39,13 +39,13 @@ use vector_types::vector::misc::{
|
||||
CentroidType, ExtrudeJoiningAlgorithm, HandleId, InterpolationDistribution, MergeByDistanceAlgorithm, PointSpacingType, RowsOrColumns, bezpath_from_manipulator_groups,
|
||||
bezpath_to_manipulator_groups, handles_to_segment, is_linear, point_to_dvec2, segment_to_handles,
|
||||
};
|
||||
use vector_types::vector::style::{DashPattern, Gradient, PaintOrder, Stroke, StrokeAlign, StrokeCap, StrokeJoin};
|
||||
use vector_types::vector::style::{DashPattern, Gradient, GradientInterpolation, PaintOrder, Stroke, StrokeAlign, StrokeCap, StrokeJoin};
|
||||
use vector_types::vector::{FillId, PointId, RegionId, SegmentDomain, SegmentId, StrokeId, VectorExt};
|
||||
use vector_types::vector::{PointDomain, RegionDomain};
|
||||
|
||||
/// The gradient color for one assign-colors position, replaying the
|
||||
/// randomized draws up to it.
|
||||
fn assign_color_at(gradient: &Gradient, position: usize, length: usize, randomize: bool, seed: SeedValue, repeat_every: u32) -> Color {
|
||||
fn assign_color_at(gradient: &Gradient, gradient_interpolation: vector_types::GradientInterpolation, position: usize, length: usize, randomize: bool, seed: SeedValue, repeat_every: u32) -> Color {
|
||||
let factor = match randomize {
|
||||
true => {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(seed.into());
|
||||
@@ -61,7 +61,7 @@ fn assign_color_at(gradient: &Gradient, position: usize, length: usize, randomiz
|
||||
_ => position as f64 % repeat_every as f64 / (repeat_every - 1) as f64,
|
||||
},
|
||||
};
|
||||
gradient.evaluate(factor, Default::default())
|
||||
gradient.evaluate(factor, Default::default(), gradient_interpolation)
|
||||
}
|
||||
|
||||
/// Uniquely sets the fill and/or stroke style of every vector element to individual colors sampled along a chosen gradient.
|
||||
@@ -104,6 +104,7 @@ fn assign_colors<'e>(
|
||||
if gradient.is_empty() {
|
||||
return Ok((content.lane(lane).map_element(element), Attr(existing_fill), Attr(existing_stroke)));
|
||||
}
|
||||
let gradient_interpolation = gradient.lane(0).attr::<vector_types::markers::GradientInterpolation>();
|
||||
let gradient_element = gradient.element_ref(0);
|
||||
let reversed;
|
||||
let gradient_element = match reverse {
|
||||
@@ -114,7 +115,7 @@ fn assign_colors<'e>(
|
||||
false => gradient_element,
|
||||
};
|
||||
|
||||
let color = assign_color_at(gradient_element, lane, content.len(), randomize, seed, repeat_every);
|
||||
let color = assign_color_at(gradient_element, gradient_interpolation, lane, content.len(), randomize, seed, repeat_every);
|
||||
let paint = List::new_from_element(color).into_graphic_list();
|
||||
let parked = park_paint(ctx.arena(), paint)?;
|
||||
|
||||
@@ -172,6 +173,7 @@ fn assign_colors_graphic<'e>(
|
||||
if gradient.is_empty() {
|
||||
return Ok(content.lane(lane).map_element(original.clone()));
|
||||
}
|
||||
let gradient_interpolation = gradient.lane(0).attr::<vector_types::markers::GradientInterpolation>();
|
||||
let gradient_element = gradient.element_ref(0);
|
||||
let reversed;
|
||||
let gradient_element = match reverse {
|
||||
@@ -217,7 +219,7 @@ fn assign_colors_graphic<'e>(
|
||||
Some(mut rows) => {
|
||||
for row in 0..rows.len() {
|
||||
let has_stroke = rows.element(row).is_some_and(|vector| vector.stroke.is_some());
|
||||
let color = assign_color_at(gradient_element, position + row, length, randomize, seed, repeat_every);
|
||||
let color = assign_color_at(gradient_element, gradient_interpolation, position + row, length, randomize, seed, repeat_every);
|
||||
let paint = List::new_from_element(color).into_graphic_list();
|
||||
if fill {
|
||||
set_paint_attribute_at(&mut rows, row, ATTR_FILL, paint.clone());
|
||||
|
||||
Reference in New Issue
Block a user