Решение на 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 {
/// Конструира нова стойност от вариант `RGB` с дадените стойности за червено, зелено и синьо.
///
pub fn new_rgb(red: u8, green: u8, blue: u8) -> Color {
return 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 {
panic!("Hue must be between 0 and 360!");
}
if saturation > 100 {
panic!("Saturation must be between 0 and 100!")
}
if value > 100 {
panic!("Value must be between 0 and 100!")
}
return Color::HSV {
hue,
saturation,
value,
};
}
}
impl Color {
/// Ако `self` е `RGB`, тогава връщате неговите `red`, `green`, `blue` компоненти в този ред.
/// Иначе, `panic!`-вате с каквото съобщение си изберете.
///
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match self {
Color::RGB {red, green, blue} => return (*red, *green, *blue),
_ => panic!("unrwap_rgb is only for color of type rgb."),
}
}
/// Ако `self` е `HSV`, тогава връщате неговите `hue`, `saturation`, `value` компоненти в този
/// ред. Иначе, `panic!`-вате с каквото съобщение си изберете.
///
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match self {
Color::HSV{ hue, saturation, value } => return (*hue, *saturation, *value),
_ => panic!("unrwap_hsv is only for color of type HSV."),
}
}
}
impl Color {
/// В случай, че варианта на `self` е `RGB`, очакваме низ със съдържание `#rrggbb`, където
/// червения, зеления и синия компонент са форматирани в шестнадесетична система, и всеки от тях е
/// точно два символа с малки букви (запълнени с нули).
///
/// Ако варианта е `HSV`, очакваме низ `hsv(h,s%,v%)`, където числата са си напечатани в
/// десетичната система, без водещи нули, без интервали след запетаите, вторите две завършващи на
/// `%`.
///
pub fn to_string(&self) -> String {
match self {
Color::RGB{red, green, blue} => return Self::rgb_to_string(*red, *green, *blue),
Color::HSV{hue, saturation, value} => Self::hsv_to_string(*hue, *saturation, *value)
}
}
pub fn rgb_to_string(red: u8, green: u8, blue: u8) -> String {
let red_hexdecimal = format!("{:02x}", red);
let green_hexdecimal = format!("{:02x}", green);
let blue_hexdecimal = format!("{:02x}", blue);
let hexdecimal = format!("#{}{}{}", red_hexdecimal, green_hexdecimal, blue_hexdecimal);
return hexdecimal;
}
//hsv(h,s%,v%)
pub fn hsv_to_string(hue: u16, saturation: u8, value: u8) -> String {
let hsv = format!("hsv({},{}%,{}%)", hue, saturation, value);
return hsv;
}
}
impl Color {
/// Инвертира цвят покомпонентно -- за всяка от стойностите се взема разликата с максимума.
///
pub fn invert(&self) -> Self {
match self {
Color::RGB{red, green, blue} => {
let inverted_rgb = Self::invert_rgb(*red, *green, *blue);
return Color::new_rgb(inverted_rgb.0, inverted_rgb.1, inverted_rgb.2);
},
Color::HSV{hue, saturation, value} => {
let inverted_hsv = Self::invert_hsv(*hue, *saturation, *value);
return Color::new_hsv(inverted_hsv.0, inverted_hsv.1, inverted_hsv.2)
}
}
}
pub fn invert_rgb(red: u8, green: u8, blue: u8) -> (u8, u8, u8) {
let red_inverted = 255 - red;
let green_inverted = 255 - green;
let blue_inverted = 255 - blue;
return (red_inverted, green_inverted, blue_inverted);
}
pub fn invert_hsv(hue: u16, saturation: u8, value: u8) -> (u16, u8, u8) {
let hue_inverted = 360 - hue;
let saturation_inverted = 100 - saturation;
let value_inverted = 100 - value;
return (hue_inverted, saturation_inverted, value_inverted);
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20230111-3772066-1w9dwy5/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