Skip to content

Commit 1b1ca86

Browse files
authored
Merge pull request #56 from InioX/dev
Dev
2 parents 0b0bed0 + 59042ef commit 1b1ca86

File tree

9 files changed

+301
-113
lines changed

9 files changed

+301
-113
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ upon = "0.8.0"
4141
reqwest = { version = "0.11.23", default_features=false, features = ["blocking", "rustls-tls"] }
4242
material-colors = "0.1.4"
4343
ahash = "0.8.7"
44-
indexmap = "2.2.2"
44+
indexmap = "2.2.2"

example/colors.whatever-extension

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,25 @@
1+
red #FF0000;
2+
green #00FF00;
3+
blue #0000FF;
4+
5+
test {{ colors.source_color.default.rgb | set_lightness: 20.0 }}
6+
test {{ colors.source_color.default.rgb | set_lightness: -20.0 }}
7+
test {{ colors.source_color.default.rgb }}
8+
9+
{{ colors.source_color.default.hex_stripped | set_lightness: 20.0 }}
10+
11+
{{ colors.source_color.default.hex | set_lightness: 20.0 }}
12+
13+
Should not change at all
14+
{{ colors.source_color.default.red | set_lightness: 5.0 }}
15+
16+
red {{harmonized_colors.red.rgb}};
17+
red {{harmonized_colors.red.rgba}};
18+
green {{harmonized_colors.green.hsl}};
19+
blue {{harmonized_colors.blue.hsla}};
20+
121
source_color {{colors.source_color.default.hex}};
222

3-
primary {{colors.primary.default.hex}};
4-
on_primary {{colors.on_primary.default.hex}};
5-
primary_container {{colors.primary_container.default.hex}};
6-
on_primary_container {{colors.on_primary_container.default.hex}};
7-
inverse_primary {{colors.inverse_primary.default.hex}};
8-
primary_fixed {{colors.primary_fixed.default.hex}};
9-
primary_fixed_dim {{colors.primary_fixed_dim.default.hex}};
10-
on_primary_fixed {{colors.on_primary_fixed.default.hex}};
11-
on_primary_fixed_variant {{colors.on_primary_fixed_variant.default.hex}};
12-
secondary {{colors.secondary.default.hex}};
13-
on_secondary {{colors.on_secondary.default.hex}};
14-
secondary_container {{colors.secondary_container.default.hex}};
15-
on_secondary_container {{colors.on_secondary_container.default.hex}};
16-
secondary_fixed {{colors.secondary_fixed.default.hex}};
17-
secondary_fixed_dim {{colors.secondary_fixed_dim.default.hex}};
18-
on_secondary_fixed {{colors.on_secondary_fixed.default.hex}};
19-
on_secondary_fixed_variant {{colors.on_secondary_fixed_variant.default.hex}};
20-
tertiary {{colors.tertiary.default.hex}};
21-
on_tertiary {{colors.on_tertiary.default.hex}};
22-
tertiary_container {{colors.tertiary_container.default.hex}};
23-
on_tertiary_container {{colors.on_tertiary_container.default.hex}};
24-
tertiary_fixed {{colors.tertiary_fixed.default.hex}};
25-
tertiary_fixed_dim {{colors.tertiary_fixed_dim.default.hex}};
26-
on_tertiary_fixed {{colors.on_tertiary_fixed.default.hex}};
27-
on_tertiary_fixed_variant {{colors.on_tertiary_fixed_variant.default.hex}};
28-
error {{colors.error.default.hex}};
29-
on_error {{colors.on_error.default.hex}};
30-
error_container {{colors.error_container.default.hex}};
31-
on_error_container {{colors.on_error_container.default.hex}};
32-
surface_dim {{colors.surface_dim.default.hex}};
33-
surface {{colors.surface.default.hex}};
34-
surface_bright {{colors.surface_bright.default.hex}};
35-
surface_container_lowest {{colors.surface_container_lowest.default.hex}};
36-
surface_container_low {{colors.surface_container_low.default.hex}};
37-
surface_container {{colors.surface_container.default.hex}};
38-
surface_container_high {{colors.surface_container_high.default.hex}};
39-
surface_container_highest {{colors.surface_container_highest.default.hex}};
40-
on_surface {{colors.on_surface.default.hex}};
41-
on_surface_variant {{colors.on_surface_variant.default.hex}};
42-
outline {{colors.outline.default.hex}};
43-
outline_variant {{colors.outline_variant.default.hex}};
44-
inverse_surface {{colors.inverse_surface.default.hex}};
45-
inverse_on_surface {{colors.inverse_on_surface.default.hex}};
46-
surface_variant {{colors.surface_variant.default.hex}};
47-
background {{colors.background.default.hex}};
48-
on_background {{colors.on_background.default.hex}};
49-
shadow {{colors.shadow.default.hex}};
50-
scrim {{colors.scrim.default.hex}};
23+
<* for name, value in colors *>
24+
{{name | replace: "_", "-" }} {{value.default.hex}};
25+
<* endfor *>

