diff --git a/src/color/format.rs b/src/color/format.rs index 84eef73..b3b542b 100644 --- a/src/color/format.rs +++ b/src/color/format.rs @@ -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() } diff --git a/src/main.rs b/src/main.rs index dbee85d..9307376 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -37,7 +38,10 @@ fn main() -> Result<(), Report> { check_version(); } + 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); @@ -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) { diff --git a/src/util/color.rs b/src/util/color.rs index 0d57c38..9a1878b 100644 --- a/src/util/color.rs +++ b/src/util/color.rs @@ -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) { @@ -32,29 +35,9 @@ 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(); @@ -62,23 +45,84 @@ pub fn dump_json(schemes: &Schemes, source_color: &Argb, format: &Format) { 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)); } - 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 { + 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), + }) +} + +#[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()