diff options
Diffstat (limited to 'src/data.rs')
-rw-r--r-- | src/data.rs | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/src/data.rs b/src/data.rs index edec158d9963..4cfe0c09e463 100644 --- a/src/data.rs +++ b/src/data.rs @@ -2,6 +2,8 @@ use crate::io::Io; use async_std::sync::Arc; +use rand::seq::SliceRandom; +use rand::thread_rng; use serde::{Deserialize, Serialize}; use std::{ collections::BTreeMap, @@ -97,9 +99,85 @@ pub struct Team { } /// An RGB color without alpha -#[derive(Serialize, Deserialize)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct Color(u8, u8, u8); +impl Color { + pub fn black() -> Self { + Self(50, 50, 50) + } + + pub fn red() -> Self { + Self(250, 50, 50) + } + + pub fn green() -> Self { + Self(100, 250, 100) + } + + pub fn blue() -> Self { + Self(100, 100, 250) + } + + pub fn teal() -> Self { + Self(150, 250, 250) + } + + pub fn purple() -> Self { + Self(150, 100, 250) + } + + pub fn orange() -> Self { + Self(250, 200, 100) + } + + pub fn yellow() -> Self { + Self(250, 250, 100) + } + + pub fn white() -> Self { + Self(225, 225, 225) + } +} + +pub trait ColorPalette { + /// Create a new color palette + fn palette() -> Self; + /// Get a palette without a certain colour + fn without(b: &Color) -> Self; + /// Mix a color back into the available palette + fn remix(&mut self, new: Color); +} + +impl ColorPalette for Vec<Color> { + fn palette() -> Self { + let mut rng = thread_rng(); + let mut pal = vec![ + Color::black(), + Color::red(), + Color::green(), + Color::blue(), + Color::teal(), + Color::purple(), + Color::orange(), + Color::yellow(), + Color::white(), + ]; + pal.shuffle(&mut rng); + pal + } + + fn without(b: &Color) -> Self { + Self::palette().into_iter().filter(|a| a == b).collect() + } + + fn remix(&mut self, new: Color) { + let mut rng = thread_rng(); + self.push(new); + self.shuffle(&mut rng); + } +} + /// Describes ownership state #[derive(Serialize, Deserialize)] pub enum Owner { |