Решение на 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 {
let hue_max = 360;
let saturation_max = 100;
let value_max = 100;
if hue > hue_max || saturation > saturation_max || value > value_max {
panic!("Values exceeding limits!");
}
Color::HSV{ hue: hue, saturation: saturation, value: value }
}
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match self {
Color::RGB{red : x, green : y, blue : z} => return (*x, *y, *z),
_ => panic!("Wrong type of color!"),
}
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match self {
Color::HSV{hue : x, saturation : y, value : z} => return (*x, *y, *z),
_ => panic!("Wrong type of color!"),
}
}
pub fn to_string(&self) -> String {
match self {
Color::RGB{red : x, green : y, blue : z} => return format!("#{:02X}{:02X}{:02X}", x, y, z),
Color::HSV{hue : x, saturation : y, value : z} => return format!("hsv({},{}%,{}%)", x, y, z),
}
}
pub fn invert(&self) -> Self {
let hue_max = 360;
let saturation_max = 100;
let value_max = 100;
let rgb_max = 255;
match self {
Color::RGB{red : x, green : y, blue : z} => return Color::RGB{red : rgb_max - x, green : rgb_max - y, blue : rgb_max - z},
Color::HSV{hue : x, saturation : y, value : z} => return Color::HSV{hue : hue_max - x, saturation : saturation_max - y, value : value_max - z},
}
}
}

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

Compiling solution v0.1.0 (/tmp/d20230111-3772066-1dktqg5/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_hsv_display ... ok
test solution_test::test_invert_hsv ... ok
test solution_test::test_invert_rgb ... ok
test solution_test::test_rgb_display ... FAILED
test solution_test::test_new_hsv ... ok

failures:

---- solution_test::test_rgb_display stdout ----
thread 'solution_test::test_rgb_display' panicked at 'assertion failed: `(left == right)`
  left: `"#01147B"`,
 right: `"#01147b"`', tests/solution_test.rs:9:5


failures:
    solution_test::test_rgb_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`

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

Николай качи първо решение на 26.10.2022 14:42 (преди почти 3 години)