Решение на CSS Цветове от Ива Манчевска
Резултати
- 16 точки от тестове
- 0 бонус точки
- 16 точки общо
- 4 успешни тест(а)
- 1 неуспешни тест(а)
Код
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:red,
green:green,
blue:blue,
}
}
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
if hue <= 360 && saturation <= 100 && value <= 100{
Color::HSV {
hue,
saturation,
value,
}
}else{
panic!("error: invalid parameters for new_hsv")
}
}
}
impl Color {
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match *self{
Color::RGB{red,green,blue} => (red,green,blue),
_=>panic!("error: invalid argument passed at unwrap_rgb")
}
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match *self{
Color::HSV{hue,saturation,value} => (hue,saturation,value),
_=>panic!("error: invalid argument passed at unwrap_hsv")
}
}
}
impl Color {
pub fn to_string(&self) -> String {
match *self{
Color::RGB{red,green,blue}=>String::from(format!("#{:02x}{:02x}{:02x}",red,green,blue)),
Color::HSV{hue,saturation,value}=>String::from(format!("{},{}%,{}%",hue,saturation,value)),
}
}
}
impl Color {
pub fn invert(&self) -> Self {
use crate::Color::*;
match *self{
Color::RGB{red,green,blue} => Color::new_rgb(255-red,255-green,255-blue),
Color::HSV{hue,saturation,value}=>Color::new_hsv(360-hue,100-saturation,100-value),
}
}
}
//#[test]
//fn test_basic() {
// let color1 = Color::new_rgb(255, 1, 255);
// //assert_eq!(color1.unwrap_rgb().0, 0);
// let color2 = Color::new_hsv(360, 50, 20);
// //assert_eq!(color2.unwrap_hsv().0, 0);
// assert_eq!(&color1.to_string()[0..7], "#ff01ff");
// //assert_eq!(&color2.to_string()[0..13], "360,100%,100%");
// assert_eq!(color1.invert().unwrap_rgb().0, 0);
// assert_eq!(color1.invert().unwrap_rgb().1, 254);
// assert_eq!(color1.invert().unwrap_rgb().2, 0);
//
// assert_eq!(color2.invert().unwrap_hsv().0, 0);
// assert_eq!(color2.invert().unwrap_hsv().1, 50);
// assert_eq!(color2.invert().unwrap_hsv().2, 80);
//}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20230111-3772066-1k78oqh/solution) warning: unused import: `crate::Color::*` --> src/lib.rs:65:13 | 65 | use crate::Color::*; | ^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: `solution` (lib) generated 1 warning Finished test [unoptimized + debuginfo] target(s) in 0.64s 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 ... FAILED test solution_test::test_invert_rgb ... ok test solution_test::test_rgb_display ... ok test solution_test::test_new_hsv ... ok failures: ---- solution_test::test_hsv_display stdout ---- thread 'solution_test::test_hsv_display' panicked at 'assertion failed: `(left == right)` left: `"0,0%,0%"`, right: `"hsv(0,0%,0%)"`', tests/solution_test.rs:15:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: solution_test::test_hsv_display test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`