Решение на CSS Цветове от Искендер Чобанов

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

Към профила на Искендер Чобанов

Резултати

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

Код

#[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,green,blue}
}
/// Конструира нова стойност от вариант `HSV` с дадените стойности.
///
/// В случай, че hue е над 360 или saturation или value са над 100, очакваме да `panic!`-нете с
/// каквото съобщение си изберете.
///
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
if hue > 360 || value > 100 || saturation > 100
{
panic!("hsv upper limits: (hue(360),value(100),saturation(100)");
}
Color::HSV{hue,saturation,value}
}
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match &self
{
Color::RGB{red,green,blue} => (*red,*green,*blue),
_ => panic!("if you read this please try unwrap_hsv()")
}
}
/// Ако `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!("if you read this please try unwrap_rgb()")
}
}
pub fn to_string(&self) -> String {
match &self
{
Color::RGB{red,green,blue} =>
{
let mut string = String::from(format!("#{:02x}",red));
string.push_str(&format!("{:02x}",green));
string.push_str(&format!("{:02x}",blue));
return string;
},
Color::HSV{hue,saturation,value} =>
{
let mut string = String::from(format!("hsv({},",hue));
string.push_str(&format!("{}%,",saturation));
string.push_str(&format!("{}%)",value));
return string;
}
}
}
pub fn invert(&self) -> Self {
match &self
{
Color::RGB{red,green,blue} => {
let max_all = u8::MAX;
Color::new_rgb(max_all-*red,max_all-*green,max_all-*blue)
},
Color::HSV{hue,saturation,value} => {
let max_first = 360;
let max_rest = 100;
Color::new_hsv(max_first - hue,max_rest-saturation,max_rest-value)
}
}
}
}

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

Compiling solution v0.1.0 (/tmp/d20230111-3772066-xv7stf/solution)
    Finished test [unoptimized + debuginfo] target(s) in 0.63s
     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 версия и 0 коментара)

Искендер качи първо решение на 26.10.2022 20:49 (преди почти 3 години)