Решение на 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!();
}
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!(),
}
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match self
{
Color::HSV{hue,saturation,value} => (*hue,*saturation,*value),
_ => panic!(),
}
}
}
impl Color {
pub fn to_string(&self) -> String
{
match self
{
Color::RGB{red,green, blue} =>
{
let mut result = String::from("#");
let aaa = &convert(red);
result.push_str(aaa);
let aaa = &convert(green);
result.push_str(aaa);
let aaa = &convert(blue);
result.push_str(aaa);
result
},
Color::HSV{hue,saturation,value} =>
{
let mut result = String::from("hsv(");
result.push_str(&hue.to_string());
result.push_str(",");
result.push_str(&saturation.to_string());
result.push_str("%,");
result.push_str(&value.to_string());
result.push_str("%)");
result
},
}
}
}
fn convert(a : &u8) -> String
{
let mut res = String::from("");
match a/16
{
1 => res.push('1'),
2 => res.push('2'),
3 => res.push('3'),
4 => res.push('4'),
5 => res.push('5'),
6 => res.push('6'),
7 => res.push('7'),
8 => res.push('8'),
9 => res.push('9'),
10 => res.push('a'),
11 => res.push('b'),
12 => res.push('c'),
13 => res.push('d'),
14 => res.push('e'),
15 => res.push('f'),
_ => res.push('0'),
}
match a%16
{
1 => res.push('1'),
2 => res.push('2'),
3 => res.push('3'),
4 => res.push('4'),
5 => res.push('5'),
6 => res.push('6'),
7 => res.push('7'),
8 => res.push('8'),
9 => res.push('9'),
10 => res.push('a'),
11 => res.push('b'),
12 => res.push('c'),
13 => res.push('d'),
14 => res.push('e'),
15 => res.push('f'),
_ => res.push('0'),
}
res
}

Има и по-лесен начин да конвертираш до шестнадесетични числа -- погледни примерно моето решение за да видиш как. Разбира се, ако просто си искал да го имплементираш на ръка, that's fine too.

impl Color {
pub fn invert(&self) -> Self
{
match self
{
Color::RGB{red,green, blue} =>
{
Color::RGB
{
red : 255-red,
blue : 255-blue,
green : 255-green,
}
},
Color::HSV{hue,saturation,value} =>
{
Color::HSV
{
hue : 360-hue,
saturation : 100-saturation,
value : 100-value,
}
},
}
}
}

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

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

Стоян качи първо решение на 27.10.2022 15:37 (преди почти 3 години)