Skip to content

Commit

Permalink
Merge pull request #25 from elementh/featrue/public-color-dict
Browse files Browse the repository at this point in the history
feature: public color dictionary
  • Loading branch information
elementh authored Dec 18, 2023
2 parents 316726a + e2203b4 commit 416d17f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/color_dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl ColorInformation {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColorDictionary {
pub monochrome: ColorInformation,
pub red: ColorInformation,
Expand All @@ -40,6 +41,7 @@ pub struct ColorDictionary {
pub purple: ColorInformation,
pub pink: ColorInformation,
}

impl ColorDictionary {
pub fn new() -> ColorDictionary {
ColorDictionary {
Expand Down
25 changes: 21 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

extern crate rand;

mod color_dictionary;
pub mod color_dictionary;

use color_dictionary::{ColorDictionary, ColorInformation};
use rand::rngs::SmallRng;
Expand Down Expand Up @@ -54,6 +54,8 @@ pub struct RandomColor {
pub seed: Option<u64>,
/// Can take values `f32` from 0 to 1.
pub alpha: Option<f32>,
/// Optional, bring your own dictionary
pub color_dictionary: Option<ColorDictionary>,
}

impl Default for RandomColor {
Expand All @@ -63,6 +65,7 @@ impl Default for RandomColor {
luminosity: None,
seed: None,
alpha: Some(1.0),
color_dictionary: Some(ColorDictionary::new()),
}
}
}
Expand All @@ -75,7 +78,10 @@ impl RandomColor {

/// Sets `RandomColor.hue` colorspace.
pub fn hue(&mut self, hue: Color) -> &mut RandomColor {
let cd = ColorDictionary::new();
let cd = match &self.color_dictionary {
Some(color_dict) => color_dict.clone(),
None => ColorDictionary::new(),
};

self.hue = match hue {
Color::Monochrome => Some(cd.monochrome),
Expand Down Expand Up @@ -189,7 +195,11 @@ impl RandomColor {
}

fn pick_saturation(&self, hue: &i64) -> i64 {
let cd = ColorDictionary::new();
let cd = match &self.color_dictionary {
Some(color_dict) => color_dict.clone(),
None => ColorDictionary::new(),
};

let s_range = cd.get_saturation_range(hue);

let s_min = s_range.0;
Expand All @@ -205,7 +215,10 @@ impl RandomColor {
}

fn pick_brightness(&self, hue: &i64, saturation: &i64) -> i64 {
let cd = ColorDictionary::new();
let cd = match &self.color_dictionary {
Some(color_dict) => color_dict.clone(),
None => ColorDictionary::new(),
};

let b_min = cd.get_minimum_value(hue, saturation);
let b_max = 100;
Expand Down Expand Up @@ -357,6 +370,7 @@ mod tests {
luminosity: Some(Luminosity::Light),
seed: Some(42),
alpha: Some(1.0),
color_dictionary: Some(ColorDictionary::new()),
}
.to_hsl_string();

Expand All @@ -383,6 +397,7 @@ mod tests {
luminosity: Some(Luminosity::Light),
seed: Some(hash),
alpha: Some(1.0),
color_dictionary: Some(ColorDictionary::new()),
}
.to_hsl_string();

Expand All @@ -405,6 +420,7 @@ mod tests {
luminosity: Some(Luminosity::Light),
seed: Some(12345u64),
alpha: Some(1.0),
color_dictionary: Some(ColorDictionary::new()),
}
.to_hsl_string();

Expand All @@ -427,6 +443,7 @@ mod tests {
luminosity: Some(Luminosity::Light),
seed: Some(12345u64),
alpha: Some(1.0),
color_dictionary: Some(ColorDictionary::new()),
}
.to_hsl_string();

Expand Down

0 comments on commit 416d17f

Please sign in to comment.