summaryrefslogtreecommitdiffstats
path: root/graphics/src/gradient.rs
diff options
context:
space:
mode:
authorLibravatar Bingus <shankern@protonmail.com>2023-05-24 13:08:59 -0700
committerLibravatar Bingus <shankern@protonmail.com>2023-05-24 13:08:59 -0700
commita395e78596d0711a50108cb6654121541337ceb5 (patch)
tree524ce93835f061c52e58fe2644174ea94c44293a /graphics/src/gradient.rs
parent6c6930b91df0d7637ed6c0f3f4e57a03c5e5896f (diff)
downloadiced-a395e78596d0711a50108cb6654121541337ceb5.tar.gz
iced-a395e78596d0711a50108cb6654121541337ceb5.tar.bz2
iced-a395e78596d0711a50108cb6654121541337ceb5.zip
Made gradient pack public for iced_graphics::gradient mod for use with GradientVertex2D.
Diffstat (limited to 'graphics/src/gradient.rs')
-rw-r--r--graphics/src/gradient.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs
index 3e88d9de..86d14b6c 100644
--- a/graphics/src/gradient.rs
+++ b/graphics/src/gradient.rs
@@ -23,6 +23,15 @@ impl From<Linear> for Gradient {
}
}
+impl Gradient {
+ /// Packs the [`Gradient`] for use in shader code.
+ pub fn pack(&self) -> [f32; 44] {
+ match self {
+ Gradient::Linear(linear) => linear.pack(),
+ }
+ }
+}
+
/// A linear gradient that can be used in the style of [`Fill`] or [`Stroke`].
///
/// [`Fill`]: crate::geometry::Fill;
@@ -85,4 +94,28 @@ impl Linear {
self
}
+
+ /// Packs the [`Gradient`] for use in shader code.
+ pub fn pack(&self) -> [f32; 44] {
+ let mut pack: [f32; 44] = [0.0; 44];
+
+ for (index, stop) in self.stops.iter().enumerate() {
+ let [r, g, b, a] =
+ stop.map_or(Color::default(), |s| s.color).into_linear();
+
+ pack[index * 4] = r;
+ pack[(index * 4) + 1] = g;
+ pack[(index * 4) + 2] = b;
+ pack[(index * 4) + 3] = a;
+
+ pack[32 + index] = stop.map_or(2.0, |s| s.offset);
+ }
+
+ pack[40] = self.start.x;
+ pack[41] = self.start.y;
+ pack[42] = self.end.x;
+ pack[43] = self.end.y;
+
+ pack
+ }
}