mirror of
https://github.com/penpot/penpot.git
synced 2025-04-15 16:31:25 -05:00
♻️ Refactor adding gradient stops
This commit is contained in:
parent
5e9f533624
commit
7105e49ac2
3 changed files with 32 additions and 26 deletions
|
@ -1,6 +1,6 @@
|
|||
mod debug;
|
||||
mod math;
|
||||
pub mod mem;
|
||||
mod mem;
|
||||
mod render;
|
||||
mod shapes;
|
||||
mod state;
|
||||
|
@ -177,33 +177,20 @@ pub extern "C" fn add_shape_linear_fill(
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RawStopData {
|
||||
color: [u8; 4],
|
||||
offset: u8,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_shape_fill_stops(ptr: *mut RawStopData, n_stops: i32) {
|
||||
pub extern "C" fn add_shape_fill_stops(ptr: *mut shapes::RawStopData, n_stops: u32) {
|
||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||
|
||||
if let Some(shape) = state.current_shape() {
|
||||
let len = n_stops as usize;
|
||||
let buffer_size = std::mem::size_of::<shapes::RawStopData>() * len;
|
||||
|
||||
unsafe {
|
||||
let buf = Vec::<RawStopData>::from_raw_parts(ptr, n_stops as usize, n_stops as usize);
|
||||
for raw_stop in buf.iter() {
|
||||
let color = skia::Color::from_argb(
|
||||
raw_stop.color[3],
|
||||
raw_stop.color[0],
|
||||
raw_stop.color[1],
|
||||
raw_stop.color[2],
|
||||
);
|
||||
shape
|
||||
.add_gradient_stop(color, (raw_stop.offset as f32) / 100.)
|
||||
.expect("got no fill or an invalid one");
|
||||
}
|
||||
mem::free(
|
||||
ptr as *mut u8,
|
||||
n_stops as usize * std::mem::size_of::<RawStopData>(),
|
||||
);
|
||||
let buffer = Vec::<shapes::RawStopData>::from_raw_parts(ptr, len, len);
|
||||
shape
|
||||
.add_gradient_stops(buffer)
|
||||
.expect("could not add gradient stops");
|
||||
mem::free(ptr as *mut u8, buffer_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,14 +92,16 @@ impl Shape {
|
|||
self.fills.clear();
|
||||
}
|
||||
|
||||
pub fn add_gradient_stop(&mut self, color: skia::Color, offset: f32) -> Result<(), String> {
|
||||
pub fn add_gradient_stops(&mut self, buffer: Vec<RawStopData>) -> Result<(), String> {
|
||||
let fill = self.fills.last_mut().ok_or("Shape has no fills")?;
|
||||
let gradient = match fill {
|
||||
Fill::LinearGradient(g) => Ok(g),
|
||||
_ => Err("Active fill is not a gradient"),
|
||||
}?;
|
||||
|
||||
gradient.add_stop(color, offset);
|
||||
for stop in buffer.into_iter() {
|
||||
gradient.add_stop(stop.color(), stop.offset());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -4,6 +4,23 @@ use super::Color;
|
|||
use crate::math;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[repr(C)]
|
||||
pub struct RawStopData {
|
||||
color: [u8; 4],
|
||||
offset: u8,
|
||||
}
|
||||
|
||||
impl RawStopData {
|
||||
pub fn color(&self) -> skia::Color {
|
||||
skia::Color::from_argb(self.color[3], self.color[0], self.color[1], self.color[2])
|
||||
}
|
||||
|
||||
pub fn offset(&self) -> f32 {
|
||||
self.offset as f32 / 100.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Gradient {
|
||||
colors: Vec<Color>,
|
||||
|
|
Loading…
Add table
Reference in a new issue