Skip to content

Commit

Permalink
Merge pull request #56 from InioX/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
InioX authored Mar 3, 2024
2 parents 0b0bed0 + 59042ef commit 1b1ca86
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ upon = "0.8.0"
reqwest = { version = "0.11.23", default_features=false, features = ["blocking", "rustls-tls"] }
material-colors = "0.1.4"
ahash = "0.8.7"
indexmap = "2.2.2"
indexmap = "2.2.2"
71 changes: 23 additions & 48 deletions example/colors.whatever-extension
Original file line number Diff line number Diff line change
@@ -1,50 +1,25 @@
red #FF0000;
green #00FF00;
blue #0000FF;

test {{ colors.source_color.default.rgb | set_lightness: 20.0 }}
test {{ colors.source_color.default.rgb | set_lightness: -20.0 }}
test {{ colors.source_color.default.rgb }}

{{ colors.source_color.default.hex_stripped | set_lightness: 20.0 }}

{{ colors.source_color.default.hex | set_lightness: 20.0 }}

Should not change at all
{{ colors.source_color.default.red | set_lightness: 5.0 }}

red {{harmonized_colors.red.rgb}};
red {{harmonized_colors.red.rgba}};
green {{harmonized_colors.green.hsl}};
blue {{harmonized_colors.blue.hsla}};

source_color {{colors.source_color.default.hex}};

primary {{colors.primary.default.hex}};
on_primary {{colors.on_primary.default.hex}};
primary_container {{colors.primary_container.default.hex}};
on_primary_container {{colors.on_primary_container.default.hex}};
inverse_primary {{colors.inverse_primary.default.hex}};
primary_fixed {{colors.primary_fixed.default.hex}};
primary_fixed_dim {{colors.primary_fixed_dim.default.hex}};
on_primary_fixed {{colors.on_primary_fixed.default.hex}};
on_primary_fixed_variant {{colors.on_primary_fixed_variant.default.hex}};
secondary {{colors.secondary.default.hex}};
on_secondary {{colors.on_secondary.default.hex}};
secondary_container {{colors.secondary_container.default.hex}};
on_secondary_container {{colors.on_secondary_container.default.hex}};
secondary_fixed {{colors.secondary_fixed.default.hex}};
secondary_fixed_dim {{colors.secondary_fixed_dim.default.hex}};
on_secondary_fixed {{colors.on_secondary_fixed.default.hex}};
on_secondary_fixed_variant {{colors.on_secondary_fixed_variant.default.hex}};
tertiary {{colors.tertiary.default.hex}};
on_tertiary {{colors.on_tertiary.default.hex}};
tertiary_container {{colors.tertiary_container.default.hex}};
on_tertiary_container {{colors.on_tertiary_container.default.hex}};
tertiary_fixed {{colors.tertiary_fixed.default.hex}};
tertiary_fixed_dim {{colors.tertiary_fixed_dim.default.hex}};
on_tertiary_fixed {{colors.on_tertiary_fixed.default.hex}};
on_tertiary_fixed_variant {{colors.on_tertiary_fixed_variant.default.hex}};
error {{colors.error.default.hex}};
on_error {{colors.on_error.default.hex}};
error_container {{colors.error_container.default.hex}};
on_error_container {{colors.on_error_container.default.hex}};
surface_dim {{colors.surface_dim.default.hex}};
surface {{colors.surface.default.hex}};
surface_bright {{colors.surface_bright.default.hex}};
surface_container_lowest {{colors.surface_container_lowest.default.hex}};
surface_container_low {{colors.surface_container_low.default.hex}};
surface_container {{colors.surface_container.default.hex}};
surface_container_high {{colors.surface_container_high.default.hex}};
surface_container_highest {{colors.surface_container_highest.default.hex}};
on_surface {{colors.on_surface.default.hex}};
on_surface_variant {{colors.on_surface_variant.default.hex}};
outline {{colors.outline.default.hex}};
outline_variant {{colors.outline_variant.default.hex}};
inverse_surface {{colors.inverse_surface.default.hex}};
inverse_on_surface {{colors.inverse_on_surface.default.hex}};
surface_variant {{colors.surface_variant.default.hex}};
background {{colors.background.default.hex}};
on_background {{colors.on_background.default.hex}};
shadow {{colors.shadow.default.hex}};
scrim {{colors.scrim.default.hex}};
<* for name, value in colors *>
{{name | replace: "_", "-" }} {{value.default.hex}};
<* endfor *>
10 changes: 9 additions & 1 deletion example/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ swww_options = [
"center",
]

