🎉 Add a DTO that handles all fill types

This commit is contained in:
Belén Albeza 2025-05-05 16:55:00 +02:00
parent 173d6c23b0
commit 784aecd1a1
3 changed files with 118 additions and 4 deletions

View file

@ -5,9 +5,9 @@ const BASE_GRADIENT_DATA_SIZE: usize = 28;
const RAW_GRADIENT_DATA_SIZE: usize =
BASE_GRADIENT_DATA_SIZE + RAW_STOP_DATA_SIZE * MAX_GRADIENT_STOPS;
#[derive(Debug)]
#[derive(Debug, PartialEq, Clone, Copy)]
#[repr(C)]
struct RawGradientData {
pub struct RawGradientData {
start_x: f32,
start_y: f32,
end_x: f32,
@ -43,6 +43,17 @@ impl From<[u8; RAW_GRADIENT_DATA_SIZE]> for RawGradientData {
}
}
impl TryFrom<&[u8]> for RawGradientData {
type Error = String;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let data: [u8; RAW_GRADIENT_DATA_SIZE] = bytes
.try_into()
.map_err(|_| "Invalid gradient data".to_string())?;
Ok(RawGradientData::from(data))
}
}
impl RawGradientData {
pub fn start(&self) -> (f32, f32) {
(self.start_x, self.start_y)
@ -55,7 +66,7 @@ impl RawGradientData {
pub const RAW_STOP_DATA_SIZE: usize = 8;
#[derive(Debug)]
#[derive(Debug, PartialEq, Clone, Copy)]
#[repr(C)]
struct RawStopData {
color: u32,

View file

@ -1,8 +1,9 @@
use crate::shapes::{Color, SolidColor};
#[repr(C)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct RawSolidData {
color: u32,
pub color: u32,
}
impl From<[u8; 4]> for RawSolidData {
@ -13,6 +14,18 @@ impl From<[u8; 4]> for RawSolidData {
}
}
impl TryFrom<&[u8]> for RawSolidData {
type Error = String;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let data: [u8; 4] = bytes
.get(0..4)
.and_then(|slice| slice.try_into().ok())
.ok_or("Invalid solid fill data".to_string())?;
Ok(RawSolidData::from(data))
}
}
impl From<RawSolidData> for SolidColor {
fn from(value: RawSolidData) -> Self {
Self(Color::new(value.color))