8000 feat: add lightness option by make-42 · Pull Request #153 · InioX/matugen · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add lightness option #153

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
- **Custom Keywords/Colors:**
- Define your own custom keywords or colors you would like to be harmonized inside the config file, that you can then use in templates
- **Palette Customization:**
- Customize the contrast and scheme type for the palette
- Customize the contrast, lightness and scheme type for the palette
- **Restart Apps/Change Wallpaper:**
- Restart supported apps and set the wallpaper on Windows, MacOS, Linux and NetBSD

Expand Down
2 changes: 1 addition & 1 deletion benches/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn parse_template(data: &str) {

let (scheme_dark, scheme_light) = get_schemes(source_color, &None, &None);
let schemes =
get_custom_color_schemes(source_color, scheme_dark, scheme_light, &None, &None, &None);
get_custom_color_schemes(source_color, scheme_dark, scheme_light, &None, &None, &None, &None, &None);
let render_data =
get_render_data(&schemes, &source_color, &SchemesEnum::Dark, &None, None).unwrap();

Expand Down
18 changes: 18 additions & 0 deletions module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ matugen: {
--type ${cfg.type} \
--json ${cfg.jsonFormat} \
--contrast ${lib.strings.floatToString cfg.contrast} \
--lightness-dark ${lib.strings.floatToString cfg.lightness_dark} \
--lightness-light ${lib.strings.floatToString cfg.lightness_light} \
--quiet \
> $out/theme.json
'');
Expand Down Expand Up @@ -189,6 +191,22 @@ in {
default = 0;
example = "0.2";
};

lightness_dark = lib.mkOption {
description = "Value from -∞ to 1. -∞ represents minimum lightness, 0 represents standard (i.e. the design as spec'd), and 1 represents maximum lightness. For dark schemes, if the considered lightnesses are between 0 and 1 then this applies an affine transformation to the lightness by keeping the value for 1 at 1 and setting the value for 0 to the lightness argument and then clamping the result";
type = lib.types.addCheck lib.types.number (lightness_dark: lightness_dark <= 1);
default = 0;
example = "0.2";

check = lightness_light: lightness_light >= -1;
};

lightness_light = lib.mkOption {
description = "Value from -1 to +∞. -1 represents minimum lightness, 0 represents standard (i.e. the design as spec'd), and +∞ represents maximum lightness. For light schemes, if the considered lightnesses are between 0 and 1 then this applies an affine transformation to the lightness by keeping the value for 0 at 0 and setting the value for 1 to (1 + the lightness argument) and then clamping the result";
type = lib.types.addCheck lib.types.number (lightness_light: lightness_light >= -1);
default = 0;
example = "0.2";
};

config = lib.mkOption {
description = "Add things to the config not covered by other options.";
Expand Down
20 changes: 20 additions & 0 deletions src/color/color.rs
Original file line number Diff 8000 line number Diff line change
Expand Up @@ -177,6 +177,26 @@ pub fn generate_dynamic_scheme(
}
}

pub fn adjust_color_lightness_dark(
color: Argb,
lightness_level_dark: &Option<f64>,
) -> Argb {
// If lightness values were plotted on a graph, the effect of this function is to rotate the line corresponding to the identity function about x = 255 and y = 255 by setting the value at x = 0 to -lightness_level*255 and then clamping the values to between 0 and 255.
let pre_lightness_level = ((color.red as f64) + (color.green as f64) + (color.blue as f64))/ 3.0;
let adj = (pre_lightness_level / 255.0 * (1.0 - lightness_level_dark.unwrap_or(0.0)) + lightness_level_dark.unwrap_or(0.0)) / pre_lightness_level* 255.0;
Argb::new(color.alpha, (color.red as f64 * adj).clamp(0.0, 255.0) as u8, (color.green as f64 * adj).clamp(0.0, 255.0) as u8, (color.blue as f64 * adj).clamp(0.0, 255.0) as u8)
}

pub fn adjust_color_lightness_light(
color: Argb,
lightness_level_light: &Option<f64>,
) -> Argb {
// If lightness values were plotted on a graph, the effect of this function is to rotate the line corresponding to the identity function about x = 0 and y = 0 by setting the value at x = 255 to 255+lightness_level*255 and then clamping the values to between 0 and 255.
let pre_lightness_level = ((color.red as f64) + (color.green as f64) + (color.blue as f64))/ 3.0;
let adj = pre_lightness_level / 255.0 * (1.0 + lightness_level_light.unwrap_or(0.0)) / pre_lightness_level* 255.0;
Argb::new(color.alpha, (color.red as f64 * adj).clamp(0.0, 255.0) as u8, (color.green as f64 * adj).clamp(0.0, 255.0) as u8, (color.blue as f64 * adj).clamp(0.0, 255.0) as u8)
}

pub fn make_custom_color(
color: CustomColor,
scheme_type: &Option<SchemeTypes>,
Expand Down
10 changes: 10 additions & 0 deletions src/gui/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ impl MyApp {
&mut self.app.args.contrast.unwrap(),
-1.0..=1.0,
));
ui.label("Lightness (Dark)");
ui.add(egui::Slider::new(
&mut self.app.args.lightness_dark.unwrap(),
-100.0..=1.0,
));
ui.label("Lightness (Light)");
ui.add(egui::Slider::new(
&mut self.app.args.lightness_light.unwrap(),
-1.0..=100.0,
));
});
ui.label("Scheme type");
egui::ComboBox::from_label("")
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ impl State {
&config_file.config.custom_colors,
&args.r#type,
&args.contrast,
&args.lightness_dark,
&args.lightness_light,
);

