Решение на CSS Цветове от Стилиян Иванов
Резултати
- 12 точки от тестове
- 0 бонус точки
- 12 точки общо
- 3 успешни тест(а)
- 2 неуспешни тест(а)
Код
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 {
Color::HSV{hue: hue, saturation: saturation, value: value}
}
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match &self {
Color::RGB{red, green, blue} => {
let red_ = red.clone();
let green_ = green.clone();
let blue_ = blue.clone();
return (red_, green_, blue_);
},
Color::HSV{hue: _, saturation: _, value: _} => {
panic!("Incorrect format!");
}
};
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match &self {
Color::HSV{hue,saturation,value} => {
let hue_ = hue.clone();
let saturation_ = saturation.clone();
let value_ = value.clone();
return (hue_, saturation_, value_);
},
Color::RGB{red: _, green: _, blue: _} => {
panic!("Incorrect format!");
},
};
}
pub fn to_string(&self) -> String {
let mut result: String = "".to_owned();
match &self {
Color::HSV{hue,saturation,value} => {
result.push_str(&hue.to_string());
result.push_str(",");
result.push_str(&saturation.to_string());
result.push_str("%,");
result.push_str(&value.to_string());
result.push_str("%");
},
Color::RGB{red, green, blue} => {
result.push_str("#");
result.push_str(&(format!("{:02x}", &red).to_string()));
result.push_str(&(format!("{:02x}", &green).to_string()));
result.push_str(&(format!("{:02x}", &blue).to_string()));
},
};
result
}
pub fn invert(&self) -> Self {
match &self {
Color::HSV{hue,saturation,value} => {
return Self::new_hsv(360 - hue, 100 - saturation, 100 - value);
},
Color::RGB{red, green, blue} => {
return Self::new_rgb(255 - red, 255 - green, 255 - blue);
},
};
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20230111-3772066-1olbw0y/solution) Finished test [unoptimized + debuginfo] target(s) in 0.70s 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_invert_rgb ... ok test solution_test::test_hsv_display ... FAILED test solution_test::test_new_hsv ... FAILED test solution_test::test_rgb_display ... 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 ---- solution_test::test_new_hsv stdout ---- thread 'solution_test::test_new_hsv' panicked at 'assertion failed: catch_unwind(|| Color::new_hsv(361, 0, 0)).is_err()', tests/solution_test.rs:22:5 failures: solution_test::test_hsv_display solution_test::test_new_hsv test result: FAILED. 3 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`