Решение на CSS Цветове от Радослав Радков

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

Към профила на Радослав Радков

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 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 {
if hue > 360 || value > 100 || saturation > 100 {
panic!("hue is over 360 or saturation is over 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)
}
Color::HSV{..} => {
panic!("self is not RGB");
}
}
}
/// Ако `self` е `HSV`, тогава връщате неговите `hue`, `saturation`, `value` компоненти в този
/// ред. Иначе, `panic!`-вате с каквото съобщение си изберете.
///
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match self{
Color::RGB{..} => {
panic!("self is not HSV");
}
Color::HSV{hue,saturation,value} => {
(*hue,*saturation,*value)
}
}
}
/// В случай, че варианта на `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)
}
}
}
}
fn main() {
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-1muu17j/solution)
warning: function `main` is never used
  --> src/lib.rs:82:4
   |
82 | fn main() {
   |    ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `solution` (lib) generated 1 warning
error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:32:22
   |
32 |     assert_eq!(black.invert().unwrap_rgb(), white.unwrap_rgb());
   |                      ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:33:22
   |
33 |     assert_eq!(white.invert().unwrap_rgb(), black.unwrap_rgb());
   |                      ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:37:23
   |
37 |     assert_eq!(color1.invert().unwrap_rgb(), color2.unwrap_rgb());
   |                       ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:38:23
   |
38 |     assert_eq!(color2.invert().unwrap_rgb(), color1.unwrap_rgb());
   |                       ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:40:23
   |
40 |     assert_eq!(color1.invert().invert().unwrap_rgb(), color1.unwrap_rgb());
   |                       ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:41:23
   |
41 |     assert_eq!(color2.invert().invert().unwrap_rgb(), color2.unwrap_rgb());
   |                       ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:49:21
   |
49 |     assert_eq!(zero.invert().unwrap_hsv(), full.unwrap_hsv());
   |                     ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:50:21
   |
50 |     assert_eq!(full.invert().unwrap_hsv(), zero.unwrap_hsv());
   |                     ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:54:23
   |
54 |     assert_eq!(color1.invert().unwrap_hsv(), color2.unwrap_hsv());
   |                       ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:55:23
   |
55 |     assert_eq!(color2.invert().unwrap_hsv(), color1.unwrap_hsv());
   |                       ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:57:23
   |
57 |     assert_eq!(color1.invert().invert().unwrap_hsv(), color1.unwrap_hsv());
   |                       ^^^^^^ method not found in `solution::Color`

error[E0599]: no method named `invert` found for enum `solution::Color` in the current scope
  --> tests/solution_test.rs:58:23
   |
58 |     assert_eq!(color2.invert().invert().unwrap_hsv(), color2.unwrap_hsv());
   |                       ^^^^^^ method not found in `solution::Color`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `solution` due to 12 previous errors

История (1 версия и 3 коментара)

Радослав качи първо решение на 27.10.2022 11:44 (преди почти 3 години)

Това домашно не се компилира, защото ти липсва функция invert, както сме описали в условието. Ако не добавиш тази функция, дори и да е с todo!(), ще получиш 0 точки.

Както мисля че се изяснихме по други канали, не си видял съобщението преди крайния срок. За следващото домашно, пусни решение по-отрано, за да има време за такива поправки.