Skip to content

Commit

Permalink
Merge pull request #66 from Theaninova/update-material-colors
Browse files Browse the repository at this point in the history
update material-colors
  • Loading branch information
InioX authored May 2, 2024
2 parents c0b0f44 + 146553b commit e795cda
Show file tree
Hide file tree
Showing 8 changed files with 494 additions and 114 deletions.
449 changes: 431 additions & 18 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ authors = ["InioX"]
description = "A material you color generation tool with templates"
repository = "https://github.com/InioX/matugen"
categories = ["command-line-utilities"]
exclude = [
"default.nix",
"flake.nix",
"shell.nix",
"example/*",
]
exclude = ["default.nix", "flake.nix", "shell.nix", "example/*"]
license = "GPL-2.0"
edition = "2021"

Expand All @@ -23,7 +18,7 @@ winapi = { version = "0.3", features = ["winuser"] }
# This is here because of #43
enquote = "1.1.0"

image = "0.24.6"
image = "0.25.0"
owo-colors = "4.0.0"
clap = { version = "4.2.4", features = ["derive"] }
color-eyre = { version = "0.6.2", default-features = false }
Expand All @@ -39,7 +34,10 @@ prettytable-rs = "0.10.0"
serde_json = "1.0.107"
update-informer = "1.1.0"
upon = "0.8.0"
reqwest = { version = "0.11.23", default_features=false, features = ["blocking", "rustls-tls"] }
material-colors = "0.1.4"
reqwest = { version = "0.11.23", default_features = false, features = [
"blocking",
"rustls-tls",
] }
material-colors = { version = "0.2.1", features = ["image"] }
ahash = "0.8.7"
indexmap = "2.2.2"
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::util::{

use indexmap::IndexMap;

use material_colors::Scheme;
use material_colors::{Argb, Scheme};

use clap::{Parser, ValueEnum};
use color_eyre::{eyre::Result, Report};
Expand All @@ -27,8 +27,8 @@ use update_informer::{registry, Check};
use util::color::{generate_dynamic_scheme, make_custom_color};

pub struct Schemes {
pub light: IndexMap<String, [u8; 4], ahash::random_state::RandomState>,
pub dark: IndexMap<String, [u8; 4], ahash::random_state::RandomState>,
pub light: IndexMap<String, Argb, ahash::random_state::RandomState>,
pub dark: IndexMap<String, Argb, ahash::random_state::RandomState>,
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
Expand Down
93 changes: 42 additions & 51 deletions src/util/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ use material_colors::dynamic_color::dynamic_scheme::DynamicScheme;
use material_colors::dynamic_color::material_dynamic_colors::MaterialDynamicColors;
use material_colors::utils::theme::{ColorGroup, CustomColor, CustomColorGroup};
use material_colors::{
Hct, SchemeContent, SchemeExpressive, SchemeFidelity, SchemeFruitSalad, SchemeMonochrome,
SchemeNeutral, SchemeRainbow, SchemeTonalSpot,
Argb, FilterType, Hct, ImageReader, SchemeContent, SchemeExpressive, SchemeFidelity,
SchemeFruitSalad, SchemeMonochrome, SchemeNeutral, SchemeRainbow, SchemeTonalSpot,
};
use owo_colors::{OwoColorize, Style};

use prettytable::{format, Cell, Row, Table};

use crate::Schemes;

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

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

use super::arguments::{ColorFormat, Format, SchemeTypes, Source};
use super::image::source_color_from_image;
use color_eyre::{eyre::Result, Report};
use colorsys::{ColorAlpha, Hsl, Rgb};
use serde_json::json;
Expand All @@ -26,12 +20,12 @@ use std::str::FromStr;

use material_colors::blend::harmonize;

pub fn rgb_from_argb(color: [u8; 4]) -> Rgb {
pub fn rgb_from_argb(color: Argb) -> Rgb {
Rgb::from([
color[1] as f64,
color[2] as f64,
color[3] as f64,
color[0] as f64,
color.red as f64,
color.green as f64,
color.blue as f64,
color.alpha as f64,
])
}

Expand Down Expand Up @@ -83,7 +77,7 @@ pub fn format_hsla(color: &Hsl) -> String {

pub fn generate_dynamic_scheme(
scheme_type: &Option<SchemeTypes>,
source_color: [u8; 4],
source_color: Argb,
is_dark: bool,
contrast_level: Option<f64>,
) -> DynamicScheme {
Expand Down Expand Up @@ -118,7 +112,7 @@ pub fn generate_dynamic_scheme(
pub fn make_custom_color(
color: CustomColor,
scheme_type: &Option<SchemeTypes>,
source_color: [u8; 4],
source_color: Argb,
contrast_level: Option<f64>,
) -> CustomColorGroup {
debug!("make_custom_color: {:#?}", &color);
Expand Down Expand Up @@ -153,7 +147,7 @@ pub fn make_custom_color(
custom_color
}

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

for (field, _color) in &schemes.dark {
Expand All @@ -173,7 +167,7 @@ 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: &Argb, format: &Format) {
type F = Format;
let fmt = match format {
F::Rgb => |c: Rgb| format!("rgb({:?}, {:?}, {:?})", c.red(), c.green(), c.blue()),
Expand Down Expand Up @@ -278,50 +272,47 @@ fn generate_style(color: &Rgb) -> Style {
}
}

pub fn get_source_color(source: &Source) -> Result<[u8; 4], Report> {
let source_color: [u8; 4] = match &source {
pub fn get_source_color(source: &Source) -> Result<Argb, Report> {
let source_color: Argb = match &source {
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 = resize(&img, 128, 128, FilterType::Gaussian);

source_color_from_image(img)?
ImageReader::extract_color(ImageReader::open(path)?.resize(
128,
128,
FilterType::Gaussian,
))
}
Source::WebImage { url } => {
// test
info!("Fetching image from <d><u>{}</>", url);

let img = fetch_image(url)?.into_rgba8();
let img = resize(&img, 128, 128, FilterType::Gaussian);

source_color_from_image(img)?
let bytes = reqwest::blocking::get(url)?.bytes()?;
ImageReader::extract_color(ImageReader::read(&bytes)?.resize(
128,
128,
FilterType::Gaussian,
))
}
Source::Color(color) => {
let src: Rgb = match color {
ColorFormat::Hex { string } => {
Rgb::from_hex_str(string).expect("Invalid hex color string provided")
}
ColorFormat::Rgb { string } => {
string.parse().expect("Invalid rgb color string provided")
}
ColorFormat::Hsl { string } => Hsl::from_str(string)
Source::Color(color) => match color {
ColorFormat::Hex { string } => {
Argb::from_str(string).expect("Invalid hex color string provided")
}
ColorFormat::Rgb { string } => {
string.parse().expect("Invalid rgb color string provided")
}
ColorFormat::Hsl { string } => {
let rgb: Rgb = Hsl::from_str(string)
.expect("Invalid hsl color string provided")
.into(),
};
[
src.alpha() as u8,
src.red() as u8,
src.green() as u8,
src.blue() as u8,
]
}
.into();
Argb {
red: rgb.red() as u8,
green: rgb.green() as u8,
blue: rgb.blue() as u8,
alpha: 255,
}
}
},
};
Ok(source_color)
}
11 changes: 6 additions & 5 deletions src/util/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use directories::ProjectDirs;
use material_colors::{argb_from_hex, utils::string::ParseError};
use std::collections::HashMap;
use material_colors::utils::color::ParseRgbError;
use material_colors::Argb;
use std::fs;
use std::path::PathBuf;
use std::{collections::HashMap, str::FromStr};

use color_eyre::{Help, Report};

Expand Down Expand Up @@ -30,15 +31,15 @@ impl CustomColor {
pub fn to_custom_color(
&self,
name: String,
) -> Result<material_colors::utils::theme::CustomColor, ParseError> {
) -> Result<material_colors::utils::theme::CustomColor, ParseRgbError> {
Ok(match self {
CustomColor::Color(color) => material_colors::utils::theme::CustomColor {
value: argb_from_hex(color)?,
value: Argb::from_str(color)?,
blend: true,
name,
},
CustomColor::Options { color, blend } => material_colors::utils::theme::CustomColor {
value: argb_from_hex(color)?,
value: Argb::from_str(color)?,
blend: *blend,
name,
},
Expand Down
23 changes: 0 additions & 23 deletions src/util/image.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ pub mod arguments;
pub mod color;
pub mod config;
pub mod filters;
pub mod image;
pub mod template;
9 changes: 5 additions & 4 deletions src/util/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use color_eyre::Help;
use color_eyre::{eyre::Result, Report};

use colorsys::{ColorAlpha, Hsl};
use material_colors::Argb;
use serde::{Deserialize, Serialize};

use upon::Value;
Expand Down Expand Up @@ -92,7 +93,7 @@ impl Template {
templates: &HashMap<String, Template>,
source: &Source,
prefix: &Option<String>,
source_color: &[u8; 4],
source_color: &Argb,
default_scheme: &SchemesEnum,
custom_keywords: &Option<HashMap<String, String>>,
) -> Result<(), Report> {
Expand Down Expand Up @@ -225,7 +226,7 @@ impl Template {

fn generate_colors(
schemes: &Schemes,
source_color: &[u8; 4],
source_color: &Argb,
default_scheme: &SchemesEnum,
) -> Result<HashMap<String, ColorVariants>, Report> {
let mut hashmap: HashMap<String, ColorVariants> = Default::default();
Expand All @@ -245,7 +246,7 @@ fn generate_colors(
fn generate_single_color(
field: &str,
schemes: &Schemes,
source_color: &[u8; 4],
source_color: &Argb,
default_scheme: &SchemesEnum,
) -> Result<ColorVariants, Report> {
let scheme = match default_scheme {
Expand All @@ -268,7 +269,7 @@ fn generate_single_color(
})
}

fn generate_color_strings(color: [u8; 4]) -> Colora {
fn generate_color_strings(color: Argb) -> Colora {
let base_color = rgb_from_argb(color);
let hsl_color = Hsl::from(&base_color);
Colora {
Expand Down

0 comments on commit e795cda

Please sign in to comment.