diff options
author | 2022-09-29 10:52:58 -0700 | |
---|---|---|
committer | 2022-09-29 11:15:35 -0700 | |
commit | 40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb (patch) | |
tree | 38ffc5dd6bae5da4da3b93664dfe27e024dfa261 /graphics/src/widget/canvas/gradient/linear.rs | |
parent | 97f385e093711c269df315b28f76e66e0220e22a (diff) | |
download | iced-40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb.tar.gz iced-40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb.tar.bz2 iced-40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb.zip |
Adds linear gradient support to 2D meshes in the canvas widget.
Diffstat (limited to 'graphics/src/widget/canvas/gradient/linear.rs')
-rw-r--r-- | graphics/src/widget/canvas/gradient/linear.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/graphics/src/widget/canvas/gradient/linear.rs b/graphics/src/widget/canvas/gradient/linear.rs new file mode 100644 index 00000000..37533e19 --- /dev/null +++ b/graphics/src/widget/canvas/gradient/linear.rs @@ -0,0 +1,73 @@ +//! A linear color gradient. +use iced_native::{Color, Point}; + +use crate::gradient::ColorStop; + +use super::Gradient; + +/// A linear gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. +#[derive(Debug, Clone, PartialEq)] +pub struct Linear { + /// The point where the linear gradient begins. + pub start: Point, + /// The point where the linear gradient ends. + pub end: Point, + /// [`ColorStop`]s along the linear gradient path. + pub color_stops: Vec<ColorStop>, +} + +/// A [`Linear`] builder. +#[derive(Debug)] +pub struct Builder { + start: Point, + end: Point, + stops: Vec<(f32, Color)>, + valid: bool, +} + +impl Builder { + /// Creates a new [`Builder`]. + pub fn new(start: Point, end: Point) -> Self { + Self { + start, + end, + stops: vec![], + valid: true, + } + } + + /// Adds a new stop, defined by an offset and a color, to the gradient. + /// + /// `offset` must be between `0.0` and `1.0`. + pub fn add_stop(mut self, offset: f32, color: Color) -> Self { + if !(0.0..=1.0).contains(&offset) { + self.valid = false; + } + + self.stops.push((offset, color)); + self + } + + /// Builds the linear [`Gradient`] of this [`Builder`]. + /// + /// Returns `None` if no stops were added to the builder or + /// if stops not between 0.0 and 1.0 were added. + pub fn build(self) -> Option<Gradient> { + if self.stops.is_empty() || !self.valid { + return None; + } + + Some(Gradient::Linear(Linear { + start: self.start, + end: self.end, + color_stops: self + .stops + .into_iter() + .map(|f| ColorStop { + offset: f.0, + color: f.1, + }) + .collect(), + })) + } +} |