Skip to content

Commit

Permalink
Merge pull request #103 from DaniD3v/compilation-performance
Browse files Browse the repository at this point in the history
Improve Compilation Performance/ Code Quality
  • Loading branch information
InioX authored Aug 21, 2024
2 parents 942efee + cd051ee commit 9b33071
Show file tree
Hide file tree
Showing 28 changed files with 925 additions and 858 deletions.
1,351 changes: 672 additions & 679 deletions Cargo.lock

Large diffs are not rendered by default.

63 changes: 39 additions & 24 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,52 @@ edition = "2021"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winuser"] }

[dependencies]
# This is here because of #43
[target.'cfg(target_os = "macos")'.dependencies]
enquote = "1.1.0"

color-eyre = { version = "0.6.2", default-features = false }
pretty_env_logger = "0.5.0"
paris-log = { version = "1.0.2", features = ["icons"] }
[features]
update-informer = ["dep:update-informer"]
web-image = ["dep:reqwest"]
dump-json = ["dep:serde_json"]

[dependencies]
# logging
log = "0.4.17"
prettytable-rs = "0.10.0"
update-informer = "1.1.0"
reqwest = { version = "0.11.23", default_features = false, features = [
"blocking",
"rustls-tls",
] }
paris-log = { version = "1.0.2", features = ["icons"] } # TODO replace with using paris directly?
pretty_env_logger = "0.5.0"

# terminal output
prettytable-rs = { default-features = false, version = "0.10.0" } # TODO feature-gate? This should definitely be a default feature tho.
color-eyre = { version = "0.6.2", default-features = false }
colorsys = { default-features = false, version = "0.6.7" }
owo-colors = "4.0.0"
colorsys = "0.6.7"

resolve-path = "0.1.0"
directories = "5.0"
proper_path_tools = "0.7.0"
# templating engine
upon = { features = ["filters", "serde"], default-features = false, version = "0.8.0" }

ahash = "0.8.7"
indexmap = "2.2.2"
# config
serde = { version = "1.0.160", features = ["serde_derive", "std"], default-features = false }
toml = { features = ["parse"], default-features = false, version = "0.8.8" }

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"
clap = { version = "4.2.4", features = ["derive", "std"], default-features = false }
material-colors = { version = "0.4.0", features = ["image"] }
regex = "1.10.5"
image = { default-features = false, version = "0.25.2" }
directories = "5.0"

# should probably be removed
indexmap = { default-features = false, version = "2.2.2" } # not sure yet
ahash = { default-features = false, version = "0.8.7" }
resolve-path = "0.1.0"
execute = "0.2.13"

# update-informer feature
update-informer = { version = "1.1.0", optional = true }

# dump-json feature
serde_json = { version = "1.0.107", optional = true }

# web-image feature
reqwest = { version = "0.12.5", default-features = false, features = [
"blocking",
"rustls-tls",
], optional = true }
16 changes: 8 additions & 8 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{ pkgs ? import <nixpkgs> { } }:
let manifest = (pkgs.lib.importTOML ./Cargo.toml).package;
{pkgs ? import <nixpkgs> {}}: let
manifest = (pkgs.lib.importTOML ./Cargo.toml).package;
in
pkgs.rustPlatform.buildRustPackage rec {
pname = manifest.name;
version = manifest.version;
cargoLock.lockFile = ./Cargo.lock;
src = pkgs.lib.cleanSource ./.;
}
pkgs.rustPlatform.buildRustPackage rec {
pname = manifest.name;
version = manifest.version;
cargoLock.lockFile = ./Cargo.lock;
src = pkgs.lib.cleanSource ./.;
}
33 changes: 18 additions & 15 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default-linux";
};
outputs = { self, nixpkgs, systems }:
let
forAllSystems = nixpkgs.lib.genAttrs (import systems);
pkgsFor = nixpkgs.legacyPackages;
in {
packages = forAllSystems (system: {
default = pkgsFor.${system}.callPackage ./. { };
});
devShells = forAllSystems (system: {
default = pkgsFor.${system}.callPackage ./shell.nix { };
});
nixosModules = {
matugen = import ./module.nix self;
default = self.nixosModules.matugen;
};
outputs = {
self,
nixpkgs,
systems,
}: let
forAllSystems = nixpkgs.lib.genAttrs (import systems);
pkgsFor = nixpkgs.legacyPackages;
in {
packages = forAllSystems (system: {
default = pkgsFor.${system}.callPackage ./. {};
});
devShells = forAllSystems (system: {
default = pkgsFor.${system}.callPackage ./shell.nix {};
});
nixosModules = {
matugen = import ./module.nix self;
default = self.nixosModules.matugen;
};
};
}
10 changes: 5 additions & 5 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{ pkgs ? import <nixpkgs> { } }:
{pkgs ? import <nixpkgs> {}}:
pkgs.mkShell {
# Get dependencies from the main package
inputsFrom = [ (pkgs.callPackage ./default.nix { }) ];
inputsFrom = [(pkgs.callPackage ./default.nix {})];
# Additional tooling
buildInputs = with pkgs; [
rust-analyzer # LSP Server
rustfmt # Formatter
clippy # Linter
rustfmt # Formatter
clippy # Linter
];
}
}
47 changes: 29 additions & 18 deletions src/color/color.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use material_colors::{
blend::harmonize, color::Argb, dynamic_color::{DynamicScheme, MaterialDynamicColors}, hct::Hct, image::{FilterType, ImageReader}, scheme::variant::{
blend::harmonize,
color::Argb,
dynamic_color::{DynamicScheme, MaterialDynamicColors},
hct::Hct,
image::{FilterType, ImageReader},
scheme::variant::{
SchemeContent, SchemeExpressive, SchemeFidelity, SchemeFruitSalad, SchemeMonochrome,
SchemeNeutral, SchemeRainbow, SchemeTonalSpot,
}, theme::{ColorGroup, CustomColor, CustomColorGroup}
},
theme::{ColorGroup, CustomColor, CustomColorGroup},
};