Self {
Expand All @@ -84,6 +86,8 @@ impl State {
&self.config_file.config.custom_colors,
&self.args.r#type,
&self.args.contrast,
&self.args.lightness_dark,
&self.args.lightness_light,
);
}

Expand Down Expand Up @@ -189,6 +193,8 @@ fn main() -> Result<(), Report> {
config: None,
prefix: None,
contrast: Some(0.0),
lightness_dark: Some(0.0),
lightness_light: Some(0.0),
verbose: Some(true),
quiet: None,
debug: Some(true),
Expand Down
8 changes: 5 additions & 3 deletions src/scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;
use material_colors::scheme::Scheme;
use std::collections::BTreeSet;

use crate::color::color::{generate_dynamic_scheme, make_custom_color, OwnCustomColor};
use crate::color::color::{generate_dynamic_scheme, adjust_color_lightness_dark, adjust_color_lightness_light, make_custom_color, OwnCustomColor};

#[allow(clippy::enum_variant_names)]
#[derive(Clone, clap::ValueEnum, Debug, Copy, PartialEq)]
Expand Down Expand Up @@ -60,6 +60,8 @@ pub fn get_custom_color_schemes(
custom_colors: &Option<HashMap<String, OwnCustomColor, std::hash::RandomState>>,
scheme_type: &Option<SchemeTypes>,
contrast: &Option<f64>,
lightness_dark: &Option<f64>,
lightness_light: &Option<f64>,
) -> Schemes {
macro_rules! from_color {
($color: expr, $variant: ident) => {
Expand Down Expand Up @@ -103,8 +105,8 @@ pub fn get_custom_color_schemes(
let custom_colors_light = custom_colors.flat_map(|c| from_color!(c, light));

let schemes: Schemes = Schemes {
dark: BTreeSet::from_iter(scheme_dark.into_iter().chain(custom_colors_dark)),
light: BTreeSet::from_iter(scheme_light.into_iter().chain(custom_colors_light)),
dark: BTreeSet::from_iter(scheme_dark.into_iter().chain(custom_colors_dark).map(|(name, color)| (name,adjust_color_lightness_dark(color, lightness_dark)))),
light: BTreeSet::from_iter(scheme_light.into_iter().chain(custom_colors_light).map(|(name, color)| (name,adjust_color_lightness_light(color, lightness_light)))),
};
schemes
}
Expand Down
16 changes: 16 additions & 0 deletions src/util/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ pub struct Cli {
/// standard (i.e. the design as spec'd), and 1 represents maximum contrast.
#[arg(long, global = true, allow_negative_numbers = true)]
pub contrast: Option<f64>,

/// Value from -∞ to 1. -∞ represents minimum lightness, 0 represents
/// standard (i.e. the design as spec'd), and 1 represents maximum lightness.
/// For dark schemes, if the considered lightnesses are between 0 and 1 then this applies an affine
/// transformation to the lightness by keeping the value for 1 at 1 and setting the
/// value for 0 to the lightness argument and then clamping the result
#[arg(long, global = true, allow_negative_numbers = true)]
pub lightness_dark: Option<f64>,

/// Value from -1 to +∞. -1 represents minimum lightness, 0 represents
/// standard (i.e. the design as spec'd), and +∞ represents maximum lightness.
/// For light schemes, if the considered lightnesses are between 0 and 1 then this applies an affine
/// transformation to the lightness by keeping the value for 0 at 0 and setting the
/// value for 1 to (1 + the lightness argument) and then clamping the result
#[arg(long, global = true, allow_negative_numbers = true)]
pub lightness_light: Option<f64>,

#[arg(short, long, global = true, action=ArgAction::SetTrue)]
pub verbose: Option<bool>,
Expand Down
0