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

JSON

Values are represented as an object {r: float, f: float, b: float, a: float}, where each color is between 0 and 1. Field a may be omitted, in which case it is taken to be 0.

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)
}

Trait Implementations

impl Deserialize for Color

fn deserialize<__D>(deserializer: &mut __D) -> Result<Color, __D::Error> where __D: Deserializer

impl Serialize for Color

fn serialize<__S>(&self, serializer: &mut __S) -> Result<(), __S::Error> where __S: Serializer

impl Parser<Color> for Color

fn description() -> String

fn parse(path: Path, source: &mut JSON) -> Result<Self, ParseError>

fn from_str(source: &str) -> Result<T, ParseError>

fn take(path: Path, source: &mut JSON, field_name: &str) -> Result<T, ParseError>

fn take_opt(path: Path, source: &mut JSON, field_name: &str) -> Option<Result<T, ParseError>>

fn take_vec_opt(path: Path, source: &mut JSON, field_name: &str) -> Option<Result<Vec<T>, ParseError>>

fn take_vec(path: Path, source: &mut JSON, field_name: &str) -> Result<Vec<T>, ParseError>

impl ToJSON for Color

fn to_json(&self, parts: &mut BinaryParts) -> JSON

Derived Implementations

impl PartialOrd for Color

fn partial_cmp(&self, __arg_0: &Color) -> Option<Ordering>

fn lt(&self, __arg_0: &Color) -> bool

fn le(&self, __arg_0: &Color) -> bool

fn gt(&self, __arg_0: &Color) -> bool

fn ge(&self, __arg_0: &Color) -> bool

impl PartialEq for Color

fn eq(&self, __arg_0: &Color) -> bool

fn ne(&self, __arg_0: &Color) -> bool

impl Clone for Color

fn clone(&self) -> Color

1.0.0fn clone_from(&mut self, source: &Self)

impl Debug for Color

fn fmt(&self, __arg_0: &mut Formatter) -> Result