Enum foxbox_taxonomy::values::Color
[−]
[src]
pub enum Color { RGBA(f64, f64, f64, f64), }
A color. Internal representation may vary. The FoxBox adapters are expected to perform conversions to the format requested by their device.
Variants
RGBA | JSONValues are represented as an object {r: float, f: float, b: float, a: float},
where each color is between 0 and 1. Field use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; println!("Testing parsing"); let source = "{ \"r\": 0.1, \"g\": 0.2, \"b\": 0.4, \"a\": 0.8 }"; let parsed = Color::from_str(source).unwrap(); let Color::RGBA(r, g, b, a) = parsed; assert_eq!(r, 0.1); assert_eq!(g, 0.2); assert_eq!(b, 0.4); assert_eq!(a, 0.8); println!("Testing serialization"); let serialized : JSON = parsed.to_json(&mut MultiPart::new()); let r = serialized.find("r").unwrap().as_f64().unwrap(); assert_eq!(r, 0.1); let g = serialized.find("g").unwrap().as_f64().unwrap(); assert_eq!(g, 0.2); let b = serialized.find("b").unwrap().as_f64().unwrap(); assert_eq!(b, 0.4); let a = serialized.find("a").unwrap().as_f64().unwrap(); assert_eq!(a, 0.8); println!("Testing parsing error (value not in [0, 1])"); // This source will not parse. let source_2 = "{ \"r\": 100, \"g\": 0.2, \"b\": 0.4, \"a\": 0.9 }"; match Color::from_str(source_2) { Err(ParseError::TypeError{..}) => {}, other => panic!("Unexpected result {:?}", other) } println!("Testing auto-added alpha"); // This source does not specify alpha, so alpha is 0. let source_3 = "{ \"r\": 0.1, \"g\": 0.2, \"b\": 0.4 }"; let parsed = Color::from_str(source_3).unwrap(); let Color::RGBA(r, g, b, a) = parsed; assert_eq!(r, 0.1); assert_eq!(g, 0.2); assert_eq!(b, 0.4); assert_eq!(a, 0.); println!("Testing parsing error (missing field)"); // This source does not specify b, so it will not parse. let source_4 = "{ \"r\": 0.1, \"g\": 0.2 }"; match Color::from_str(source_4) { Err(ParseError::MissingField{ref name, ..}) if &name as &str == "b" => {}, other => panic!("Unexpected result {:?}", other) } |