Решение на CSS Цветове от Петко Каменов

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

Към профила на Петко Каменов

Резултати

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

Код

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, green, blue }
}
/// Конструира нова стойност от вариант `HSV` с дадените стойности.
///
/// В случай, че hue е над 360 или saturation или value са над 100, очакваме да `panic!`-нете с
/// каквото съобщение си изберете.
///
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
assert!(hue <= 360, "hue must be <= 360");
assert!(saturation <= 100, "saturation must be <= 100");
assert!(value <= 100, "value must be <= 100");
Color::HSV {
hue,
saturation,
value,
}
}
/// Ако `self` е `RGB`, тогава връщате неговите `red`, `green`, `blue` компоненти в този ред.
/// Иначе, `panic!`-вате с каквото съобщение си изберете.
///
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match self {
Color::RGB { red, green, blue } => (*red, *green, *blue),
_ => panic!("Color is not RGB"),
}
}
/// Ако `self` е `HSV`, тогава връщате неговите `hue`, `saturation`, `value` компоненти в този
/// ред. Иначе, `panic!`-вате с каквото съобщение си изберете.
///
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match self {
Color::HSV {
hue,
saturation,
value,
} => (*hue, *saturation, *value),
_ => panic!("Color is not HSV"),
}
}
/// В случай, че варианта на `self` е `RGB`, очакваме низ със съдържание `#rrggbb`, където
/// червения, зеления и синия компонент са форматирани в шестнадесетична система, и всеки от тях е
/// точно два символа с малки букви (запълнени с нули).
///
/// Ако варианта е `HSV`, очакваме низ `hsv(h,s%,v%)`, където числата са си напечатани в
/// десетичната система, без водещи нули, без интервали след запетаите, вторите две завършващи на
/// `%`.
///
pub fn to_string(&self) -> String {
match self {
Color::RGB { red, green, blue } => format!("#{:02x}{:02x}{:02x}", red, green, blue),
Color::HSV {
hue,
saturation,
value,
} => format!("hsv({},{}%,{}%)", hue, saturation, value),
}
}
/// Инвертира цвят покомпонентно -- за всяка от стойностите се взема разликата с максимума.
///
pub fn invert(&self) -> Self {
match self {
Color::RGB { red, green, blue } => Color::RGB {
red: 255 - red,
green: 255 - green,
blue: 255 - blue,
},
Color::HSV {
hue,
saturation,
value,
} => Color::HSV {
hue: 360 - hue,
saturation: 100 - saturation,
value: 100 - value,
},
}
}
}
#[cfg(test)]
mod tests {
use crate::Color;
#[test]
fn test_new_rgb() {
let color = Color::new_rgb(255, 0, 0);
match color {
Color::RGB { red, green, blue } => {
assert_eq!(red, 255);
assert_eq!(green, 0);
assert_eq!(blue, 0);
}
_ => panic!("Expected RGB color"),
}
}
#[test]
fn test_new_hsv() {
let color = Color::new_hsv(360, 100, 100);
match color {
Color::HSV {
hue,
saturation,
value,
} => {
assert_eq!(hue, 360);
assert_eq!(saturation, 100);
assert_eq!(value, 100);
}
_ => panic!("Expected HSV color"),
}
}
#[test]
#[should_panic]
fn test_new_hsv_panic() {
Color::new_hsv(361, 100, 100);
}
#[test]
#[should_panic]
fn test_new_hsv_panic2() {
Color::new_hsv(24, 101, 100);
}
#[test]
fn test_unwrap_rgb() {
let color = Color::new_rgb(255, 0, 0);
let (red, green, blue) = color.unwrap_rgb();
assert_eq!(red, 255);
assert_eq!(green, 0);
assert_eq!(blue, 0);
}
#[test]
#[should_panic]
fn test_unwrap_rgb_panic() {
let color = Color::new_hsv(360, 100, 100);
color.unwrap_rgb();
}
#[test]
fn test_unwrap_hsv() {
let color = Color::new_hsv(360, 100, 100);
let (hue, saturation, value) = color.unwrap_hsv();
assert_eq!(hue, 360);
assert_eq!(saturation, 100);
assert_eq!(value, 100);
}
#[test]
#[should_panic]
fn test_unwrap_hsv_panic() {
let color = Color::new_rgb(255, 0, 0);
color.unwrap_hsv();
}
#[test]
fn test_to_string_rgb() {
let color1 = Color::new_rgb(255, 1, 255);
let color2 = Color::new_rgb(0, 0, 0);
let color3 = Color::new_rgb(255, 255, 255);
assert_eq!(color1.to_string(), "#ff01ff");
assert_eq!(color2.to_string(), "#000000");
assert_eq!(color3.to_string(), "#ffffff");
}
#[test]
fn test_to_string_hsv() {
let color1 = Color::new_hsv(360, 100, 100);
let color2 = Color::new_hsv(0, 0, 0);
let color3 = Color::new_hsv(0, 0, 100);
assert_eq!(color1.to_string(), "hsv(360,100%,100%)");
assert_eq!(color2.to_string(), "hsv(0,0%,0%)");
assert_eq!(color3.to_string(), "hsv(0,0%,100%)");
}
#[test]
fn test_invert_rgb() {
let color1 = Color::new_rgb(255, 0, 255);
let color2 = Color::new_rgb(0, 0, 0);
let color3 = Color::new_rgb(254, 1, 3);
assert_eq!(color1.invert().unwrap_rgb(), (0, 255, 0));
assert_eq!(color2.invert().unwrap_rgb(), (255, 255, 255));
assert_eq!(color3.invert().unwrap_rgb(), (1, 254, 252));
assert_eq!(color1.invert().to_string(), "#00ff00");
assert_eq!(color2.invert().to_string(), "#ffffff");
assert_eq!(color3.invert().to_string(), "#01fefc");
}
#[test]
fn test_invert_hsv() {
let color1 = Color::new_hsv(0, 50, 0);
let color2 = Color::new_hsv(150, 30, 45);
let color3 = Color::new_hsv(10, 1, 78);
assert_eq!(color1.invert().unwrap_hsv(), (360, 50, 100));
assert_eq!(color2.invert().unwrap_hsv(), (210, 70, 55));
assert_eq!(color3.invert().unwrap_hsv(), (350, 99, 22));
assert_eq!(color1.invert().to_string(), "hsv(360,50%,100%)");
assert_eq!(color2.invert().to_string(), "hsv(210,70%,55%)");
assert_eq!(color3.invert().to_string(), "hsv(350,99%,22%)");
}
#[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_basic_2() {
let color1 = Color::new_rgb(255, 255, 255);
assert_eq!(color1.unwrap_rgb().0, 255);
assert_eq!(&color1.to_string()[0..1], "#");
let color2 = Color::new_hsv(360, 100, 100);
assert_eq!(color2.unwrap_hsv().0, 360);
assert_eq!(color1.invert().unwrap_rgb().0, 0);
}
}

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

Compiling solution v0.1.0 (/tmp/d20230111-3772066-10m9un8/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 ... 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 коментар)

Петко качи първо решение на 22.10.2022 14:57 (преди почти 3 години)