use colorsys::{Hsl, Rgb};
use std::str::FromStr;

use crate::{color::math::get_color_distance_lab, scheme::scheme::SchemeTypes};
use crate::{color::math::get_color_distance_lab, scheme::SchemeTypes};

#[derive(clap::Parser, Debug)]
pub enum ColorFormat {
Expand All @@ -21,8 +27,11 @@ pub enum ColorFormat {
pub enum Source {
/// The image to use for generating a color scheme
Image { path: String },

#[cfg(feature = "web-image")]
/// The image to fetch from web and use for generating a color scheme
WebImage { url: String },

/// The source color to use for generating a color scheme
#[clap(subcommand)]
Color(crate::color::color::ColorFormat),
Expand Down Expand Up @@ -62,19 +71,22 @@ impl OwnCustomColor {
}

pub fn get_source_color(source: &Source) -> Result<Argb, Box<dyn std::error::Error>> {
let source_color: Argb = match &source {
use crate::color::color;

let source_color: Argb = match source {
Source::Image { path } => {
// test
info!("Opening image in <d><u>{}</>", path);
crate::color::color::get_source_color_from_image(path).expect("Could not get source color from image")
color::get_source_color_from_image(path).expect("Could not get source color from image")
}
#[cfg(feature = "web-image")]
Source::WebImage { url } => {
info!("Fetching image from <d><u>{}</>", url);
crate::color::color::get_source_color_from_web_image(url).expect("Could not get source color from web image")
}
Source::Color(color) => {
crate::color::color::get_source_color_from_color(color).expect("Could not get source color from color")
color::get_source_color_from_web_image(url)
.expect("Could not get source color from web image")
}
Source::Color(color) => color::get_source_color_from_color(color)
.expect("Could not get source color from color"),
};
Ok(source_color)
}
Expand All @@ -87,16 +99,17 @@ pub fn get_source_color_from_image(path: &str) -> Result<Argb, Box<dyn std::erro
)))
}

#[cfg(feature = "web-image")]
pub fn get_source_color_from_web_image(url: &str) -> Result<Argb, Box<dyn std::error::Error>> {
let bytes = reqwest::blocking::get(url)?.bytes()?;
Ok(ImageReader::extract_color(ImageReader::read(&bytes)?.resize(
128,
128,
FilterType::Lanczos3,
)))
Ok(ImageReader::extract_color(
ImageReader::read(&bytes)?.resize(128, 128, FilterType::Lanczos3),
))
}

