Решение на CSS Цветове от Калин Борисов
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 5 успешни тест(а)
- 0 неуспешни тест(а)
Код
// First homework iteration Worked with basic tests
// Probably could be better written
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` с дадените стойности.
///
/// В случай, че 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 - 360");
}
if saturation > 100 {
panic!("saturation must be between 0 - 100");
}
if value > 100 {
panic!("value must be between 0 - 100");
}
Color::HSV{
hue: hue,
saturation: saturation,
value: value
}
}
}
impl Color {
/// Ако `self` е `RGB`, тогава връщате неговите `red`, `green`, `blue` компоненти в този ред.
/// Иначе, `panic!`-вате с каквото съобщение си изберете.
///
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match self {
Color::RGB {red,green,blue} => (*red,*green,*blue),
Color::HSV {..} => panic!("HSV color in a RGB method")
}
}
/// Ако `self` е `HSV`, тогава връщате неговите `hue`, `saturation`, `value` компоненти в този
/// ред. Иначе, `panic!`-вате с каквото съобщение си изберете.
///
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match self {
Color::HSV {hue,saturation,value} => (*hue, *saturation, *value),
Color::RGB {..} => panic!("RGB color in a HSV method")
}
}
}
impl Color {
/// Инвертира цвят покомпонентно -- за всяка от стойностите се взема разликата с максимума.
///
pub fn invert(&self) -> Self {
//todo!();// pattern match create new color with same type and invert vals
match self {
Color::RGB {red,green,blue} => Color::new_rgb(255 - *red,255 - *green,255 - *blue),
Color::HSV {hue,saturation,value} => Color::new_hsv(360-*hue,100-*saturation, 100 - *value)
}
}
}
/// В случай, че варианта на `self` е `RGB`, очакваме низ със съдържание `#rrggbb`, където
/// червения, зеления и синия компонент са форматирани в шестнадесетична система, и всеки от тях е
/// точно два символа с малки букви (запълнени с нули).
///
/// Ако варианта е `HSV`, очакваме низ `hsv(h,s%,v%)`, където числата са си напечатани в
/// десетичната система, без водещи нули, без интервали след запетаите, вторите две завършващи на
/// `%`.
///
impl Color{
pub fn to_string(&self) -> String { // not sure abot color impl
match self {
Color::RGB {red,green,blue} => hex_string_formatter(*red,*green,*blue),
Color::HSV {hue,saturation,value} => hsv_string_formatter(*hue,*saturation,*value),
}
}
}
fn hex_string_formatter(red: u8, green: u8, blue: u8) -> String {
format!("#{:02x}{:02x}{:02x}",red,green,blue)
}
fn hsv_string_formatter(hue: u16, saturation: u8, value: u8) -> String{
format!("hsv({},{}%,{}%)",hue,saturation,value)
}
/*fn main() {
let _temp = Color::RGB{red: 13,green: 23, blue: 32};
let _temp2 = Color::new_rgb(1,2,3);
println!("{}", _temp.unwrap_rgb().0);
println!("{}", _temp.unwrap_rgb().1);
println!("{}", _temp.unwrap_rgb().2);
println!("{}", _temp2.unwrap_rgb().0);
println!("{}", _temp2.unwrap_rgb().1);
println!("{}", _temp2.unwrap_rgb().2);
let _temp3 = _temp.invert();
println!("{}", _temp3.unwrap_rgb().0);
println!("{}", _temp3.unwrap_rgb().1);
println!("{}", _temp3.unwrap_rgb().2);
println!("{}", Color::new_hsv(1,2,3).invert().to_string());
println!("{}", Color::new_rgb(0, 0, 0).to_string()); //=> #000000
println!("{}", Color::new_rgb(255, 1, 255).to_string()); //=> #ff01ff
println!("{}", Color::new_hsv(0, 0, 0).to_string()); //=> hsv(0,0%,0%)
println!("{}", Color::new_hsv(360, 100, 100).to_string()); //=> hsv(360,100%,100%)
}*/
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20230111-3772066-1l3ssdc/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