Skip to content

Commit

Permalink
feat: add harmonized_colors to --json flag (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Mar 3, 2024
1 parent 59042ef commit 298adf9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 52 deletions.
56 changes: 20 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn main() -> Result<(), Report> {
}

if let Some(ref format) = args.json {
dump_json(&schemes, &source_color, format);
dump_json(&schemes, &source_color, format, &harmonized_colors);
}

if args.dry_run == Some(false) {
Expand All @@ -99,7 +99,7 @@ fn main() -> Result<(), Report> {
&source_color,
&default_scheme,
&config.config.custom_keywords,
harmonized_colors,
&harmonized_colors,
)?;

if config.config.reload_apps == Some(true) {
Expand Down Expand Up @@ -177,44 +177,28 @@ fn generate_scheme(
) -> Scheme {
match scheme_type.unwrap() {
SchemeTypes::SchemeContent => {
Scheme::from(
SchemeContent::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeExpressive => {
Scheme::from(
SchemeExpressive::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeFidelity => {
Scheme::from(
SchemeFidelity::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeFruitSalad => {
Scheme::from(
SchemeFruitSalad::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeMonochrome => {
Scheme::from(
SchemeMonochrome::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 => Scheme::from(
SchemeExpressive::new(Hct::new(source_color), is_dark, contrast_level).scheme,
),
SchemeTypes::SchemeFidelity => Scheme::from(
SchemeFidelity::new(Hct::new(source_color), is_dark, contrast_level).scheme,
),
SchemeTypes::SchemeFruitSalad => Scheme::from(
SchemeFruitSalad::new(Hct::new(source_color), is_dark, contrast_level).scheme,
),
SchemeTypes::SchemeMonochrome => Scheme::from(
SchemeMonochrome::new(Hct::new(source_color), is_dark, contrast_level).scheme,
),
SchemeTypes::SchemeNeutral => {
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 => {
Scheme::from(
SchemeRainbow::new(Hct::new(source_color), is_dark, contrast_level).scheme,
)
}
SchemeTypes::SchemeTonalSpot => {
Scheme::from(
SchemeTonalSpot::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 => Scheme::from(
SchemeTonalSpot::new(Hct::new(source_color), is_dark, contrast_level).scheme,
),
}
}
22 changes: 9 additions & 13 deletions src/util/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ pub fn show_color(schemes: &Schemes, source_color: &[u8; 4]) {
table.printstd();
}

pub fn dump_json(schemes: &Schemes, source_color: &[u8; 4], format: &Format) {
pub fn dump_json(
schemes: &Schemes,
source_color: &[u8; 4],
format: &Format,
harmonized_colors: &Option<HashMap<String, [u8; 4]>>,
) {
type F = Format;
let fmt = match format {
F::Rgb => |c: Rgb| format!("rgb({:?}, {:?}, {:?})", c.red(), c.green(), c.blue()),
Expand All @@ -131,18 +136,8 @@ pub fn dump_json(schemes: &Schemes, source_color: &[u8; 4], format: &Format) {
c.alpha()
)
},
F::Hsl => {
|c: Rgb| Hsl::from((c.red(), c.green(), c.blue())).to_css_string()
}
F::Hsla => |c: Rgb| {
Hsl::from((
c.red(),
c.green(),
c.blue(),
c.alpha(),
))
.to_css_string()
},
F::Hsl => |c: Rgb| Hsl::from((c.red(), c.green(), c.blue())).to_css_string(),
F::Hsla => |c: Rgb| Hsl::from((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('#', ""),
};
Expand All @@ -167,6 +162,7 @@ pub fn dump_json(schemes: &Schemes, source_color: &[u8; 4], format: &Format) {
"light": colors_normal_light,
"dark": colors_normal_dark,
},
"harmonized_colors": harmonized_colors,
})
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/util/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Template {
source_color: &[u8; 4],
default_scheme: &SchemesEnum,
custom_keywords: &Option<HashMap<String, String>>,
harmonized_colors: Option<HashMap<String, [u8; 4]>>,
harmonized_colors: &Option<HashMap<String, [u8; 4]>>,
) -> Result<(), Report> {
let default_prefix = "@".to_string();

Expand Down Expand Up @@ -275,12 +275,12 @@ fn generate_colors(

fn generate_harmonized_colors(
_source_color: &[u8; 4],
harmonized_colors: Option<HashMap<String, [u8; 4]>>,
harmonized_colors: &Option<HashMap<String, [u8; 4]>>,
) -> Result<Option<HashMap<String, Colora>>, Report> {
if let Some(colors) = harmonized_colors {
let mut map: HashMap<String, Colora> = Default::default();
for (name, color) in colors {
map.insert(name, generate_color_strings(color));
map.insert(name.to_string(), generate_color_strings(*color));
}
Ok(Some(map))
} else {
Expand Down

0 comments on commit 298adf9

Please sign in to comment.