virtual_console_lib/
colors.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::{anyhow, Error};
6use carnelian::color::Color;
7use std::str::FromStr;
8
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub struct ColorScheme {
11    pub front: Color,
12    pub back: Color,
13}
14
15const WHITE_COLOR: Color = Color { r: 255, g: 255, b: 255, a: 255 };
16const BLACK_COLOR: Color = Color { r: 0, g: 0, b: 0, a: 255 };
17const BLUE_COLOR: Color = Color { r: 0, g: 0, b: 170, a: 255 };
18
19pub const DARK_COLOR_SCHEME: ColorScheme = ColorScheme { front: WHITE_COLOR, back: BLACK_COLOR };
20pub const LIGHT_COLOR_SCHEME: ColorScheme = ColorScheme { front: BLACK_COLOR, back: WHITE_COLOR };
21pub const SPECIAL_COLOR_SCHEME: ColorScheme = ColorScheme { front: WHITE_COLOR, back: BLUE_COLOR };
22
23impl Default for ColorScheme {
24    fn default() -> Self {
25        DARK_COLOR_SCHEME
26    }
27}
28
29impl FromStr for ColorScheme {
30    type Err = Error;
31
32    fn from_str(s: &str) -> Result<Self, Self::Err> {
33        match s {
34            "dark" => Ok(DARK_COLOR_SCHEME),
35            "light" => Ok(LIGHT_COLOR_SCHEME),
36            "special" => Ok(SPECIAL_COLOR_SCHEME),
37            "default" => Ok(ColorScheme::default()),
38            _ => Err(anyhow!("Invalid ColorScheme {}", s)),
39        }
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn check_color_scheme_from_str() -> Result<(), Error> {
49        let dark = ColorScheme::from_str("dark")?;
50        assert_eq!(dark, DARK_COLOR_SCHEME);
51        let light = ColorScheme::from_str("light")?;
52        assert_eq!(light, LIGHT_COLOR_SCHEME);
53        let special = ColorScheme::from_str("special")?;
54        assert_eq!(special, SPECIAL_COLOR_SCHEME);
55        let default = ColorScheme::from_str("default")?;
56        assert_eq!(default, ColorScheme::default());
57        let invalid = ColorScheme::from_str("invalid");
58        assert_eq!(invalid.is_ok(), false);
59
60        Ok(())
61    }
62}