Skip to content

Commit

Permalink
Merge pull request #24 from elementh/feature/upgrade
Browse files Browse the repository at this point in the history
Feature/upgrade
  • Loading branch information
elementh committed Dec 18, 2023
2 parents 303ca58 + b5ed571 commit 316726a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 57 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "random_color"
version = "0.6.1"
version = "0.7.0"
edition = "2021"
authors = ["Lucas Maximiliano Marino <[email protected]>"]
license = "MIT"
readme = "README.md"
Expand All @@ -10,7 +11,6 @@ homepage ="https://github.com/elementh/random_color"
description = "Rust crate for generating random attractive colors"

[features]
wasm-bindgen = ["rand/wasm-bindgen"]

[dependencies]
rand = { version = "0.7.3", features = ["small_rng"] }
rand = { version = "0.8.5", features = ["small_rng"] }
29 changes: 14 additions & 15 deletions src/color_dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColorInformation {
pub range: [i64; 2],
pub lower_bounds: Vec<[i64; 2]>,
Expand All @@ -7,8 +7,7 @@ pub struct ColorInformation {
}

impl ColorInformation {
pub fn new(range: [i64; 2], lower_bounds: Vec<[i64; 2]>) -> ColorInformation {

pub fn new(range: [i64; 2], lower_bounds: Vec<[i64; 2]>) -> Self {
let saturation_range_min = lower_bounds[0][0];
let saturation_range_max = lower_bounds[lower_bounds.len() - 1][0];

Expand All @@ -19,11 +18,11 @@ impl ColorInformation {

let value_range = [value_range_min, value_range_max];

ColorInformation {
range: range,
lower_bounds: lower_bounds,
saturation_range: saturation_range,
value_range: value_range,
Self {
range,
lower_bounds,
saturation_range,
value_range,
}
}
pub fn has_between_range(&self, hue: &i64) -> bool {
Expand Down Expand Up @@ -167,19 +166,19 @@ impl ColorDictionary {
}

pub fn get_color(self, hue: &i64) -> ColorInformation {
if self.monochrome.has_between_range(&hue) {
if self.monochrome.has_between_range(hue) {
self.monochrome
} else if self.red.has_between_range(&hue) {
} else if self.red.has_between_range(hue) {
self.red
} else if self.orange.has_between_range(&hue) {
} else if self.orange.has_between_range(hue) {
self.orange
} else if self.yellow.has_between_range(&hue) {
} else if self.yellow.has_between_range(hue) {
self.yellow
} else if self.green.has_between_range(&hue) {
} else if self.green.has_between_range(hue) {
self.green
} else if self.blue.has_between_range(&hue) {
} else if self.blue.has_between_range(hue) {
self.blue
} else if self.purple.has_between_range(&hue) {
} else if self.purple.has_between_range(hue) {
self.purple
} else {
self.pink
Expand Down
86 changes: 47 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ mod color_dictionary;
use color_dictionary::{ColorDictionary, ColorInformation};
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color {
Monochrome,
Red,
Expand All @@ -34,15 +35,16 @@ pub enum Color {
Purple,
Pink,
}
#[derive(Debug, PartialEq)]

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Luminosity {
Random,
Bright,
Light,
Dark,
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub struct RandomColor {
/// Can take values of `Color` enum.
pub hue: Option<ColorInformation>,
Expand All @@ -53,16 +55,24 @@ pub struct RandomColor {
/// Can take values `f32` from 0 to 1.
pub alpha: Option<f32>,
}
impl RandomColor {
/// Generates a new RandomColor
pub fn new() -> RandomColor {

impl Default for RandomColor {
fn default() -> Self {
RandomColor {
hue: None,
luminosity: None,
seed: None,
alpha: Some(1.0),
}
}
}

impl RandomColor {
/// Generates a new RandomColor
pub fn new() -> Self {
Self::default()
}

/// Sets `RandomColor.hue` colorspace.
pub fn hue(&mut self, hue: Color) -> &mut RandomColor {
let cd = ColorDictionary::new();
Expand Down Expand Up @@ -120,13 +130,12 @@ impl RandomColor {
}

pub fn to_rgba_string(&self) -> String {
let a: f32;
let (h, s, b) = self.generate_color();
let rgb = self.hsv_to_rgb(h, s, b);
match self.alpha {
Some(alpha) => a = alpha,
None => a = rand::random(),
}
let a: f32 = match self.alpha {
Some(alpha) => alpha,
None => rand::random(),
};

format!("rgba({}, {}, {}, {})", rgb[0], rgb[1], rgb[2], a)
}
Expand All @@ -144,13 +153,12 @@ impl RandomColor {
}

pub fn to_hsla_string(&self) -> String {
let a: f32;
let (h, s, b) = self.generate_color();
let hsv = self.hsv_to_hsl(h, s, b);
match self.alpha {
Some(alpha) => a = alpha,
None => a = rand::random(),
}
let a: f32 = match self.alpha {
Some(alpha) => alpha,
None => rand::random(),
};
format!("hsl({}, {}%, {}%, {})", hsv[0], hsv[1], hsv[2], a)
}

Expand Down Expand Up @@ -216,12 +224,12 @@ impl RandomColor {
}

if min == max {
max = max + 1;
max += 1;
}

match self.seed {
None => SmallRng::from_entropy().gen_range(min, max),
Some(seed) => SmallRng::seed_from_u64(seed as u64).gen_range(min, max),
None => SmallRng::from_entropy().gen_range(min..max),
Some(seed) => SmallRng::seed_from_u64(seed).gen_range(min..max),
}
}

Expand Down Expand Up @@ -330,16 +338,16 @@ impl Seed for &str {
}
}


#[cfg(test)]
mod tests {
use color_dictionary::ColorDictionary;
use RandomColor;

use Color;
use Luminosity;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hasher, Hash};
use std::hash::{Hash, Hasher};

use crate::color_dictionary::ColorDictionary;
use crate::Color;
use crate::Luminosity;
use crate::RandomColor;

#[test]
fn accept_values() {
Expand Down Expand Up @@ -367,7 +375,7 @@ mod tests {
let cd = ColorDictionary::new();

let mut hasher = DefaultHasher::new();
"New Seed".to_string().hash(&mut hasher);
"New Seed".to_string().hash(&mut hasher);
let hash = hasher.finish();

let test_case = RandomColor {
Expand All @@ -376,7 +384,7 @@ mod tests {
seed: Some(hash),
alpha: Some(1.0),
}
.to_hsl_string();
.to_hsl_string();

let rc = RandomColor::new()
.hue(Color::Blue)
Expand All @@ -398,7 +406,7 @@ mod tests {
seed: Some(12345u64),
alpha: Some(1.0),
}
.to_hsl_string();
.to_hsl_string();

let rc = RandomColor::new()
.hue(Color::Blue)
Expand All @@ -420,7 +428,7 @@ mod tests {
seed: Some(12345u64),
alpha: Some(1.0),
}
.to_hsl_string();
.to_hsl_string();

let rc = RandomColor::new()
.hue(Color::Blue)
Expand All @@ -441,7 +449,7 @@ mod tests {
.alpha(1.0)
.to_hsv_array();

assert_eq!(test_case, [189, 40, 91]);
assert_eq!(test_case, [191, 30, 94]);
}
#[test]
fn generates_color_as_rgb_string() {
Expand All @@ -452,7 +460,7 @@ mod tests {
.alpha(1.0)
.to_rgb_string();

assert_eq!(test_case, "rgb(139, 218, 232)");
assert_eq!(test_case, "rgb(167, 226, 239)");
}
#[test]
fn generates_color_as_rgba_string() {
Expand All @@ -463,7 +471,7 @@ mod tests {
.alpha(1.0)
.to_rgba_string();

assert_eq!(test_case, "rgba(139, 218, 232, 1)");
assert_eq!(test_case, "rgba(167, 226, 239, 1)");
}
#[test]
fn generates_color_as_rgb_array() {
Expand All @@ -474,7 +482,7 @@ mod tests {
.alpha(1.0)
.to_rgb_array();

assert_eq!(test_case, [139, 218, 232]);
assert_eq!(test_case, [167, 226, 239]);
}
#[test]
fn generates_color_as_hsl_string() {
Expand All @@ -485,8 +493,9 @@ mod tests {
.alpha(1.0)
.to_hsl_string();

assert_eq!(test_case, "hsl(189, 66%, 27%)");
assert_eq!(test_case, "hsl(191, 70%, 20%)");
}

#[test]
fn generates_color_as_hsla_string() {
let test_case = RandomColor::new()
Expand All @@ -496,7 +505,7 @@ mod tests {
.alpha(1.0)
.to_hsla_string();

assert_eq!(test_case, "hsl(189, 66%, 27%, 1)");
assert_eq!(test_case, "hsl(191, 70%, 20%, 1)");
}
#[test]
fn generates_color_as_hsl_array() {
Expand All @@ -507,7 +516,7 @@ mod tests {
.alpha(1.0)
.to_hsl_array();

assert_eq!(test_case, [189, 66, 27]);
assert_eq!(test_case, [191, 70, 20]);
}
#[test]
fn generates_color_as_hex() {
Expand All @@ -518,7 +527,7 @@ mod tests {
.alpha(1.0)
.to_hex();

assert_eq!(test_case, "#8bdae8");
assert_eq!(test_case, "#a7e2ef");
}

#[test]
Expand Down Expand Up @@ -546,7 +555,6 @@ mod tests {
.seed(5)
.to_hex();

println!("test_case: {}", test_case);
assert_eq!(test_case, "#0f0d93");
assert_eq!(test_case, "#3e0496");
}
}

0 comments on commit 316726a

Please sign in to comment.