Rust: Rename Palette to FlavourColours

Plus run `rustfmt`

Closes #26
This commit is contained in:
Carsten Kragelund 2022-10-26 22:41:16 +02:00 committed by Hamothy
parent 41035e40cd
commit b27829caea
6 changed files with 78 additions and 85 deletions

View file

@ -1,15 +1,15 @@
use catppuccin::Flavour;
fn main() {
let (r, g, b) = Flavour::Latte.palette().teal.into();
let (r, g, b) = Flavour::Latte.colours().teal.into();
println!(
"Latte's teal is #{}, which is rgb({r}, {g}, {b})",
Flavour::Latte.palette().teal.hex()
Flavour::Latte.colours().teal.hex()
);
let (r, g, b) = Flavour::Mocha.palette().teal.into();
let (r, g, b) = Flavour::Mocha.colours().teal.into();
println!(
"Mocha's teal is #{}, which is rgb({r}, {g}, {b})",
Flavour::Mocha.palette().teal.hex()
Flavour::Mocha.colours().teal.hex()
);
}

View file

@ -1,13 +1,12 @@
use catppuccin::Flavour;
fn main() {
// iterate over the four Catppuccin flavours.
for flavour in Flavour::into_iter() {
println!("{}", flavour.name());
// iterate over the 26 colours in the flavour.
for (i, colour) in flavour.palette().into_iter().enumerate() {
for (i, colour) in flavour.colours().into_iter().enumerate() {
print!("{}", colour.ansi_paint("██"));
// the 14 analogous colours go on one line,

View file

@ -13,7 +13,7 @@ impl Colour {
/// ```
/// use catppuccin::Flavour;
///
/// let hex = Flavour::Mocha.palette().teal.hex();
/// let hex = Flavour::Mocha.colours().teal.hex();
/// assert_eq!(hex, "94E2D5");
/// ```
pub fn hex(&self) -> String {

View file

@ -1,4 +1,4 @@
use crate::{Colour, Palette};
use crate::{Colour, FlavourColours};
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Flavour {
@ -18,9 +18,9 @@ impl Flavour {
}
}
pub fn palette(self) -> Palette {
pub fn colours(self) -> FlavourColours {
match self {
Self::Latte => Palette {
Self::Latte => FlavourColours {
rosewater: Colour(220, 138, 120),
flamingo: Colour(221, 120, 120),
pink: Colour(234, 118, 203),
@ -48,7 +48,7 @@ impl Flavour {
mantle: Colour(230, 233, 239),
crust: Colour(220, 224, 232),
},
Self::Frappe => Palette {
Self::Frappe => FlavourColours {
rosewater: Colour(242, 213, 207),
flamingo: Colour(238, 190, 190),
pink: Colour(244, 184, 228),
@ -76,7 +76,7 @@ impl Flavour {
mantle: Colour(41, 44, 60),
crust: Colour(35, 38, 52),
},
Self::Macchiato => Palette {
Self::Macchiato => FlavourColours {
rosewater: Colour(244, 219, 214),
flamingo: Colour(240, 198, 198),
pink: Colour(245, 189, 230),
@ -104,7 +104,7 @@ impl Flavour {
mantle: Colour(30, 32, 48),
crust: Colour(24, 25, 38),
},
Self::Mocha => Palette {
Self::Mocha => FlavourColours {
rosewater: Colour(245, 224, 220),
flamingo: Colour(242, 205, 205),
pink: Colour(245, 194, 231),
@ -136,25 +136,19 @@ impl Flavour {
}
/// Returns an iterator over the four delicious Catppuccin flavours.
pub fn into_iter() -> std::array::IntoIter<Flavour, 4> {
[
Self::Latte,
Self::Frappe,
Self::Macchiato,
Self::Mocha,
]
.into_iter()
[Self::Latte, Self::Frappe, Self::Macchiato, Self::Mocha].into_iter()
}
}
#[cfg(test)]
mod tests {
use super::Flavour;
use crate::palette::validate_colours;
use crate::flavour_colours::validate_colours;
use indoc::indoc;
#[test]
fn validate_latte_colours() {
validate_colours(
Flavour::Latte.palette(),
Flavour::Latte.colours(),
indoc! {"
Rosewater #dc8a78 rgb(220, 138, 120) hsl(11, 59%, 67%)
Flamingo #dd7878 rgb(221, 120, 120) hsl(0, 60%, 67%)
@ -189,7 +183,7 @@ mod tests {
#[test]
fn validate_frappe_colours() {
validate_colours(
Flavour::Frappe.palette(),
Flavour::Frappe.colours(),
indoc! {"
Rosewater #f2d5cf rgb(242, 213, 207) hsl(10, 57%, 88%)
Flamingo #eebebe rgb(238, 190, 190) hsl(0, 59%, 84%)
@ -224,7 +218,7 @@ mod tests {
#[test]
fn validate_macchiato_colours() {
validate_colours(
Flavour::Macchiato.palette(),
Flavour::Macchiato.colours(),
indoc! {"
Rosewater #f4dbd6 rgb(244, 219, 214) hsl(10, 58%, 90%)
Flamingo #f0c6c6 rgb(240, 198, 198) hsl(0, 58%, 86%)
@ -259,7 +253,7 @@ mod tests {
#[test]
fn validate_mocha_colours() {
validate_colours(
Flavour::Mocha.palette(),
Flavour::Mocha.colours(),
indoc! {"
Rosewater #f5e0dc rgb(245, 224, 220) hsl(10, 56%, 91%)
Flamingo #f2cdcd rgb(242, 205, 205) hsl(0, 59%, 88%)

View file

@ -2,7 +2,7 @@ use crate::Colour;
/// Contains the colour options for a flavour of Catppuccin.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Palette {
pub struct FlavourColours {
pub rosewater: Colour,
pub flamingo: Colour,
pub pink: Colour,
@ -31,7 +31,7 @@ pub struct Palette {
pub crust: Colour,
}
impl Palette {
impl FlavourColours {
/// Returns an iterator over the colours in the flavour.
pub fn into_iter(&self) -> std::array::IntoIter<&Colour, 26> {
[
@ -71,7 +71,7 @@ impl Palette {
/// Copy the table in question from the following URL:
/// https://github.com/catppuccin/catppuccin#-palettes
#[cfg(test)]
pub(crate) fn validate_colours(flavour: Palette, colours: &str) {
pub(crate) fn validate_colours(flavour_colours: FlavourColours, colours: &str) {
fn from_hex(hex: &str) -> Colour {
let hex = hex.strip_prefix('#').unwrap_or(hex);
let hex = i32::from_str_radix(hex, 16).expect(&format!(
@ -96,30 +96,30 @@ pub(crate) fn validate_colours(flavour: Palette, colours: &str) {
.expect("ran out of colours, check paste format")
};
assert_eq!(next_colour(), flavour.rosewater);
assert_eq!(next_colour(), flavour.flamingo);
assert_eq!(next_colour(), flavour.pink);
assert_eq!(next_colour(), flavour.mauve);
assert_eq!(next_colour(), flavour.red);
assert_eq!(next_colour(), flavour.maroon);
assert_eq!(next_colour(), flavour.peach);
assert_eq!(next_colour(), flavour.yellow);
assert_eq!(next_colour(), flavour.green);
assert_eq!(next_colour(), flavour.teal);
assert_eq!(next_colour(), flavour.sky);
assert_eq!(next_colour(), flavour.sapphire);
assert_eq!(next_colour(), flavour.blue);
assert_eq!(next_colour(), flavour.lavender);
assert_eq!(next_colour(), flavour.text);
assert_eq!(next_colour(), flavour.subtext1);
assert_eq!(next_colour(), flavour.subtext0);
assert_eq!(next_colour(), flavour.overlay2);
assert_eq!(next_colour(), flavour.overlay1);
assert_eq!(next_colour(), flavour.overlay0);
assert_eq!(next_colour(), flavour.surface2);
assert_eq!(next_colour(), flavour.surface1);
assert_eq!(next_colour(), flavour.surface0);
assert_eq!(next_colour(), flavour.base);
assert_eq!(next_colour(), flavour.mantle);
assert_eq!(next_colour(), flavour.crust);
assert_eq!(next_colour(), flavour_colours.rosewater);
assert_eq!(next_colour(), flavour_colours.flamingo);
assert_eq!(next_colour(), flavour_colours.pink);
assert_eq!(next_colour(), flavour_colours.mauve);
assert_eq!(next_colour(), flavour_colours.red);
assert_eq!(next_colour(), flavour_colours.maroon);
assert_eq!(next_colour(), flavour_colours.peach);
assert_eq!(next_colour(), flavour_colours.yellow);
assert_eq!(next_colour(), flavour_colours.green);
assert_eq!(next_colour(), flavour_colours.teal);
assert_eq!(next_colour(), flavour_colours.sky);
assert_eq!(next_colour(), flavour_colours.sapphire);
assert_eq!(next_colour(), flavour_colours.blue);
assert_eq!(next_colour(), flavour_colours.lavender);
assert_eq!(next_colour(), flavour_colours.text);
assert_eq!(next_colour(), flavour_colours.subtext1);
assert_eq!(next_colour(), flavour_colours.subtext0);
assert_eq!(next_colour(), flavour_colours.overlay2);
assert_eq!(next_colour(), flavour_colours.overlay1);
assert_eq!(next_colour(), flavour_colours.overlay0);
assert_eq!(next_colour(), flavour_colours.surface2);
assert_eq!(next_colour(), flavour_colours.surface1);
assert_eq!(next_colour(), flavour_colours.surface0);
assert_eq!(next_colour(), flavour_colours.base);
assert_eq!(next_colour(), flavour_colours.mantle);
assert_eq!(next_colour(), flavour_colours.crust);
}

View file

@ -22,7 +22,7 @@
//! fn confirm(text: String) -> Button {
//! Button {
//! text,
//! background_colour: Flavour::Mocha.palette().green.hex(),
//! background_colour: Flavour::Mocha.colours().green.hex(),
//! }
//! }
//! ```
@ -55,8 +55,8 @@
mod colour;
pub use colour::Colour;
mod palette;
pub use palette::Palette;
mod flavour_colours;
pub use flavour_colours::FlavourColours;
mod flavour;
pub use flavour::Flavour;
@ -80,34 +80,34 @@ mod tests {
#[test]
fn test_colours_iter() {
for flavour in Flavour::into_iter() {
let palette = flavour.palette();
let mut colours = palette.into_iter();
assert_eq!(colours.next(), Some(&palette.rosewater));
assert_eq!(colours.next(), Some(&palette.flamingo));
assert_eq!(colours.next(), Some(&palette.pink));
assert_eq!(colours.next(), Some(&palette.mauve));
assert_eq!(colours.next(), Some(&palette.red));
assert_eq!(colours.next(), Some(&palette.maroon));
assert_eq!(colours.next(), Some(&palette.peach));
assert_eq!(colours.next(), Some(&palette.yellow));
assert_eq!(colours.next(), Some(&palette.green));
assert_eq!(colours.next(), Some(&palette.teal));
assert_eq!(colours.next(), Some(&palette.sky));
assert_eq!(colours.next(), Some(&palette.sapphire));
assert_eq!(colours.next(), Some(&palette.blue));
assert_eq!(colours.next(), Some(&palette.lavender));
assert_eq!(colours.next(), Some(&palette.text));
assert_eq!(colours.next(), Some(&palette.subtext1));
assert_eq!(colours.next(), Some(&palette.subtext0));
assert_eq!(colours.next(), Some(&palette.overlay2));
assert_eq!(colours.next(), Some(&palette.overlay1));
assert_eq!(colours.next(), Some(&palette.overlay0));
assert_eq!(colours.next(), Some(&palette.surface2));
assert_eq!(colours.next(), Some(&palette.surface1));
assert_eq!(colours.next(), Some(&palette.surface0));
assert_eq!(colours.next(), Some(&palette.base));
assert_eq!(colours.next(), Some(&palette.mantle));
assert_eq!(colours.next(), Some(&palette.crust));
let colours = flavour.colours();
let mut colours_iter = colours.into_iter();
assert_eq!(colours_iter.next(), Some(&colours.rosewater));
assert_eq!(colours_iter.next(), Some(&colours.flamingo));
assert_eq!(colours_iter.next(), Some(&colours.pink));
assert_eq!(colours_iter.next(), Some(&colours.mauve));
assert_eq!(colours_iter.next(), Some(&colours.red));
assert_eq!(colours_iter.next(), Some(&colours.maroon));
assert_eq!(colours_iter.next(), Some(&colours.peach));
assert_eq!(colours_iter.next(), Some(&colours.yellow));
assert_eq!(colours_iter.next(), Some(&colours.green));
assert_eq!(colours_iter.next(), Some(&colours.teal));
assert_eq!(colours_iter.next(), Some(&colours.sky));
assert_eq!(colours_iter.next(), Some(&colours.sapphire));
assert_eq!(colours_iter.next(), Some(&colours.blue));
assert_eq!(colours_iter.next(), Some(&colours.lavender));
assert_eq!(colours_iter.next(), Some(&colours.text));
assert_eq!(colours_iter.next(), Some(&colours.subtext1));
assert_eq!(colours_iter.next(), Some(&colours.subtext0));
assert_eq!(colours_iter.next(), Some(&colours.overlay2));
assert_eq!(colours_iter.next(), Some(&colours.overlay1));
assert_eq!(colours_iter.next(), Some(&colours.overlay0));
assert_eq!(colours_iter.next(), Some(&colours.surface2));
assert_eq!(colours_iter.next(), Some(&colours.surface1));
assert_eq!(colours_iter.next(), Some(&colours.surface0));
assert_eq!(colours_iter.next(), Some(&colours.base));
assert_eq!(colours_iter.next(), Some(&colours.mantle));
assert_eq!(colours_iter.next(), Some(&colours.crust));
}
}
}