From 0a011f90313dfbd77da5fdaa58bd93924ba7625c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 1 May 2020 21:51:08 +0200 Subject: [PATCH] Improve `generate_theme` in `color_palette` --- examples/color_palette/src/main.rs | 42 +++++++++++------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index 46a4d085..12c24a64 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -21,36 +21,24 @@ pub struct State { fn generate_theme(base_color: &Color) -> Vec { use palette::{Hsl, Hue, Shade, Srgb}; - let mut theme = Vec::::new(); + // Convert to HSL color for manipulation let hsl = Hsl::from(Srgb::from(*base_color)); - theme.push( - Srgb::from(hsl.shift_hue(-135.0).lighten(0.075)) - .clamp() - .into(), - ); - theme.push(Srgb::from(hsl.shift_hue(-120.0)).clamp().into()); - theme.push( - Srgb::from(hsl.shift_hue(-105.0).darken(0.075)) - .clamp() - .into(), - ); - theme.push(Srgb::from(hsl.darken(0.075)).clamp().into()); - theme.push(*base_color); - theme.push(Srgb::from(hsl.lighten(0.075)).clamp().into()); - theme.push( - Srgb::from(hsl.shift_hue(105.0).darken(0.075)) - .clamp() - .into(), - ); - theme.push(Srgb::from(hsl.shift_hue(120.0)).clamp().into()); - theme.push( - Srgb::from(hsl.shift_hue(135.0).lighten(0.075)) - .clamp() - .into(), - ); - theme + [ + hsl.shift_hue(-135.0).lighten(0.075), + hsl.shift_hue(-120.0), + hsl.shift_hue(-105.0).darken(0.075), + hsl.darken(0.075), + hsl, + hsl.lighten(0.075), + hsl.shift_hue(105.0).darken(0.075), + hsl.shift_hue(120.0), + hsl.shift_hue(135.0).lighten(0.075), + ] + .iter() + .map(|&color| Srgb::from(color).clamp().into()) + .collect() } struct ColorPicker {