3use std::cmp::Ordering::Equal;
7use palette::{FromColor, Hsv, Srgb};
9const VERSION: &str = env!("CARGO_PKG_VERSION");
19 let matches = clap::Command::new("brightness_adjust")
21 .author("Alex Chan <alex@alexwlchan.net>")
22 .about("Show some darker/lighter variants of a given colour")
25 .help("colour to start with as a hex string, e.g. #d01c11")
31 // This .unwrap() is safe because "path" is a required param
32 let base_colour = matches
33 .get_one::<String>("COLOUR")
34 .expect("`COLOUR` is required");
36 let rgb = match Srgb::from_str(base_colour) {
37 Ok(c) => c.into_format::<u8>(),
39 eprintln!("Unable to parse COLOUR {:?} as hex: {}", base_colour, e);
40 std::process::exit(1);
44 let hsv: Hsv = Hsv::from_color(rgb.into_format::<f32>());
46 let mut outputs = vec![OutputValue {
52 for value in (0..=100).step_by(5) {
53 let modified_hsv = Hsv::new(hsv.hue, hsv.saturation, value as f32 / 100.0);
54 outputs.push(OutputValue {
55 value: modified_hsv.value,
56 rgb: Srgb::from_color(modified_hsv).into_format::<u8>(),
61 // https://www.reddit.com/r/rust/comments/29kia3/comment/cilrzik/
62 outputs.sort_by(|a, b| a.value.partial_cmp(&b.value).unwrap_or(Equal));
67 "{:3}% = \x1B[38;2;{};{};{}m▇ #{:02x}{:02x}{:02x}\x1B[0m",
68 (op.value * 100.0).round(),
77 println!(" (original colour)");