[config.custom_keywords]
test = "aaaa"

[templates.name1]
input_path = "example/colors.whatever-extension"
output_path = "example/a/colors-generated.whatever-extension"
output_path = "example/a/colors-generated.whatever-extension"

[config.colors_to_harmonize]
green = "#00FF00"
red = "#FF0000"
blue = "#0000FF"
35 changes: 26 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
use std::io::Write;
use update_informer::{registry, Check};

use util::arguments::SchemeTypes;
use util::{arguments::SchemeTypes, color::harmonize_colors};

use material_colors::{
SchemeContent, SchemeExpressive, SchemeFidelity, SchemeFruitSalad, SchemeMonochrome,
Expand Down Expand Up @@ -76,6 +76,12 @@ fn main() -> Result<(), Report> {
light: IndexMap::from_iter(scheme_light),
};

let mut harmonized_colors = None;

if let Some(colors) = &config.config.colors_to_harmonize {
harmonized_colors = Some(harmonize_colors(&source_color, colors));
};

if args.show_colors == Some(true) {
show_color(&schemes, &source_color);
}
Expand All @@ -93,6 +99,7 @@ fn main() -> Result<(), Report> {
&source_color,
&default_scheme,
&config.config.custom_keywords,
harmonized_colors,
)?;

if config.config.reload_apps == Some(true) {
Expand Down Expand Up @@ -170,34 +177,44 @@ fn generate_scheme(
) -> Scheme {
match scheme_type.unwrap() {
SchemeTypes::SchemeContent => {
return Scheme::from(SchemeContent::new(Hct::new(source_color), is_dark, contrast_level).scheme)
Scheme::from(
SchemeContent::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeExpressive => {
return Scheme::from(
Scheme::from(
SchemeExpressive::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeFidelity => {
return Scheme::from(SchemeFidelity::new(Hct::new(source_color), is_dark, contrast_level).scheme)
Scheme::from(
SchemeFidelity::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeFruitSalad => {
return Scheme::from(
Scheme::from(
SchemeFruitSalad::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeMonochrome => {
return Scheme::from(
Scheme::from(
SchemeMonochrome::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeNeutral => {
return Scheme::from(SchemeNeutral::new(Hct::new(source_color), is_dark, contrast_level).scheme)
Scheme::from(
SchemeNeutral::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeRainbow => {
return Scheme::from(SchemeRainbow::new(Hct::new(source_color), is_dark, contrast_level).scheme)
Scheme::from(
SchemeRainbow::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeTonalSpot => {
return Scheme::from(SchemeTonalSpot::new(Hct::new(source_color), is_dark, contrast_level).scheme)
Scheme::from(
SchemeTonalSpot::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
}
}
106 changes: 92 additions & 14 deletions src/util/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::Schemes;

use crate::util::image::fetch_image;

use image::io::Reader as ImageReader;
use image::imageops::{resize, FilterType};
use image::io::Reader as ImageReader;

use super::arguments::{ColorFormat, Format, Source};
use super::image::source_color_from_image;
Expand All @@ -17,6 +17,8 @@ use serde_json::json;
use std::collections::HashMap;
use std::str::FromStr;

use material_colors::blend::harmonize;

pub fn rgb_from_argb(color: [u8; 4]) -> Rgb {
Rgb::from([
color[1] as f64,
Expand All @@ -26,6 +28,76 @@ pub fn rgb_from_argb(color: [u8; 4]) -> Rgb {
])
}

pub fn format_hex(color: &Rgb) -> String {
color.to_hex_string()
}

pub fn format_hex_stripped(color: &Rgb) -> String {
color.to_hex_string()[1..].to_string()
}

pub fn format_rgb(color: &Rgb) -> String {
format!(
"rgb({:?}, {:?}, {:?})",
color.red() as u8,
color.green() as u8,
color.blue() as u8,
)
}

pub fn format_rgba(color: &Rgb) -> String {
format!(
"rgba({:?}, {:?}, {:?}, {:?})",
color.red() as u8,
color.green() as u8,
color.blue() as u8,
color.alpha() as u8
)
}

pub fn format_hsl(color: &Hsl) -> String {
format!(
"hsl({:?}, {:?}%, {:?}%)",
color.hue() as u8,
color.saturation() as u8,
color.lightness() as u8,
)
}

pub fn format_hsla(color: &Hsl) -> String {
format!(
"hsla({:?}, {:?}%, {:?}%, {:?})",
color.hue() as u8,
color.saturation() as u8,
color.lightness() as u8,
color.alpha() as u8
)
}

pub fn harmonize_colors(
source_color: &[u8; 4],
colors: &HashMap<String, String>,
) -> HashMap<String, [u8; 4]> {
debug!("colors_to_harmonize: {:#?}", &colors);
let mut harmonized_colors: HashMap<String, [u8; 4]> = HashMap::default();

for (name, color) in colors {
let rgb = Rgb::from_hex_str(color)
.expect("Invalid hex color string provided for `harmonized_colors`");

let argb: [u8; 4] = [
rgb.alpha() as u8,
rgb.red() as u8,
rgb.green() as u8,
rgb.blue() as u8,
];
harmonized_colors.insert(name.to_string(), harmonize(argb, *source_color));
}

debug!("harmonized_colors: {:#?}", &harmonized_colors);
harmonized_colors
}

pub fn show_color(schemes: &Schemes, source_color: &[u8; 4]) {
let mut table: Table = generate_table_format();

Expand All @@ -36,7 +108,12 @@ pub fn show_color(schemes: &Schemes, source_color: &[u8; 4]) {
generate_table_rows(&mut table, field, color_light, color_dark);
}

generate_table_rows(&mut table, "source_color", rgb_from_argb(*source_color), rgb_from_argb(*source_color));
generate_table_rows(
&mut table,
"source_color",
rgb_from_argb(*source_color),
rgb_from_argb(*source_color),
);

table.printstd();
}
Expand All @@ -55,19 +132,19 @@ pub fn dump_json(schemes: &Schemes, source_color: &[u8; 4], format: &Format) {
)
},
F::Hsl => {
|c: Rgb| Hsl::from((c.red() as f64, c.green() as f64, c.blue() as f64)).to_css_string()
|c: Rgb| Hsl::from((c.red(), c.green(), c.blue())).to_css_string()
}
F::Hsla => |c: Rgb| {
Hsl::from((
c.red() as f64,
c.green() as f64,
c.blue() as f64,
c.alpha() as f64,
c.red(),
c.green(),
c.blue(),
c.alpha(),
))
.to_css_string()
},
F::Hex => |c: Rgb| c.to_hex_string(),
F::Strip => |c: Rgb| c.to_hex_string().replace("#", ""),
F::Strip => |c: Rgb| c.to_hex_string().replace('#', ""),
};

let mut colors_normal_light: HashMap<&str, String> = HashMap::new();
Expand Down Expand Up @@ -161,12 +238,13 @@ pub fn get_source_color(source: &Source) -> Result<[u8; 4], Report> {
Source::Image { path } => {
// test
info!("Opening image in <d><u>{}</>", path);
let img = ImageReader::open(path).expect("failed to open image")
.with_guessed_format()
.expect("failed to guess format")
.decode()
.expect("failed to decode image")
.into_rgba8();
let img = ImageReader::open(path)
.expect("failed to open image")
.with_guessed_format()
.expect("failed to guess format")
.decode()
.expect("failed to decode image")
.into_rgba8();
let img = resize(&img, 128, 128, FilterType::Gaussian);

source_color_from_image(img)?
Expand Down
9 changes: 2 additions & 7 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,8 @@ pub struct Config {
pub swww_options: Option<Vec<String>>,
pub feh_options: Option<Vec<String>>,
pub prefix: Option<String>,
pub custom_keywords: Option<HashMap<String, CustomKeyword>>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct CustomKeyword {
pub find: String,
pub replace: String,
pub custom_keywords: Option<HashMap<String, String>>,
pub colors_to_harmonize: Option<HashMap<String, String>>,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down
Loading

0 comments on commit 1b1ca86

Please sign in to comment.