Решение на CSS Цветове от Теодор Борисов

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

Към профила на Теодор Борисов

Резултати

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

Код

/// MAX стойност за RGB
pub const MAX_RGB: u8 = u8::MAX;
/// MAX стойност за value и saturation
pub const MAX_VS: u8 = 100u8;
/// MAX стойност за hue
pub const MAX_HUE: u16 = 360u16;
#[derive(Debug)]
pub enum Color {
RGB {
red: u8,
green: u8,
blue: u8
},
HSV {
hue: u16,
saturation: u8,
value: u8,
}
}
impl Color {
/// Създаване на RGB
pub fn new_rgb(red: u8, green: u8, blue: u8) -> Color {
Color::RGB {
red: red,
green: green,
blue: blue
}
}
/// Създаване на HSV
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
if hue > MAX_HUE || saturation > MAX_VS || value > MAX_VS {
panic!("Грешни стойности за HSV!");
} else {
Color::HSV {
hue: hue,
saturation: saturation,
value: value
}
}
}
/// Вземане на стойности от RGB
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match *self {
Self::RGB { red, green, blue } => (red, green, blue),
_ => panic!("Това не е RGB!")
}
}
/// Вземане на стойности от HSV
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match *self {
Self::HSV { hue, saturation, value } => (hue, saturation, value),
_ => panic!("Това не е HSV!")
}
}
/// Инвертира цвят покомпонентно -- за всяка от стойностите се взема разликата с максимума.
pub fn invert(&self) -> Self {
match *self {
Self::RGB { red, green, blue } => Self::RGB { red: MAX_RGB - red,
green: MAX_RGB - green,
blue: MAX_RGB - blue },
Self::HSV { hue, saturation, value } => Self::HSV { hue: MAX_HUE - hue,
saturation: MAX_VS - saturation,
value: MAX_VS - value }
}
}
/// Създава string, съдържащ стойностите
pub fn to_string(&self) -> String {
match *self {
Self::RGB { red, green, blue } => String::from(format!("#{:02x}{:02x}{:02x}", red, green, blue)),
Self::HSV { hue, saturation, value } => String::from(format!("hsv({:0},{:0}%,{:0}%)", hue, saturation, value)),
}
}
}
#[test]
fn test_basic() {
let color1 = Color::new_rgb(0, 0, 0);
assert_eq!(color1.unwrap_rgb().0, 0);
assert_eq!(&color1.to_string()[0..1], "#");
let color2 = Color::new_hsv(0, 0, 0);
assert_eq!(color2.unwrap_hsv().0, 0);
assert_eq!(color1.invert().unwrap_rgb().0, 255);
}
#[test]
fn test_functionality_rgb() {
let color = Color::new_rgb(128, 100, 255);
assert_eq!(color.unwrap_rgb().0, 128);
assert_eq!(color.unwrap_rgb().1, 100);
assert_eq!(color.unwrap_rgb().2, 255);
assert_eq!(&color.to_string(), "#8064ff");
assert_eq!(color.invert().unwrap_rgb().0, 127);
assert_eq!(color.invert().unwrap_rgb().1, 155);
assert_eq!(color.invert().unwrap_rgb().2, 0);
assert_eq!(color.invert().to_string(), "#7f9b00");
}
#[test]
fn test_functionality_hsv() {
let color = Color::new_hsv(220, 100, 50);
assert_eq!(color.unwrap_hsv().0, 220);
assert_eq!(color.unwrap_hsv().1, 100);
assert_eq!(color.unwrap_hsv().2, 50);
assert_eq!(&color.to_string(), "hsv(220,100%,50%)");
assert_eq!(color.invert().unwrap_hsv().0, 140);
assert_eq!(color.invert().unwrap_hsv().1, 0);
assert_eq!(color.invert().unwrap_hsv().2, 50);
assert_eq!(color.invert().to_string(), "hsv(140,0%,50%)");
}

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

Compiling solution v0.1.0 (/tmp/d20230111-3772066-1qum55k/solution)
    Finished test [unoptimized + debuginfo] target(s) in 0.60s
     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 ... 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 коментар)

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

Имаш добри тестове за "happy path" сценариите в които успешно се конструират и unwrap-ват неща, но ти липсват тестове за невалиден вход и невалиден unwrap.

Иначе кода си е смислено написан, good job 👍