pub fn deserialize_with_optional_alpha<'de, T, A, D>(
    deserializer: D
) -> Result<Alpha<T, A>, D::Error>
where T: Deserialize<'de>, A: Stimulus + Deserialize<'de>, D: Deserializer<'de>,
Expand description

Deserialize a transparent color without requiring the alpha to be specified.

A color with missing alpha will be interpreted as fully opaque.

use serde::Deserialize;
use palette::Srgba;

#[derive(Deserialize, PartialEq, Debug)]
struct MyColors {
    #[serde(deserialize_with = "palette::serde::deserialize_with_optional_alpha")]
    opaque: Srgba,
    #[serde(deserialize_with = "palette::serde::deserialize_with_optional_alpha")]
    transparent: Srgba,
}

let my_colors = MyColors {
    opaque: Srgba::new(0.6, 0.8, 0.3, 1.0),
    transparent: Srgba::new(0.6, 0.8, 0.3, 0.5),
};

let json = r#"{
    "opaque":{"red":0.6,"green":0.8,"blue":0.3},
    "transparent":{"red":0.6,"green":0.8,"blue":0.3,"alpha":0.5}
}"#;
assert_eq!(
    serde_json::from_str::<MyColors>(json).unwrap(),
    my_colors
);