summaryrefslogtreecommitdiffstats
path: root/graphics/src/gradient.rs
diff options
context:
space:
mode:
authorLibravatar shan <shankern@protonmail.com>2022-10-05 10:49:58 -0700
committerLibravatar shan <shankern@protonmail.com>2022-10-05 10:49:58 -0700
commit30432cbade3d9b25c4df62656a7494db3f4ea82a (patch)
tree186ca59e50caede84ede2e2381dc01dd0483806b /graphics/src/gradient.rs
parent6e7b3ced0b1daf368e44e181ecdb4ae529877eb6 (diff)
downloadiced-30432cbade3d9b25c4df62656a7494db3f4ea82a.tar.gz
iced-30432cbade3d9b25c4df62656a7494db3f4ea82a.tar.bz2
iced-30432cbade3d9b25c4df62656a7494db3f4ea82a.zip
Readjusted namespaces, removed Geometry example as it's no longer relevant.
Diffstat (limited to 'graphics/src/gradient.rs')
-rw-r--r--graphics/src/gradient.rs75
1 files changed, 2 insertions, 73 deletions
diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs
index 0c394e8b..33453c67 100644
--- a/graphics/src/gradient.rs
+++ b/graphics/src/gradient.rs
@@ -1,4 +1,6 @@
//! For creating a Gradient.
+mod linear;
+
use iced_native::Color;
pub use crate::gradient::linear::Linear;
use crate::Point;
@@ -28,76 +30,3 @@ impl Gradient {
}
}
-/// Linear gradient builder & definition.
-pub mod linear {
- use crate::gradient::{ColorStop, Gradient};
- use crate::{Color, Point};
-
- /// 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;
- }
-
- let mut stops: Vec<ColorStop> = self.stops.clone().into_iter().map(|f| ColorStop {
- offset: f.0,
- color: f.1
- }).collect();
-
- stops.sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap());
-
- Some(Gradient::Linear(Linear {
- start: self.start,
- end: self.end,
- color_stops: stops
- }))
- }
- }
-}
-