From 8a4ff4fbb27ef62790d1d2f77ed39fb3836fd32e Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Fri, 6 Sep 2024 12:14:04 +0100 Subject: [PATCH 1/2] feat(red): add random color generation using tinyrand --- src/ext/red.rs | 71 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/src/ext/red.rs b/src/ext/red.rs index 7542354..08d0973 100644 --- a/src/ext/red.rs +++ b/src/ext/red.rs @@ -4,6 +4,8 @@ use std::{fmt, str::FromStr}; use rgb::Rgb; +use strum::EnumCount; +use tinyrand::{RandRange, StdRand}; use crate::Prefix; @@ -11,7 +13,7 @@ use super::ExtendedColour; /// Shades of red #[allow(missing_docs)] -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumCount)] pub enum Red { Maroon, #[allow(clippy::enum_variant_names)] @@ -38,22 +40,22 @@ pub enum Red { impl fmt::Display for Red { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Red::Maroon => write!(f, "#800000"), - Red::DarkRed => write!(f, "#8B0000"), - Red::Brown => write!(f, "#A52A2A"), - Red::Firebrick => write!(f, "#B22222"), - Red::Crimson => write!(f, "#DC143C"), - Red::Red => write!(f, "#FF0000"), - Red::Tomato => write!(f, "#FF6347"), - Red::Coral => write!(f, "#FF7F50"), - Red::IndianRed => write!(f, "#CD5C5C"), - Red::LightCoral => write!(f, "#F08080"), - Red::DarkSalmon => write!(f, "#E9967A"), - Red::Salmon => write!(f, "#FA8072"), - Red::LightSalmon => write!(f, "#FFA07A"), - Red::OrangeRed => write!(f, "#FF4500"), - Red::DarkOrange => write!(f, "#FF8C00"), - Red::Orange => write!(f, "#FFA500"), + Self::Maroon => write!(f, "#800000"), + Self::DarkRed => write!(f, "#8B0000"), + Self::Brown => write!(f, "#A52A2A"), + Self::Firebrick => write!(f, "#B22222"), + Self::Crimson => write!(f, "#DC143C"), + Self::Red => write!(f, "#FF0000"), + Self::Tomato => write!(f, "#FF6347"), + Self::Coral => write!(f, "#FF7F50"), + Self::IndianRed => write!(f, "#CD5C5C"), + Self::LightCoral => write!(f, "#F08080"), + Self::DarkSalmon => write!(f, "#E9967A"), + Self::Salmon => write!(f, "#FA8072"), + Self::LightSalmon => write!(f, "#FFA07A"), + Self::OrangeRed => write!(f, "#FF4500"), + Self::DarkOrange => write!(f, "#FF8C00"), + Self::Orange => write!(f, "#FFA500"), } } } @@ -142,6 +144,41 @@ impl Red { _ => None, } } + + /// Generate a random colour + /// + /// ## Example + /// + ///``` + /// # use named_colour::ext::Red; + /// # fn main() { + /// let colour = Red::random(); + /// + /// # } + /// ``` + pub fn random() -> Self { + let mut rand = StdRand::default(); + + match rand.next_range(0..Red::COUNT) { + 0 => Self::Maroon, + 1 => Self::DarkRed, + 2 => Self::Brown, + 3 => Self::Firebrick, + 4 => Self::Crimson, + 5 => Self::Red, + 6 => Self::Tomato, + 7 => Self::Coral, + 8 => Self::IndianRed, + 9 => Self::LightCoral, + 10 => Self::DarkSalmon, + 11 => Self::Salmon, + 12 => Self::LightSalmon, + 13 => Self::OrangeRed, + 14 => Self::DarkOrange, + 15 => Self::Orange, + _ => Self::Red, + } + } } impl FromStr for Red { From 2324074ef5aee05f3799da2b611a21b7defbe165 Mon Sep 17 00:00:00 2001 From: Jerus Bot <171541392+jerus-bot@users.noreply.github.com> Date: Fri, 6 Sep 2024 11:18:54 +0000 Subject: [PATCH 2/2] chore: update changelog for pr --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71ffd1e..c68c245 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - add random color generation and EnumCount derivation(pr [#78]) - add random color generation and derive EnumCount for Green enum(pr [#79]) - add random color generation and derive EnumCount trait(pr [#80]) +- add random color generation using tinyrand(pr [#81]) ### Changed @@ -195,6 +196,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#78]: https://github.com/jerus-org/named-colour/pull/78 [#79]: https://github.com/jerus-org/named-colour/pull/79 [#80]: https://github.com/jerus-org/named-colour/pull/80 +[#81]: https://github.com/jerus-org/named-colour/pull/81 [Unreleased]: https://github.com/jerus-org/named-colour/compare/v0.3.0...HEAD [0.3.0]: https://github.com/jerus-org/named-colour/compare/v0.2.0...v0.3.0 [0.2.0]: https://github.com/jerus-org/named-colour/compare/v0.1.6...v0.2.0