Решение на 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,
green,
blue
}
}
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
if hue > 360 || saturation > 100 || value > 100 {
panic!("That is an invalid color!");
}
Color::HSV {
hue,
saturation,
value
}
}
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match *self {
Color::RGB { red, green, blue } => {
(red, green, blue)
}
Color::HSV { .. } => {
panic!("Unwrap type error!");
}
}
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match *self {
Color::HSV { hue, saturation, value } => {
(hue, saturation, value)
}
Color::RGB { .. } => {
panic!("Unwrap type error!");
}
}
}
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}%)")
}
}
}
pub fn invert(&self) -> Self {
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)
}
}
}
}

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

Compiling solution v0.1.0 (/tmp/d20230111-3772066-evoyzb/solution)
    Finished test [unoptimized + debuginfo] target(s) in 0.65s
     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

История (2 версии и 0 коментара)

Йордан качи първо решение на 24.10.2022 22:32 (преди 7 месеца)

Йордан качи решение на 24.10.2022 22:34 (преди 7 месеца)

-extern crate core;
-
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,
green,
blue
}
}
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
if hue > 360 || saturation > 100 || value > 100 {
panic!("That is an invalid color!");
}
Color::HSV {
hue,
saturation,
value
}
}
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
match *self {
Color::RGB { red, green, blue } => {
(red, green, blue)
}
Color::HSV { .. } => {
panic!("Unwrap type error!");
}
}
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
match *self {
Color::HSV { hue, saturation, value } => {
(hue, saturation, value)
}
Color::RGB { .. } => {
panic!("Unwrap type error!");
}
}
}
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}%)")
}
}
}
pub fn invert(&self) -> Self {
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)
}
}
}
}