Решение на CSS Цветове от Тодор Кюркчиев

Обратно към всички решения

Към профила на Тодор Кюркчиев

Резултати

  • 20 точки от тестове
  • 0 бонус точки
  • 20 точки общо
  • 5 успешни тест(а)
  • 0 неуспешни тест(а)

Код

pub enum Color {
RGB {
red: u8,
green: u8,
blue: u8
},
HSV {
hue: u16,
saturation: u8,
value: u8,
}
}
impl Color {
pub fn new_rgb(red: u8, green: u8, blue: u8) -> Color {
Color::RGB {red, green, blue}
}
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
assert!(hue <= 360, "hue must be <= 360");
assert!(saturation <= 100, "saturation must be <= 100");
assert!(value <= 100, "value must be <= 100");
Color::HSV {hue, saturation, value}
}
}
impl Color {
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match self {
Color::RGB {red, green, blue} => (*red, *green, *blue),
_ => panic!("Color is not RGB"),
}
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match self {
Color::HSV {hue, saturation, value} => (*hue, *saturation, *value),
_ => panic!("Color is not HSV"),
}
}
pub fn to_string(&self) -> String {
match self {
Color::RGB {red, green, blue} => format!("#{:02x}{:02x}{:02x}", red, green, blue),
Color::HSV {hue, saturation, value} => format!("hsv({},{}%,{}%)", hue, saturation, value),
}
}
}
impl Color {
pub fn invert(&self) -> Self {
match self {
Color::RGB {red, green, blue} => Color::RGB {
red: 255 - red,
green: 255 - green,
blue: 255 - blue,
},
Color::HSV {hue, saturation, value} => Color::HSV {
hue: 360 - hue,
saturation: 100 - saturation,
value: 100 - value,
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
assert_eq!(Color::new_rgb(0, 0, 0).to_string(), "#000000");
assert_eq!(Color::new_rgb(255, 1, 255).to_string(), "#ff01ff");
assert_eq!(Color::new_hsv(0, 0, 0).to_string(), "hsv(0,0%,0%)");
assert_eq!(Color::new_hsv(360, 100, 100).to_string(), "hsv(360,100%,100%)");
assert_eq!(Color::new_hsv(0,50,0).invert().to_string(), "hsv(360,50%,100%)");
}
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20230111-3772066-lrl1cy/solution)
    Finished test [unoptimized + debuginfo] target(s) in 0.59s
     Running tests/solution_test.rs (target/debug/deps/solution_test-0edbea2040daef01)

running 5 tests
test solution_test::test_invert_hsv ... ok
test solution_test::test_hsv_display ... ok
test solution_test::test_invert_rgb ... ok
test solution_test::test_rgb_display ... ok
test solution_test::test_new_hsv ... ok

test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

История (1 версия и 1 коментар)