Skip to content

Commit

Permalink
feat: add color comparsion (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Jun 25, 2024
1 parent 50f8dfd commit 0ff95ef
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 17 deletions.
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 17 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,31 @@ winapi = { version = "0.3", features = ["winuser"] }
# This is here because of #43
enquote = "1.1.0"

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 }
serde = { version = "1.0.160", features = ["derive"] }
toml = "0.8.8"
directories = "5.0"
resolve-path = "0.1.0"
pretty_env_logger = "0.5.0"
log = "0.4.17"
paris-log = { version = "1.0.2", features = ["icons"] }
colorsys = "0.6.7"
log = "0.4.17"
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 = { version = "0.3.2", features = ["image"] }

owo-colors = "4.0.0"
colorsys = "0.6.7"

resolve-path = "0.1.0"
directories = "5.0"
proper_path_tools = "0.7.0"

ahash = "0.8.7"
indexmap = "2.2.2"
proper_path_tools = "0.7.0"

image = "0.24.7"
upon = "0.8.0"
clap = { version = "4.2.4", features = ["derive"] }
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0.107"
toml = "0.8.8"
material-colors = { version = "0.3.2", features = ["image"] }
16 changes: 16 additions & 0 deletions example/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ test = "aaaa"
[templates.name1]
input_path = "example/colors.whatever-extension"
output_path = "example/a/colors-generated.whatever-extension"
colors_to_compare = [
{ name = "black", color = "#000000" },
{ name = "red", color = "#ff0000" },
{ name = "maroon", color = "#800000" },
{ name = "yellow", color = "#ffff00" },
{ name = "olive", color = "#808000" },
{ name = "lime", color = "#00ff00" },
{ name = "green", color = "#008000" },
{ name = "aqua", color = "#00ffff" },
{ name = "teal", color = "#008080" },
{ name = "blue", color = "#0000ff" },
{ name = "navy", color = "#000080" },
{ name = "fuchsia", color = "#ff00ff" },
{ name = "purple", color = "#800080" },
]
compare_to = "#aefbd5"

[config.custom_colors]
green = "#00FF00"
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
use std::{collections::HashMap, io::Write};
use update_informer::{registry, Check};

use util::color::{generate_dynamic_scheme, make_custom_color};
use util::color::{self, generate_dynamic_scheme, make_custom_color};

pub struct Schemes {
pub light: IndexMap<String, Argb, ahash::random_state::RandomState>,
Expand Down
48 changes: 48 additions & 0 deletions src/util/color.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use material_colors::color::Lab;
use material_colors::dynamic_color::dynamic_scheme::DynamicScheme;
use material_colors::dynamic_color::material_dynamic_colors::MaterialDynamicColors;
use material_colors::theme::{ColorGroup, CustomColor, CustomColorGroup};
Expand All @@ -18,6 +19,7 @@ use prettytable::{format, Cell, Row, Table};
use crate::Schemes;

use super::arguments::{ColorFormat, Format, SchemeTypes, Source};
use super::template::ColorDefinition;
use color_eyre::{eyre::Result, Report};
use colorsys::{ColorAlpha, Hsl, Rgb};
use serde_json::json;
Expand Down Expand Up @@ -81,6 +83,52 @@ pub fn format_hsla(color: &Hsl) -> String {
)
}

pub fn rgb_from_hex() {

}

pub fn get_color_distance_lab(c1: &str, c2: &str) -> f64 {
let c1 = Lab::from(Argb::from_str(c1).unwrap());
let c2 = Lab::from(Argb::from_str(c2).unwrap());

let l: f64 = c1.l - c2.l;
let a: f64 = c1.a - c2.a;
let b: f64 = c1.b - c2.b;

return f64::sqrt((l * l) + (a * a) + (b * b))
}

// for rgb - useless but ill keep it here

// pub fn get_color_distance(c1: &Rgb, c2: &Rgb) -> f64 {
// let (r1, g1, b1) = (c1.red() as i64, c1.blue() as i64, c1.green() as i64);
// let (r2, g2, b2) = (c2.red() as i64, c2.green() as i64, c2.blue() as i64);

// let rmean: f64 = ((r1 + r2) / 2) as f64;
// let weightR: f64 = 2.0 + rmean / 256.0;
// let weightG: f64 = 4.0;
// let weightB: f64 = 2.0 + (255.0 - rmean) / 256.0;

// return f64::sqrt(weightR * i64::pow(r1-r2, 2) as f64 + weightG * i64::pow(g1-g2, 2) as f64 + weightB * i64::pow(b1-b2, 2) as f64)
// }

pub fn color_to_string(colors_to_compare: &Vec<ColorDefinition>, compare_to: &String) {
let base_color = Rgb::from_hex_str(&compare_to).unwrap();

let mut closest_distance: Option<f64> = None;
let mut closest_color: &str = "";

for c in colors_to_compare {
let distance = get_color_distance_lab(&c.color, "#bed8f7");
if closest_distance.is_none() || closest_distance.unwrap() > distance {
closest_distance = Some(distance);
closest_color = &c.name;
}
debug!("distance: {}, name: {}", distance, c.name)
}
debug!("closest distance: {:?}, closest color: {}", closest_distance, closest_color)
}

pub fn generate_dynamic_scheme(
scheme_type: &Option<SchemeTypes>,
source_color: Argb,
Expand Down
16 changes: 15 additions & 1 deletion src/util/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};

use upon::Value;

use crate::util::color;
use crate::util::filters::set_lightness;

use std::str;
Expand All @@ -32,11 +33,20 @@ use crate::{Schemes, SchemesEnum};

use upon::{Engine, Syntax};

#[derive(Serialize, Deserialize, Debug)]
pub struct ColorDefinition {
pub name: String,
pub color: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Template {
pub input_path: PathBuf,
pub output_path: PathBuf,
pub mode: Option<SchemesEnum>,
pub hook: Option<String>,
pub colors_to_compare: Option<Vec<ColorDefinition>>,
pub compare_to: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -143,6 +153,10 @@ impl Template {
for (i, (name, template)) in templates.iter().enumerate() {
let input_path_absolute = template.input_path.try_resolve()?;
let output_path_absolute = template.output_path.try_resolve()?;

if template.colors_to_compare.is_some() && template.compare_to.is_some() {
color::color_to_string(&template.colors_to_compare.as_ref().unwrap(), &template.compare_to.as_ref().unwrap());
}

if !input_path_absolute.exists() {
warn!("<d>The <yellow><b>{}</><d> template in <u>{}</><d> doesnt exist, skipping...</>", name, input_path_absolute.display());
Expand Down Expand Up @@ -207,7 +221,7 @@ impl Template {
output_path_absolute.to_path_buf()
};

println!("{:?}", out);
debug!("out: {:?}", out);

let mut output_file = OpenOptions::new()
.create(true)
Expand Down

0 comments on commit 0ff95ef

Please sign in to comment.