@@ -27,6 +27,9 @@ style_class!(pub HoverTargetClass);
2727#[ derive( Debug , Clone , PartialEq ) ]
2828pub struct DesignSystem {
2929 pub bg_base : Color ,
30+ pub bg_elevate1 : f32 ,
31+ pub bg_elevate2 : f32 ,
32+ pub bg_darken : f32 ,
3033 pub text_base : Color ,
3134 pub text_lightness : f32 ,
3235 pub primary_base : Color ,
@@ -39,20 +42,23 @@ pub struct DesignSystem {
3942 pub font_size : f32 ,
4043}
4144
42- pub ( crate ) const LIGHT_THEME : DesignSystem = DesignSystem :: light ( ) ;
43- pub ( crate ) const DARK_THEME : DesignSystem = DesignSystem :: dark ( ) ;
45+ // pub(crate) const LIGHT_THEME: DesignSystem = DesignSystem::light();
46+ // pub(crate) const DARK_THEME: DesignSystem = DesignSystem::dark();
4447
4548impl DesignSystem {
4649 /// Create a light mode design system.
47- pub const fn light ( ) -> Self {
50+ pub fn light ( ) -> Self {
4851 Self {
4952 bg_base : hsl ( 0. , 0. , 95. ) ,
50- text_base : hsl ( 0. , 0. , 10. ) ,
53+ bg_elevate1 : 0.05 ,
54+ bg_elevate2 : 0.1 ,
55+ bg_darken : -0.1 ,
56+ text_base : hsl ( 0. , 0. , 0. ) ,
5157 text_lightness : 0.05 ,
52- primary_base : hsl ( 196. , 78. , 42. ) ,
53- success_base : hsl ( 151. , 55. , 40. ) ,
54- warning_base : hsl ( 39. , 79. , 52. ) ,
55- danger_base : hsl ( 355. , 67. , 53. ) ,
58+ primary_base : Color :: from_rgb8 ( 24 , 150 , 194 ) ,
59+ success_base : Color :: from_rgb8 ( 45 , 157 , 103 ) ,
60+ warning_base : Color :: from_rgb8 ( 229 , 162 , 35 ) ,
61+ danger_base : Color :: from_rgb8 ( 215 , 55 , 69 ) ,
5662 padding : 5. ,
5763 border_radius : 5. ,
5864 font_size : 14. ,
@@ -61,15 +67,18 @@ impl DesignSystem {
6167 }
6268
6369 /// Create a dark mode design system.
64- pub const fn dark ( ) -> Self {
70+ pub fn dark ( ) -> Self {
6571 Self {
6672 bg_base : hsl ( 0. , 0. , 15. ) ,
67- text_base : hsl ( 0. , 0. , 0. ) ,
73+ bg_elevate1 : 0.05 ,
74+ bg_elevate2 : 0.1 ,
75+ bg_darken : -0.1 ,
76+ text_base : hsl ( 0. , 0. , 100. ) ,
6877 text_lightness : 0.95 ,
69- primary_base : hsl ( 197. , 67. , 54. ) ,
70- success_base : hsl ( 153. , 47. , 52. ) ,
71- warning_base : hsl ( 38. , 89. , 63. ) ,
72- danger_base : hsl ( 1. , 84. , 64. ) ,
78+ primary_base : Color :: from_rgb8 ( 58 , 170 , 216 ) ,
79+ success_base : Color :: from_rgb8 ( 74 , 190 , 138 ) ,
80+ warning_base : Color :: from_rgb8 ( 245 , 184 , 78 ) ,
81+ danger_base : Color :: from_rgb8 ( 240 , 86 , 84 ) ,
7382 padding : 5. ,
7483 border_radius : 5. ,
7584 font_size : 14. ,
@@ -83,41 +92,45 @@ impl DesignSystem {
8392 self . bg_base
8493 }
8594
86- pub const fn bg_elevated ( & self ) -> Color {
87- hsl_map_lightness ( self . bg_base , 0.05 )
95+ pub fn bg_elevated ( & self ) -> Color {
96+ self . bg_base . map_lightness ( |c| c + self . bg_elevate1 )
97+ }
98+
99+ pub fn bg_elevated2 ( & self ) -> Color {
100+ self . bg_base . map_lightness ( |c| c + self . bg_elevate2 )
88101 }
89102
90- pub const fn bg_overlay ( & self ) -> Color {
103+ pub fn bg_overlay ( & self ) -> Color {
91104 let adjustment = if self . is_dark { 0.1 } else { -0.1 } ;
92- hsl_map_lightness ( self . bg_base , adjustment)
105+ self . bg_base . map_lightness ( |c| c + adjustment)
93106 }
94107
95- pub const fn bg_disabled ( & self ) -> Color {
96- let adjustment = if self . is_dark { -0.05 } else { -0.2 } ;
97- hsl_map_lightness ( self . bg_base , adjustment)
108+ pub fn bg_disabled ( & self ) -> Color {
109+ let adjustment = if self . is_dark { -0.05 } else { -0.1 } ;
110+ self . bg_base . map_lightness ( |c| c + adjustment)
98111 }
99112
100113 // Border
101114
102- pub const fn border ( & self ) -> Color {
115+ pub fn border ( & self ) -> Color {
103116 let adjustment = if self . is_dark { 0.15 } else { -0.15 } ;
104- hsl_map_lightness ( self . bg_base , adjustment)
117+ self . bg_base . map_lightness ( |c| c + adjustment)
105118 }
106-
107- pub const fn border_muted ( & self ) -> Color {
119+
120+ pub fn border_muted ( & self ) -> Color {
108121 let adjustment = if self . is_dark { 0.15 } else { -0.15 } ;
109- hsl_map_lightness ( self . border ( ) , adjustment) . with_alpha ( 0.8 )
122+ self . border ( ) . map_lightness ( |c| c + adjustment) . with_alpha ( 0.8 )
110123 }
111124
112125 // Text
113126
114- pub const fn text ( & self ) -> Color {
115- hsl_map_lightness ( self . text_base , self . text_lightness )
127+ pub fn text ( & self ) -> Color {
128+ self . text_base . map_lightness ( |_| self . text_lightness )
116129 }
117130
118- pub const fn text_muted ( & self ) -> Color {
131+ pub fn text_muted ( & self ) -> Color {
119132 let adjustment = if self . is_dark { -0.25 } else { 0.25 } ;
120- hsl_map_lightness ( self . text_base , adjustment) . with_alpha ( 0.5 )
133+ self . text_base . map_lightness ( |c| c + adjustment) . with_alpha ( 0.5 )
121134 }
122135
123136 // Primary
@@ -126,8 +139,8 @@ impl DesignSystem {
126139 self . primary_base
127140 }
128141
129- pub const fn primary_muted ( & self ) -> Color {
130- hsl_map_lightness ( self . primary_base , - 0.05 )
142+ pub fn primary_muted ( & self ) -> Color {
143+ self . primary_base . map_lightness ( |c| c - 0.05 )
131144 }
132145
133146 // Semantic colors
@@ -281,12 +294,15 @@ impl StylePropValue for DesignSystem {
281294 padding : self . padding * inv_t + other. padding * t,
282295 border_radius : self . border_radius * inv_t + other. border_radius * t,
283296 font_size : self . font_size * inv_t + other. font_size * t,
297+ bg_elevate1 : self . bg_elevate1 * inv_t + other. bg_elevate1 * t,
298+ bg_elevate2 : self . bg_elevate2 * inv_t + other. bg_elevate2 * t,
299+ bg_darken : self . bg_darken * inv_t + other. bg_darken * t,
284300 } )
285301 }
286302}
287303
288304prop ! (
289- pub Theme : DesignSystem { inherited } = LIGHT_THEME
305+ pub Theme : DesignSystem { inherited } = DesignSystem :: light ( )
290306) ;
291307pub trait StyleThemeExt {
292308 fn theme ( self , theme : DesignSystem ) -> Self ;
@@ -549,15 +565,15 @@ pub(crate) fn default_theme(os_theme: winit::window::Theme) -> Style {
549565
550566 Style :: new ( )
551567 . apply_if ( os_theme == winit:: window:: Theme :: Light , |s| {
552- let light = LIGHT_THEME ;
568+ let light = DesignSystem :: light ( ) ;
553569 s. color ( light. text ( ) )
554570 . font_size ( light. font_size ( ) )
555571 . background ( light. bg_base ( ) )
556572 . color ( light. text ( ) )
557573 . theme ( light)
558574 } )
559575 . apply_if ( os_theme == winit:: window:: Theme :: Dark , |s| {
560- let dark = DARK_THEME ;
576+ let dark = DesignSystem :: dark ( ) ;
561577 s. color ( dark. text ( ) )
562578 . font_size ( dark. font_size ( ) )
563579 . background ( dark. bg_base ( ) )
@@ -697,53 +713,12 @@ pub const fn hsl(h: f32, s: f32, l: f32) -> Color {
697713 Color :: new ( [ hue, sat, lum, 1. ] )
698714}
699715
700- /// Map lightness using hsl colorspace.
701- pub const fn hsl_map_lightness ( c : Color , adjustment : f32 ) -> Color {
702- let [ r, g, b, _] = c. components ;
703- let [ h, s, l] = rgb_to_hsl ( [ r, g, b] , true ) ;
704- let l = ( ( l * 0.01 ) + adjustment) * 100. ;
705- hsl ( h, s, l)
706- }
707-
708716const fn transform ( n : f32 , h : f32 , light : f32 , a : f32 ) -> f32 {
709717 let x = n + h * ( 1.0 / 30.0 ) ;
710718 let k = x - 12.0 * ( x * ( 1.0 / 12.0 ) ) . floor ( ) ;
711719 light - a * ( k - 3.0 ) . min ( 9.0 - k) . clamp ( -1.0 , 1.0 )
712720}
713721
714- const fn rgb_to_hsl ( [ r, g, b] : [ f32 ; 3 ] , hue_hack : bool ) -> [ f32 ; 3 ] {
715- let max = r. max ( g) . max ( b) ;
716- let min = r. min ( g) . min ( b) ;
717- let mut hue = 0.0 ;
718- let mut sat = 0.0 ;
719- let light = 0.5 * ( min + max) ;
720- let d = max - min;
721-
722- const EPSILON : f32 = 1e-6 ;
723- if d > EPSILON {
724- let denom = light. min ( 1.0 - light) ;
725- if denom. abs ( ) > EPSILON {
726- sat = ( max - light) / denom;
727- }
728- hue = if max == r {
729- ( g - b) / d
730- } else if max == g {
731- ( b - r) / d + 2.0
732- } else {
733- // max == b
734- ( r - g) / d + 4.0
735- } ;
736- hue *= 60.0 ;
737- // Deal with negative saturation from out of gamut colors
738- if hue_hack && sat < 0.0 {
739- hue += 180.0 ;
740- sat = sat. abs ( ) ;
741- }
742- hue -= 360. * ( hue * ( 1.0 / 360.0 ) ) . floor ( ) ;
743- }
744- [ hue, sat * 100.0 , light * 100.0 ]
745- }
746-
747722#[ test]
748723fn rgb_hsl_conversion ( ) {
749724 let rgb_bg_base = Color :: from_rgb8 ( 242 , 242 , 242 ) ;
0 commit comments