example/config.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ swww_options = [
77
"center",
88
]
99

10+
[config.custom_keywords]
11+
test = "aaaa"
12+
1013
[templates.name1]
1114
input_path = "example/colors.whatever-extension"
12-
output_path = "example/a/colors-generated.whatever-extension"
15+
output_path = "example/a/colors-generated.whatever-extension"
16+
17+
[config.colors_to_harmonize]
18+
green = "#00FF00"
19+
red = "#FF0000"
20+
blue = "#0000FF"

src/main.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
2424
use std::io::Write;
2525
use update_informer::{registry, Check};
2626

27-
use util::arguments::SchemeTypes;
27+
use util::{arguments::SchemeTypes, color::harmonize_colors};
2828

2929
use material_colors::{
3030
SchemeContent, SchemeExpressive, SchemeFidelity, SchemeFruitSalad, SchemeMonochrome,
@@ -76,6 +76,12 @@ fn main() -> Result<(), Report> {
7676
light: IndexMap::from_iter(scheme_light),
7777
};
7878

79+
let mut harmonized_colors = None;
80+
81+
if let Some(colors) = &config.config.colors_to_harmonize {
82+
harmonized_colors = Some(harmonize_colors(&source_color, colors));
83+
};
84+
7985
if args.show_colors == Some(true) {
8086
show_color(&schemes, &source_color);
8187
}
@@ -93,6 +99,7 @@ fn main() -> Result<(), Report> {
9399
&source_color,
94100
&default_scheme,
95101
&config.config.custom_keywords,
102+
harmonized_colors,
96103
)?;
97104

98105
if config.config.reload_apps == Some(true) {
@@ -170,34 +177,44 @@ fn generate_scheme(
170177
) -> Scheme {
171178
match scheme_type.unwrap() {
172179
SchemeTypes::SchemeContent => {
173-
return Scheme::from(SchemeContent::new(Hct::new(source_color), is_dark, contrast_level).scheme)
180+
Scheme::from(
181+
SchemeContent::new(Hct::new(source_color), is_dark, contrast_level).scheme,
182+
)
174183
}
175184
SchemeTypes::SchemeExpressive => {
176-
return Scheme::from(
185+
Scheme::from(
177186
SchemeExpressive::new(Hct::new(source_color), is_dark, contrast_level).scheme,
178187
)
179188
}
180189
SchemeTypes::SchemeFidelity => {
181-
return Scheme::from(SchemeFidelity::new(Hct::new(source_color), is_dark, contrast_level).scheme)
190+
Scheme::from(
191+
SchemeFidelity::new(Hct::new(source_color), is_dark, contrast_level).scheme,
192+
)
182193
}
183194
SchemeTypes::SchemeFruitSalad => {
184-
return Scheme::from(
195+
Scheme::from(
185196
SchemeFruitSalad::new(Hct::new(source_color), is_dark, contrast_level).scheme,
186197
)
187198
}
188199
SchemeTypes::SchemeMonochrome => {
189-
return Scheme::from(
200+
Scheme::from(
190201
SchemeMonochrome::new(Hct::new(source_color), is_dark, contrast_level).scheme,
191202
)
192203
}
193204
SchemeTypes::SchemeNeutral => {
194-
return Scheme::from(SchemeNeutral::new(Hct::new(source_color), is_dark, contrast_level).scheme)
205+
Scheme::from(
206+
SchemeNeutral::new(Hct::new(source_color), is_dark, contrast_level).scheme,
207+
)
195208
}
196209
SchemeTypes::SchemeRainbow => {
197-
return Scheme::from(SchemeRainbow::new(Hct::new(source_color), is_dark, contrast_level).scheme)
210+
Scheme::from(
211+
SchemeRainbow::new(Hct::new(source_color), is_dark, contrast_level).scheme,
212+
)
198213
}
199214
SchemeTypes::SchemeTonalSpot => {
200-
return Scheme::from(SchemeTonalSpot::new(Hct::new(source_color), is_dark, contrast_level).scheme)
215+
Scheme::from(
216+
SchemeTonalSpot::new(Hct::new(source_color), is_dark, contrast_level).scheme,
217+
)
201218
}
202219
}
203220
}

src/util/color.rs

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::Schemes;
66

77
use crate::util::image::fetch_image;
88

9-
use image::io::Reader as ImageReader;
109
use image::imageops::{resize, FilterType};
10+
use image::io::Reader as ImageReader;
1111

1212
use super::arguments::{ColorFormat, Format, Source};
1313
use super::image::source_color_from_image;
@@ -17,6 +17,8 @@ use serde_json::json;
1717
use std::collections::HashMap;
1818
use std::str::FromStr;
1919

20+
use material_colors::blend::harmonize;
21+
2022
pub fn rgb_from_argb(color: [u8; 4]) -> Rgb {
2123
Rgb::from([
2224
color[1] as f64,
@@ -26,6 +28,76 @@ pub fn rgb_from_argb(color: [u8; 4]) -> Rgb {
2628
])
2729
}
2830

31+
pub fn format_hex(color: &Rgb) -> String {
32+
color.to_hex_string()
33+
}
34+
35+
pub fn format_hex_stripped(color: &Rgb) -> String {
36+
color.to_hex_string()[1..].to_string()
37+
}
38+
39+
pub fn format_rgb(color: &Rgb) -> String {
40+
format!(
41+
"rgb({:?}, {:?}, {:?})",
42+
color.red() as u8,
43+
color.green() as u8,
44+
color.blue() as u8,
45+
)
46+
}
47+
48+
pub fn format_rgba(color: &Rgb) -> String {
49+
format!(
50+
"rgba({:?}, {:?}, {:?}, {:?})",
51+
color.red() as u8,
52+
color.green() as u8,
53+
color.blue() as u8,
54+
color.alpha() as u8
55+
)
56+
}
57+
58+
pub fn format_hsl(color: &Hsl) -> String {
59+
format!(
60+
"hsl({:?}, {:?}%, {:?}%)",
61+
color.hue() as u8,
62+
color.saturation() as u8,
63+
color.lightness() as u8,
64+
)
65+
}
66+
67+
pub fn format_hsla(color: &Hsl) -> String {
68+
format!(
69+
"hsla({:?}, {:?}%, {:?}%, {:?})",
70+
color.hue() as u8,
71+
color.saturation() as u8,
72+
color.lightness() as u8,
73+
color.alpha() as u8
74+
)
75+
}
76+
77+
pub fn harmonize_colors(
78+
source_color: &[u8; 4],
79+
colors: &HashMap<String, String>,
80+
) -> HashMap<String, [u8; 4]> {
81+
debug!("colors_to_harmonize: {:#?}", &colors);
82+
let mut harmonized_colors: HashMap<String, [u8; 4]> = HashMap::default();
83+
84+
for (name, color) in colors {
85+
let rgb = Rgb::from_hex_str(color)
86+
.expect("Invalid hex color string provided for `harmonized_colors`");
87+
88+
let argb: [u8; 4] = [
89+
rgb.alpha() as u8,
90+
rgb.red() as u8,
91+
rgb.green() as u8,
92+
rgb.blue() as u8,
93+
];
94+
harmonized_colors.insert(name.to_string(), harmonize(argb, *source_color));
95+
}
96+
97+
debug!("harmonized_colors: {:#?}", &harmonized_colors);
98+
harmonized_colors
99+
}
100+
29101
pub fn show_color(schemes: &Schemes, source_color: &[u8; 4]) {
30102
let mut table: Table = generate_table_format();
31103

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

39-
generate_table_rows(&mut table, "source_color", rgb_from_argb(*source_color), rgb_from_argb(*source_color));
111+
generate_table_rows(
112+
&mut table,
113+
"source_color",
114+
rgb_from_argb(*source_color),
115+
rgb_from_argb(*source_color),
116+
);
40117

41118
table.printstd();
42119
}
@@ -55,19 +132,19 @@ pub fn dump_json(schemes: &Schemes, source_color: &[u8; 4], format: &Format) {
55132
)
56133
},
57134
F::Hsl => {
58-
|c: Rgb| Hsl::from((c.red() as f64, c.green() as f64, c.blue() as f64)).to_css_string()
135+
|c: Rgb| Hsl::from((c.red(), c.green(), c.blue())).to_css_string()
59136
}
60137
F::Hsla => |c: Rgb| {
61138
Hsl::from((
62-
c.red() as f64,
63-
c.green() as f64,
64-
c.blue() as f64,
65-
c.alpha() as f64,
139+
c.red(),
140+
c.green(),
141+
c.blue(),
142+
c.alpha(),
66143
))
67144
.to_css_string()
68145
},
69146
F::Hex => |c: Rgb| c.to_hex_string(),
70-
F::Strip => |c: Rgb| c.to_hex_string().replace("#", ""),
147+
F::Strip => |c: Rgb| c.to_hex_string().replace('#', ""),
71148
};
72149

73150
let mut colors_normal_light: HashMap<&str, String> = HashMap::new();
@@ -161,12 +238,13 @@ pub fn get_source_color(source: &Source) -> Result<[u8; 4], Report> {
161238
Source::Image { path } => {
162239
// test
163240
info!("Opening image in <d><u>{}</>", path);
164-
let img = ImageReader::open(path).expect("failed to open image")
165-
.with_guessed_format()
166-
.expect("failed to guess format")
167-
.decode()
168-
.expect("failed to decode image")
169-
.into_rgba8();
241+
let img = ImageReader::open(path)
242+
.expect("failed to open image")
243+
.with_guessed_format()
244+
.expect("failed to guess format")
245+
.decode()
246+
.expect("failed to decode image")
247+
.into_rgba8();
170248
let img = resize(&img, 128, 128, FilterType::Gaussian);
171249

172250
source_color_from_image(img)?

src/util/config.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@ pub struct Config {
2828
pub swww_options: Option<Vec<String>>,
2929
pub feh_options: Option<Vec<String>>,
3030
pub prefix: Option<String>,
31-
pub custom_keywords: Option<HashMap<String, CustomKeyword>>,
32-
}
33-
34-
#[derive(Deserialize, Serialize, Debug)]
35-
pub struct CustomKeyword {
36-
pub find: String,
37-
pub replace: String,
31+
pub custom_keywords: Option<HashMap<String, String>>,
32+
pub colors_to_harmonize: Option<HashMap<String, String>>,
3833
}
3934

4035
#[derive(Deserialize, Serialize, Debug)]

0 commit comments

Comments
 (0)