use super::*;
impl TryFrom<Color> for SPFColor {
type Error = ConversionError;
fn try_from(color: Color) -> Result<Self, Self::Error> {
Ok(SPFColor {
has_color_type: color.color_type.is_some() as c_uchar,
color_type: color.color_type.unwrap_or(ColorType::Dynamic) as c_uchar,
has_custom_alpha: color.custom_alpha.is_some() as c_uchar,
custom_alpha: color.custom_alpha.unwrap_or(0) as c_uchar,
r: color.r as c_uchar,
g: color.g as c_uchar,
b: color.b as c_uchar,
})
}
}
impl TryInto<Color> for &SPFColor {
type Error = ConversionError;
fn try_into(self) -> Result<Color, Self::Error> {
let color_type = if self.has_color_type != 0 {
Some(
ColorType::try_from(self.color_type)
.map_err(|_| ConversionError::UnsupportedColorType)?,
)
} else {
None
};
let custom_alpha = ffi_to_option!(self.has_custom_alpha, self.custom_alpha);
Ok(Color {
color_type,
custom_alpha,
r: self.r,
g: self.g,
b: self.b,
})
}
}