Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CMYK color space #17

Merged
merged 2 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Color {
match color_space {
ColorSpace::Rgb => Color::Rgb(rgb),
ColorSpace::Cmy => Color::Cmy(Cmy::from_rgb(&rgb)),
ColorSpace::Cmyk => Color::Cmyk(Cmyk::from_rgb(&rgb)),
ColorSpace::Cmyk => Color::Cmyk(cmyk_from_rgb(&rgb)),
ColorSpace::Hsv => Color::Hsv(Hsv::from_rgb(&rgb)),
ColorSpace::Hsl => Color::Hsl(Hsl::from_rgb(&rgb)),
ColorSpace::Lch => Color::Lch(Lch::from_rgb(&rgb)),
Expand All @@ -74,6 +74,20 @@ impl Color {
}
}

fn cmyk_from_rgb(rgb: &Rgb) -> Cmyk {
let cmy = Cmy::from_rgb(rgb);
let k = cmy.c.min(cmy.m.min(cmy.y.min(1.0)));
match (k - 1.0).abs() < 1e-3 {
true => Cmyk::new(0.0, 0.0, 0.0, k),
false => Cmyk::new(
(cmy.c - k) / (1.0 - k),
(cmy.m - k) / (1.0 - k),
(cmy.y - k) / (1.0 - k),
k,
),
}
}

impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (color_space, parts) = self.divide();
Expand Down
10 changes: 8 additions & 2 deletions src/command_line.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{bail, Context, Result};
use clap::{App, Arg, ArgGroup};
use clap::{App, AppSettings, Arg, ArgGroup};

use crate::color::{Color, ColorSpace};

Expand Down Expand Up @@ -68,6 +68,8 @@ The input color. Supported formats:
/// clap terminates the application, so this function never returns.
fn clap_args() -> clap::ArgMatches<'static> {
App::new(APP_NAME)
.global_setting(AppSettings::ColorAuto)
.global_setting(AppSettings::ColoredHelp)
.version(APP_VERSION)
.version_short("v")
.author("Ludwig Stecher <[email protected]>")
Expand Down Expand Up @@ -112,8 +114,12 @@ fn clap_args() -> clap::ArgMatches<'static> {
.long("out")
.short("o")
.takes_value(true)
.help("Output color space")
.help(
"Output color space [possible values: rgb, cmy, \
cmyk, hsv, hsl, lch, luv, lab, hunterlab, xyz, yxy]",
)
.possible_values(COLOR_SPACES)
.hide_possible_values(true)
.case_insensitive(true)
.requires("COLOR"),
)
Expand Down