Skip to content

Commit

Permalink
Add 'pastel set' subcommand, closes sharkdp#43
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Aug 29, 2019
1 parent 21d0318 commit 0db8ba1
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/cli/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ pub fn build_cli() -> App<'static, 'static> {
)
.arg(color_arg.clone()),
)
.subcommand(
SubCommand::with_name("set")
.about("Set a color property to a specific value")
.long_about("Set the given property to a specific value\n\
Example:\n \
pastel random | pastel set luminance 0.9")
.arg(
Arg::with_name("property")
.help("The property that should be changed")
.possible_values(&["lightness", "hue", "chroma",
"lab-a", "lab-b",
"red", "green", "blue",
"hsl-hue", "hsl-saturation", "hsl-lightness"])
.case_insensitive(true)
.required(true),
)
.arg(
Arg::with_name("value")
.help("The new numerical value of the property")
.required(true),
)
.arg(color_arg.clone()),
)
.subcommand(
SubCommand::with_name("saturate")
.long_about(
Expand Down
80 changes: 80 additions & 0 deletions src/cli/commands/color_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use crate::colorspace::get_mixing_function;
use crate::commands::prelude::*;
use pastel::Fraction;

fn clamp(lower: f64, upper: f64, x: f64) -> f64 {
f64::max(f64::min(upper, x), lower)
}

use super::show::show_color;

macro_rules! color_command {
Expand Down Expand Up @@ -72,3 +76,79 @@ color_command!(MixCommand, config, matches, color, {

mix(&base, &color, fraction)
});

color_command!(SetCommand, config, matches, color, {
let property = matches.value_of("property").expect("required argument");
let property = property.to_lowercase();
let property = property.as_ref();

let value = number_arg(matches, "value")?;

match property {
"red" | "green" | "blue" => {
let mut rgba = color.to_rgba();
let value = clamp(0.0, 255.0, value) as u8;
match property {
"red" => {
rgba.r = value;
}
"green" => {
rgba.g = value;
}
"blue" => {
rgba.b = value;
}
_ => unreachable!(),
}
Color::from_rgba(rgba.r, rgba.g, rgba.b, rgba.alpha)
}
"hsl-hue" | "hsl-saturation" | "hsl-lightness" => {
let mut hsla = color.to_hsla();
match property {
"hsl-hue" => {
hsla.h = value;
}
"hsl-saturation" => {
hsla.s = value;
}
"hsl-lightness" => {
hsla.l = value;
}
_ => unreachable!(),
}
Color::from_hsla(hsla.h, hsla.s, hsla.l, hsla.alpha)
}
"lightness" | "lab-a" | "lab-b" => {
let mut lab = color.to_lab();
match property {
"lightness" => {
lab.l = value;
}
"lab-a" => {
lab.a = value;
}
"lab-b" => {
lab.b = value;
}
_ => unreachable!(),
}
Color::from_lab(lab.l, lab.a, lab.b, lab.alpha)
}
"hue" | "chroma" => {
let mut lch = color.to_lch();
match property {
"hue" => {
lch.h = value;
}
"chroma" => {
lch.c = value;
}
_ => unreachable!(),
}
Color::from_lch(lch.l, lch.c, lch.h, lch.alpha)
}
&_ => {
unreachable!("Unknown property");
}
}
});
2 changes: 1 addition & 1 deletion src/cli/commands/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{PastelError, Result};
use pastel::Color;

pub fn number_arg(matches: &ArgMatches, name: &str) -> Result<f64> {
let value_str = matches.value_of(name).unwrap();
let value_str = matches.value_of(name).expect("required argument");
value_str
.parse::<f64>()
.map_err(|_| PastelError::CouldNotParseNumber(value_str.into()))
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Command {
"lighten" => Command::WithColor(Box::new(color_commands::LightenCommand)),
"darken" => Command::WithColor(Box::new(color_commands::DarkenCommand)),
"rotate" => Command::WithColor(Box::new(color_commands::RotateCommand)),
"set" => Command::WithColor(Box::new(color_commands::SetCommand)),
"complement" => Command::WithColor(Box::new(color_commands::ComplementCommand)),
"mix" => Command::WithColor(Box::new(color_commands::MixCommand)),
"to-gray" => Command::WithColor(Box::new(color_commands::ToGrayCommand)),
Expand Down
30 changes: 30 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,33 @@ fn sort_by_basic() {
.success()
.stdout("hsl(0,0.0%,0.0%)\nhsl(0,0.0%,50.2%)\nhsl(0,0.0%,100.0%)\n");
}

#[test]
fn set_basic() {
pastel()
.arg("set")
.arg("hsl-hue")
.arg("120")
.arg("red")
.assert()
.success()
.stdout("hsl(120,100.0%,50.0%)\n");

pastel()
.arg("set")
.arg("hsl-saturation")
.arg("0.1")
.arg("red")
.assert()
.success()
.stdout("hsl(0,10.0%,50.0%)\n");

pastel()
.arg("set")
.arg("hsl-lightness")
.arg("0.5")
.arg("white")
.assert()
.success()
.stdout("hsl(0,0.0%,50.0%)\n");
}

0 comments on commit 0db8ba1

Please sign in to comment.