Make Packed fully opaque
... by only allowing direct conversion from our `Gradient` types
This commit is contained in:
parent
902e333148
commit
8ca7b884c0
2 changed files with 28 additions and 37 deletions
|
|
@ -4,7 +4,7 @@
|
||||||
//!
|
//!
|
||||||
//! [`Gradient`]: crate::core::Gradient;
|
//! [`Gradient`]: crate::core::Gradient;
|
||||||
use crate::core::gradient::ColorStop;
|
use crate::core::gradient::ColorStop;
|
||||||
use crate::core::{Color, Point};
|
use crate::core::{self, Color, Point, Rectangle};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
|
@ -125,8 +125,31 @@ impl Linear {
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct Packed([f32; 44]);
|
pub struct Packed([f32; 44]);
|
||||||
|
|
||||||
impl From<[f32; 44]> for Packed {
|
/// Creates a new [`Packed`] gradient for use in shader code.
|
||||||
fn from(value: [f32; 44]) -> Self {
|
pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed {
|
||||||
Self(value)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -183,7 +183,7 @@ impl<'a> Layer<'a> {
|
||||||
}
|
}
|
||||||
Background::Gradient(gradient) => {
|
Background::Gradient(gradient) => {
|
||||||
let quad = quad::Gradient {
|
let quad = quad::Gradient {
|
||||||
gradient: pack_gradient(
|
gradient: gradient::pack(
|
||||||
gradient,
|
gradient,
|
||||||
Rectangle::new(
|
Rectangle::new(
|
||||||
quad.position.into(),
|
quad.position.into(),
|
||||||
|
|
@ -311,35 +311,3 @@ impl<'a> Layer<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Packs the [`Gradient`] for use in shader code.
|
|
||||||
fn pack_gradient(
|
|
||||||
gradient: &core::Gradient,
|
|
||||||
bounds: Rectangle,
|
|
||||||
) -> gradient::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;
|
|
||||||
|
|
||||||
data.into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue