sharkdp / pastel

A command-line tool to generate, analyze, convert and manipulate colors
Apache License 2.0
5.04k stars 97 forks source link

Write helper function to get colorspace from string #56

Closed sharkdp closed 5 years ago

sharkdp commented 5 years ago

Instead of this:

            let color = match matches.value_of("colorspace").expect("required argument") {
                "rgb" => start.mix::<RGBA<f64>>(&stop, fraction),
                "hsl" => start.mix::<HSLA>(&stop, fraction),
                "lab" => start.mix::<Lab>(&stop, fraction),
                "lch" => start.mix::<LCh>(&stop, fraction),
                _ => unimplemented!("Unknown color space"),
            };

write a function to get a Box<dyn ColorSpace> from a &str. Something like:

fn get_representation(colorspace_name: &str, c: &Color) -> Box<dyn ColorSpace> {
    match colorspace_name {
        "RGB" => Box::new(RGB::from_color(c)),
        "HSL" => Box::new(HSL::from_color(c)),
        _ => unreachable!("Unknown color space"),
    }
}

Then, instead of calling Color::mix::<...>, directly call .mix(..) on the colorspace representations of "from" and "to" (in case of the gradient subcomand).