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

Add set luminance using a binary search #105

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/cli/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ pub fn build_cli() -> App<'static, 'static> {
Arg::with_name("property")
.help("The property that should be changed")
.possible_values(&["lightness", "hue", "chroma",
"lab-a", "lab-b",
"lab-a", "lab-b", "luminance",
"red", "green", "blue",
"hsl-hue", "hsl-saturation", "hsl-lightness"])
.case_insensitive(true)
Expand Down
32 changes: 31 additions & 1 deletion src/cli/commands/color_commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::colorspace::get_mixing_function;
use crate::commands::prelude::*;

use pastel::colorspace::ColorSpace;
use pastel::ColorblindnessType;
use pastel::Fraction;

Expand Down Expand Up @@ -136,7 +137,7 @@ color_command!(SetCommand, config, matches, color, {
}
Color::from_hsla(hsla.h, hsla.s, hsla.l, hsla.alpha)
}
"lightness" | "lab-a" | "lab-b" => {
"lightness" | "lab-a" | "lab-b" | "luminance" => {
let mut lab = color.to_lab();
match property {
"lightness" => {
Expand All @@ -148,6 +149,35 @@ color_command!(SetCommand, config, matches, color, {
"lab-b" => {
lab.b = value;
}
"luminance" => {
let mut luminance = lab.into_color().luminance();

let mut max_l = 100.0;
let mut min_l = 0.0;
let mut past_l = lab.l;

// Binary search to get to the luminance value we want by adjusting the
// lightness value in lab
while (luminance - value).abs() > 0.001 {
if luminance > value {
max_l = lab.l;
} else {
min_l = lab.l;
}

lab.l = (max_l + min_l) / 2.0;

// If we haven't made any changes, get out before we loop infinitely
if past_l == lab.l {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really the only thing that could happen? Couldn't the L value also toggle between two values? To be safe, we could also use an iteration counter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'll put in an iteration counter.

break;
}
past_l = lab.l;

// Do this in order to get the actual L value
lab = lab.into_color().to_lab();
luminance = lab.into_color().luminance();
}
}
_ => unreachable!(),
}
Color::from_lab(lab.l, lab.a, lab.b, lab.alpha)
Expand Down