use cranpose_foundation::{NodeCapabilities, PlainElement};
use crate::modifier::{Color, RoundedCornerShape};
#[derive(Debug, Clone, PartialEq)]
pub struct BackgroundElement {
color: Color,
}
impl BackgroundElement {
pub fn new(color: Color) -> Self {
Self { color }
}
pub fn color(&self) -> Color {
self.color
}
}
impl PlainElement for BackgroundElement {
const WORDS: usize = 4;
const CAPABILITIES: NodeCapabilities = NodeCapabilities::DRAW;
fn to_words(&self, words: &mut [u32]) {
let Color(red, green, blue, alpha) = self.color;
words.copy_from_slice(&[red, green, blue, alpha].map(f32::to_bits));
}
#[inline]
fn from_words(words: &[u32]) -> Self {
let channels = match *words {
[red, green, blue, alpha, ..] => [red, green, blue, alpha],
_ => [0; 4],
};
let [red, green, blue, alpha] = channels.map(f32::from_bits);
Self {
color: Color(red, green, blue, alpha),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CornerShapeElement {
shape: RoundedCornerShape,
}
impl CornerShapeElement {
pub fn new(shape: RoundedCornerShape) -> Self {
Self { shape }
}
pub fn shape(&self) -> RoundedCornerShape {
self.shape
}
}
impl PlainElement for CornerShapeElement {
const WORDS: usize = 4;
const CAPABILITIES: NodeCapabilities = NodeCapabilities::DRAW;
fn to_words(&self, words: &mut [u32]) {
let radii = self.shape.radii();
words.copy_from_slice(
&[
radii.top_left,
radii.top_right,
radii.bottom_right,
radii.bottom_left,
]
.map(f32::to_bits),
);
}
#[inline]
fn from_words(words: &[u32]) -> Self {
let radii = match *words {
[top_left, top_right, bottom_right, bottom_left, ..] => {
[top_left, top_right, bottom_right, bottom_left]
}
_ => [0; 4],
};
let [top_left, top_right, bottom_right, bottom_left] = radii.map(f32::from_bits);
Self {
shape: RoundedCornerShape::new(top_left, top_right, bottom_right, bottom_left),
}
}
}