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,
red: color.red as c_uchar,
green: color.green as c_uchar,
blue: color.blue 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,
red: self.red,
green: self.green,
blue: self.blue,
})
}
}