Skip to content

Commit

Permalink
feat: add palettes to dump_json
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Nov 30, 2024
1 parent 86ccf49 commit 3a5e27b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
8 changes: 8 additions & 0 deletions src/color/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ pub fn rgb_from_argb(color: Argb) -> Rgb {
])
}

pub fn hsl_from_argb(color: Argb) -> Hsl {
rgb_from_argb(color).as_ref().into()
}

pub fn hsl_from_rgb(color: Rgb) -> Hsl {
color.as_ref().into()
}

pub fn format_hex(color: &Rgb) -> String {
color.to_hex_string()
}
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern crate pretty_env_logger;
#[macro_use]
extern crate paris_log;
use material_colors::theme::ThemeBuilder;

mod helpers;
pub mod template;
Expand Down Expand Up @@ -37,7 +38,10 @@ fn main() -> Result<(), Report> {
check_version();

Check warning on line 38 in src/main.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/main.rs

Check warning on line 38 in src/main.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/main.rs

Check warning on line 38 in src/main.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/main.rs
}


let source_color = get_source_color(&args.source).unwrap();

let theme = ThemeBuilder::with_source(source_color).build();

let (scheme_dark, scheme_light) = get_schemes(source_color, &args.r#type, &args.contrast);

Expand All @@ -61,7 +65,7 @@ fn main() -> Result<(), Report> {
#[cfg(feature = "dump-json")]
if let Some(ref format) = args.json {
use crate::util::color::dump_json;
dump_json(&schemes, &source_color, format);
dump_json(&schemes, &source_color, format, theme.palettes);
}

if args.dry_run == Some(false) {
Expand Down
96 changes: 70 additions & 26 deletions src/util/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use material_colors::color::Argb;
use owo_colors::{OwoColorize, Style};

use prettytable::{format, Cell, Row, Table};
use material_colors::theme::Palettes;
use material_colors::palette::TonalPalette;

use colorsys::Rgb;

use crate::Schemes;

#[cfg(feature = "dump-json")]
use super::arguments::Format;

use colorsys::Rgb;
use matugen::color::format::rgb_from_argb;

pub fn show_color(schemes: &Schemes, source_color: &Argb) {
Expand All @@ -32,53 +35,94 @@ pub fn show_color(schemes: &Schemes, source_color: &Argb) {
}

#[cfg(feature = "dump-json")]
pub fn dump_json(schemes: &Schemes, source_color: &Argb, format: &Format) {
use colorsys::{ColorAlpha, Hsl};
use serde_json::json;
pub fn dump_json(schemes: &Schemes, source_color: &Argb, format: &Format, palettes: Palettes) {
use std::collections::HashMap;

type F = Format;
let fmt = match format {
F::Rgb => |c: Rgb| format!("rgb({:?}, {:?}, {:?})", c.red(), c.green(), c.blue()),
F::Rgba => |c: Rgb| {
format!(
"rgba({:?}, {:?}, {:?}, {:?})",
c.red(),
c.green(),
c.blue(),
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::Hex => |c: Rgb| c.to_hex_string(),
F::Strip => |c: Rgb| c.to_hex_string().replace('#', ""),
};

let mut colors_normal_light: HashMap<&str, String> = HashMap::new();
let mut colors_normal_dark: HashMap<&str, String> = HashMap::new();

for ((field, color_light), (_, color_dark)) in std::iter::zip(&schemes.light, &schemes.dark) {
let color_light: Rgb = rgb_from_argb(*color_light);
let color_dark: Rgb = rgb_from_argb(*color_dark);

colors_normal_light.insert(field, fmt(color_light));
colors_normal_dark.insert(field, fmt(color_dark));
colors_normal_light.insert(field, format_single_color(color_light, format));
colors_normal_dark.insert(field, format_single_color(color_dark, format));

Check warning on line 49 in src/util/color.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/util/color.rs

Check warning on line 49 in src/util/color.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/util/color.rs

Check warning on line 49 in src/util/color.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/util/color.rs
}

colors_normal_light.insert("source_color", fmt(rgb_from_argb(*source_color)));
colors_normal_light.insert("source_color", format_single_color(rgb_from_argb(*source_color), format));

println!(
"{}",
json!({
serde_json::json!({
"colors": {
"light": colors_normal_light,
"dark": colors_normal_dark,
},
"palettes": format_palettes(palettes, format),
})
);
}

#[cfg(feature = "dump-json")]
fn format_palettes(palettes: Palettes, format: &Format) -> serde_json::Value {

Check warning on line 67 in src/util/color.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/util/color.rs

Check warning on line 67 in src/util/color.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/util/color.rs

Check warning on line 67 in src/util/color.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/util/color.rs
let primary = format_single_palette(palettes.primary, format);
let secondary = format_single_palette(palettes.secondary, format);
let tertiary = format_single_palette(palettes.tertiary, format);
let neutral = format_single_palette(palettes.neutral, format);
let neutral_variant = format_single_palette(palettes.neutral_variant, format);
let error = format_single_palette(palettes.error, format);
serde_json::json!({
"primary": primary,
"secondary": secondary,
"tertiary": tertiary,
"neutral": neutral,
"neutral_variant": neutral_variant,
"error": error,
})
}

#[cfg(feature = "dump-json")]
fn format_single_palette(palette: TonalPalette, format: &Format) -> serde_json::Value {
serde_json::json!({
"0": format_single_color(rgb_from_argb(palette.tone(0)), format),
"5": format_single_color(rgb_from_argb(palette.tone(5)), format),
"10": format_single_color(rgb_from_argb(palette.tone(10)), format),
"15": format_single_color(rgb_from_argb(palette.tone(15)), format),
"20": format_single_color(rgb_from_argb(palette.tone(20)), format),
"25": format_single_color(rgb_from_argb(palette.tone(25)), format),
"30": format_single_color(rgb_from_argb(palette.tone(30)), format),
"35": format_single_color(rgb_from_argb(palette.tone(35)), format),
"40": format_single_color(rgb_from_argb(palette.tone(40)), format),
"45": format_single_color(rgb_from_argb(palette.tone(45)), format),
"50": format_single_color(rgb_from_argb(palette.tone(50)), format),
"55": format_single_color(rgb_from_argb(palette.tone(55)), format),
"60": format_single_color(rgb_from_argb(palette.tone(60)), format),
"65": format_single_color(rgb_from_argb(palette.tone(65)), format),
"70": format_single_color(rgb_from_argb(palette.tone(70)), format),
"75": format_single_color(rgb_from_argb(palette.tone(75)), format),
"80": format_single_color(rgb_from_argb(palette.tone(80)), format),
"85": format_single_color(rgb_from_argb(palette.tone(85)), format),
"90": format_single_color(rgb_from_argb(palette.tone(90)), format),
"95": format_single_color(rgb_from_argb(palette.tone(95)), format),
"100": format_single_color(rgb_from_argb(palette.tone(100)), format),
})
}

Check warning on line 110 in src/util/color.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/util/color.rs

Check warning on line 110 in src/util/color.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/util/color.rs

Check warning on line 110 in src/util/color.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/src/util/color.rs
#[cfg(feature = "dump-json")]
fn format_single_color(color: Rgb, format: &Format) -> String {
use matugen::color::format::{format_hex, format_hex_stripped, format_hsl, format_hsla, format_rgb, format_rgba, hsl_from_rgb};

let fmt = match format {
Format::Rgb => |c: Rgb| format_rgb(&c),
Format::Rgba => |c: Rgb| format_rgba(&c, true),
Format::Hsl => |c: Rgb| format_hsl(&hsl_from_rgb(c)),
Format::Hsla => |c: Rgb| format_hsla(&hsl_from_rgb(c), true),
Format::Hex => |c: Rgb| format_hex(&c),
Format::Strip => |c: Rgb| format_hex_stripped(&c),
};
fmt(color)
}

fn generate_table_format() -> Table {
let mut table = Table::new();
let format = format::FormatBuilder::new()
Expand Down

0 comments on commit 3a5e27b

Please sign in to comment.