summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2023-05-29 21:09:17 +0200
committerLibravatar GitHub <noreply@github.com>2023-05-29 21:09:17 +0200
commit75110b9c0e82f37f3c75ce276216db5dbcb4ff1b (patch)
treed559afb3868aa12132e5047d18cb95c1fb135595 /graphics
parentfcb1b454368638209862aeb5db41bc5f7d6d51a7 (diff)
parent8ca7b884c0695e4e7a031ea1359ee733bdcaa8a4 (diff)
downloadiced-75110b9c0e82f37f3c75ce276216db5dbcb4ff1b.tar.gz
iced-75110b9c0e82f37f3c75ce276216db5dbcb4ff1b.tar.bz2
iced-75110b9c0e82f37f3c75ce276216db5dbcb4ff1b.zip
Merge pull request #1871 from bungoboingo/fix/bg-gradient
[Fix] Make gradient pack fn public for iced_graphics::Gradient
Diffstat (limited to 'graphics')
-rw-r--r--graphics/src/gradient.rs69
-rw-r--r--graphics/src/primitive.rs3
2 files changed, 70 insertions, 2 deletions
diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs
index 3e88d9de..d3eabb6f 100644
--- a/graphics/src/gradient.rs
+++ b/graphics/src/gradient.rs
@@ -4,7 +4,7 @@
//!
//! [`Gradient`]: crate::core::Gradient;
use crate::core::gradient::ColorStop;
-use crate::core::{Color, Point};
+use crate::core::{self, Color, Point, Rectangle};
use std::cmp::Ordering;
#[derive(Debug, Clone, PartialEq)]
@@ -23,6 +23,15 @@ impl From<Linear> for Gradient {
}
}
+impl Gradient {
+ /// Packs the [`Gradient`] for use in shader code.
+ pub fn pack(&self) -> Packed {
+ 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,62 @@ impl Linear {
self
}
+
+ /// Packs the [`Gradient`] for use in shader code.
+ pub fn pack(&self) -> Packed {
+ let mut data: [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();
+
+ data[index * 4] = r;
+ data[(index * 4) + 1] = g;
+ data[(index * 4) + 2] = b;
+ data[(index * 4) + 3] = a;
+
+ data[32 + index] = stop.map_or(2.0, |s| s.offset);
+ }
+
+ data[40] = self.start.x;
+ data[41] = self.start.y;
+ data[42] = self.end.x;
+ data[43] = self.end.y;
+
+ Packed(data)
+ }
+}
+
+/// Packed [`Gradient`] data for use in shader code.
+#[derive(Debug, Copy, Clone, PartialEq)]
+#[repr(C)]
+pub struct Packed([f32; 44]);
+
+/// Creates a new [`Packed`] gradient for use in shader code.
+pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed {
+ match gradient {
+ core::Gradient::Linear(linear) => {
+ let mut data: [f32; 44] = [0.0; 44];
+
+ for (index, stop) in linear.stops.iter().enumerate() {
+ let [r, g, b, a] =
+ stop.map_or(Color::default(), |s| s.color).into_linear();
+
+ data[index * 4] = r;
+ data[(index * 4) + 1] = g;
+ data[(index * 4) + 2] = b;
+ data[(index * 4) + 3] = a;
+ data[32 + index] = stop.map_or(2.0, |s| s.offset);
+ }
+
+ let (start, end) = linear.angle.to_distance(&bounds);
+
+ data[40] = start.x;
+ data[41] = start.y;
+ data[42] = end.x;
+ data[43] = end.y;
+
+ Packed(data)
+ }
+ }
}
diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs
index 9728db39..2d4f5511 100644
--- a/graphics/src/primitive.rs
+++ b/graphics/src/primitive.rs
@@ -4,6 +4,7 @@ use crate::core::image;
use crate::core::svg;
use crate::core::text;
use crate::core::{Background, Color, Font, Rectangle, Size, Vector};
+use crate::gradient;
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
@@ -258,7 +259,7 @@ pub struct GradientVertex2D {
pub position: [f32; 2],
/// The packed vertex data of the gradient.
- pub gradient: [f32; 44],
+ pub gradient: gradient::Packed,
}
#[allow(unsafe_code)]