pub fn get_source_color_from_color(color: &ColorFormat) -> Result<Argb, Box<dyn std::error::Error>> {
pub fn get_source_color_from_color(
color: &ColorFormat,
) -> Result<Argb, Box<dyn std::error::Error>> {
match color {
ColorFormat::Hex { string } => {
Ok(Argb::from_str(string).expect("Invalid hex color string provided"))
Expand Down Expand Up @@ -169,8 +182,6 @@ pub fn make_custom_color(
let light = generate_dynamic_scheme(scheme_type, value, false, contrast_level);
let dark = generate_dynamic_scheme(scheme_type, value, true, contrast_level);



// debug!("custom_color: {:#?}", &custom_color);
CustomColorGroup {
color,
Expand Down Expand Up @@ -207,4 +218,4 @@ pub fn color_to_string(colors_to_compare: &Vec<ColorDefinition>, compare_to: &st
closest_distance, closest_color
);
closest_color.to_string()
}
}
4 changes: 2 additions & 2 deletions src/color/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use material_colors::color::Argb;
use colorsys::{ColorAlpha, Hsl, Rgb};
use material_colors::color::Argb;

pub fn rgb_from_argb(color: Argb) -> Rgb {
Rgb::from([
Expand Down Expand Up @@ -74,4 +74,4 @@ pub fn format_hsla(color: &Hsl, divide: bool) -> String {
color.alpha()
)
}
}
}
10 changes: 7 additions & 3 deletions src/color/math.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use colorsys::Rgb;
use material_colors::color::{Argb, Lab};
use std::str::FromStr;
use colorsys::Rgb;

pub fn get_color_distance_lab(c1: &str, c2: &str) -> f64 {
let c1 = Lab::from(Argb::from_str(c1).unwrap());
Expand All @@ -25,5 +25,9 @@ pub fn get_color_distance(c1: &Rgb, c2: &Rgb) -> f64 {
let weight_g: f64 = 4.0;
let weight_b: f64 = 2.0 + (255.0 - rmean) / 256.0;

return f64::sqrt(weight_r * i64::pow(r1-r2, 2) as f64 + weight_g * i64::pow(g1-g2, 2) as f64 + weight_b * i64::pow(b1-b2, 2) as f64)
}
f64::sqrt(
weight_r * i64::pow(r1 - r2, 2) as f64
+ weight_g * i64::pow(g1 - g2, 2) as f64
+ weight_b * i64::pow(b1 - b2, 2) as f64,
)
}
4 changes: 2 additions & 2 deletions src/color/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod color;
pub mod format;
pub mod math;
pub mod color;
pub mod parse;
pub mod parse;
2 changes: 1 addition & 1 deletion src/color/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ pub fn check_string_value(value: &Value) -> Option<&String> {
Value::String(v) => Some(v),
_v => None,
}
}
}
1 change: 1 addition & 0 deletions src/exec/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

24 changes: 11 additions & 13 deletions src/exec/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ use upon::{Engine, Syntax, Template, Value};

use crate::color::color::color_to_string;


pub fn format_hook(
engine: &Engine,
render_data: &mut Value,
hook: &String,
colors_to_compare: &Option<Vec<crate::color::color::ColorDefinition>>,
compare_to: &Option<String>,
) -> Result<(), Box<dyn std::error::Error>> {
let closest_color: Option<String> =
if colors_to_compare.is_some() && compare_to.is_some() {
let s = engine.compile(compare_to.as_ref().unwrap())?;
let compare_to = s.render(engine, &render_data).to_string()?;
Some(color_to_string(
colors_to_compare.as_ref().unwrap(),
&compare_to,
))
} else {
None
};
let closest_color: Option<String> = if colors_to_compare.is_some() && compare_to.is_some() {
let s = engine.compile(compare_to.as_ref().unwrap())?;
let compare_to = s.render(engine, &*render_data).to_string()?;
Some(color_to_string(
colors_to_compare.as_ref().unwrap(),
&compare_to,
))
} else {
None
};

let t = engine.compile(hook)?;
let res = if colors_to_compare.is_some() && compare_to.is_some() {
Expand Down Expand Up @@ -66,7 +64,7 @@ pub fn format_hook_text(
}
}

let data = template.render(&engine, &render_data).to_string().unwrap();
let data = template.render(&engine, render_data).to_string().unwrap();

data
}
2 changes: 1 addition & 1 deletion src/exec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod command;
pub mod hook;
pub mod command;
4 changes: 2 additions & 2 deletions src/filters/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use colorsys::{ColorAlpha, Hsl, Rgb};
use std::str::FromStr;

use crate::color::{
parse::{parse_color, check_string_value},
format::{format_rgba, format_hsla},
format::{format_hsla, format_rgba},
parse::{check_string_value, parse_color},
};

pub fn set_alpha(value: &Value, amount: f64) -> Result<String, String> {
Expand Down
6 changes: 3 additions & 3 deletions src/filters/grayscale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use colorsys::{ColorTransform, Hsl, Rgb};
use std::str::FromStr;

use crate::color::{
parse::{parse_color, check_string_value},
format::{format_hex, format_hex_stripped, format_rgb, format_rgba, format_hsl, format_hsla},
format::{format_hex, format_hex_stripped, format_hsl, format_hsla, format_rgb, format_rgba},
parse::{check_string_value, parse_color},
};

pub fn grayscale(value: &Value) -> Result<String, String> {
Expand Down Expand Up @@ -50,4 +50,4 @@ pub fn grayscale(value: &Value) -> Result<String, String> {
}
v => Ok(v.to_string()),
}
}
}
4 changes: 2 additions & 2 deletions src/filters/hue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use colorsys::{ColorTransform, Hsl, Rgb};
use std::str::FromStr;

use crate::color::{
parse::{parse_color, check_string_value},
format::{format_hex, format_hex_stripped, format_rgb, format_rgba, format_hsl, format_hsla},
format::{format_hex, format_hex_stripped, format_hsl, format_hsla, format_rgb, format_rgba},
parse::{check_string_value, parse_color},
};

pub fn set_hue(value: &Value, amount: f64) -> Result<String, String> {
Expand Down
Loading

0 comments on commit 9b33071

Please sign in to comment.