Решение на 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: red,
green: green,
blue: blue
}
}
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
if hue > 360 || saturation > 100 || value > 100 {
panic!("Invalid input in HSV constructor");
}
Color::HSV {
hue: hue,
saturation: saturation,
value: value
}
}
}
impl Color {
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match self {
Color::RGB { red, green, blue } => (*red, *green, *blue),
_ => panic!("Error - unwrap rgb not allowed for current enum type")
}
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match self {
Color::HSV { hue, saturation, value } => (*hue, *saturation, *value),
_ => panic!("Error - unwrap hsv not allowed for current enum type")
}
}
}
impl Color {
pub fn to_string(&self) -> String {
match self {
Color::RGB { red, green, blue } => {
let str_rgb = format!("#{:02x?}{:02x?}{:02x?}", red, green, blue);
str_rgb
},
Color::HSV { hue, saturation, value } => {
let str_hsv = format!("hsv({},{}%,{}%)", hue, saturation, value);
str_hsv
}
}
}
}
impl Color {
pub fn invert(&self) -> Self {
match self {
Color::RGB { red, green, blue } => {
let add_r = 255 - red;
let add_g = 255 - green;
let add_b = 255 - blue;
Color::new_rgb(add_r, add_g, add_b)
},
Color::HSV { hue, saturation, value } => {
let add_h = 360 - hue;
let add_s = 100 - saturation;
let add_v = 100 - value;
Color::new_hsv(add_h, add_s, add_v)
}
}
}
}

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

Compiling solution v0.1.0 (/tmp/d20230111-3772066-1yh9ac5/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 версия и 0 коментара)

Таня качи първо решение на 23.10.2022 15:59 (преди почти 3 години)