diff options
author | 2022-05-26 23:58:56 +0200 | |
---|---|---|
committer | 2022-05-26 23:58:56 +0200 | |
commit | d988d813d77bc23147a179586206048e6cc42157 (patch) | |
tree | 70226d18d1255d2cc0bb342fca6c2362f6652611 /style/src/theme | |
parent | d5bc610d0139fb331a59fc904a932d156f637391 (diff) | |
download | iced-d988d813d77bc23147a179586206048e6cc42157.tar.gz iced-d988d813d77bc23147a179586206048e6cc42157.tar.bz2 iced-d988d813d77bc23147a179586206048e6cc42157.zip |
Introduce specific types for each `palette::Extended` field
We will have more control over color calculations for each semantic purpose this way.
Diffstat (limited to 'style/src/theme')
-rw-r--r-- | style/src/theme/palette.rs | 139 |
1 files changed, 109 insertions, 30 deletions
diff --git a/style/src/theme/palette.rs b/style/src/theme/palette.rs index acc147ed..3d1ca097 100644 --- a/style/src/theme/palette.rs +++ b/style/src/theme/palette.rs @@ -60,9 +60,10 @@ impl Palette { pub struct Extended { pub background: Background, - pub primary: Group, - pub success: Group, - pub danger: Group, + pub primary: Primary, + pub secondary: Secondary, + pub success: Success, + pub danger: Danger, } lazy_static! { @@ -75,17 +76,18 @@ impl Extended { pub fn generate(palette: Palette) -> Self { Self { background: Background::new(palette.background, palette.text), - primary: Group::new( + primary: Primary::generate( palette.primary, palette.background, palette.text, ), - success: Group::new( + secondary: Secondary::generate(palette.background, palette.text), + success: Success::generate( palette.success, palette.background, palette.text, ), - danger: Group::new( + danger: Danger::generate( palette.danger, palette.background, palette.text, @@ -94,44 +96,113 @@ impl Extended { } } -pub struct Background { - pub base: Color, - pub weak: Color, - pub strong: Color, +#[derive(Debug, Clone, Copy)] +pub struct Pair { + pub color: Color, pub text: Color, } +impl Pair { + pub fn new(color: Color, text: Color) -> Self { + Self { + color, + text: readable(color, text), + } + } +} + +pub struct Background { + pub base: Pair, + pub weak: Pair, + pub strong: Pair, +} + impl Background { pub fn new(base: Color, text: Color) -> Self { + let weak = mix(base, text, 0.15); + let strong = mix(base, text, 0.25); + Self { - base, - weak: mix(base, text, 0.15), - strong: mix(base, text, 0.25), - text, + base: Pair::new(base, text), + weak: Pair::new(weak, text), + strong: Pair::new(strong, text), } } } -pub struct Group { - pub base: Color, - pub weak: Color, - pub strong: Color, - pub text: Color, +pub struct Primary { + pub base: Pair, + pub weak: Pair, + pub strong: Pair, +} + +impl Primary { + pub fn generate(base: Color, background: Color, text: Color) -> Self { + let weak = mix(base, background, 0.4); + let strong = deviate(base, 0.1); + + Self { + base: Pair::new(base, text), + weak: Pair::new(weak, text), + strong: Pair::new(strong, text), + } + } } -impl Group { - pub fn new(base: Color, background: Color, text: Color) -> Self { - let strong = if is_dark(base) { - lighten(base, 0.1) - } else { - darken(base, 0.1) - }; +pub struct Secondary { + pub base: Pair, + pub weak: Pair, + pub strong: Pair, +} + +impl Secondary { + pub fn generate(base: Color, text: Color) -> Self { + let base = mix(base, text, 0.2); + let weak = mix(base, text, 0.1); + let strong = mix(base, text, 0.3); Self { - base, - weak: mix(base, background, 0.4), - strong, - text: readable(strong, text), + base: Pair::new(base, text), + weak: Pair::new(weak, text), + strong: Pair::new(strong, text), + } + } +} + +pub struct Success { + pub base: Pair, + pub weak: Pair, + pub strong: Pair, +} + +impl Success { + pub fn generate(base: Color, background: Color, text: Color) -> Self { + let weak = mix(base, background, 0.4); + let strong = deviate(base, 0.1); + + Self { + base: Pair::new(base, text), + weak: Pair::new(weak, text), + strong: Pair::new(strong, text), + } + } +} + +pub struct Danger { + pub base: Pair, + pub weak: Pair, + pub strong: Pair, +} + +impl Danger { + pub fn generate(base: Color, background: Color, text: Color) -> Self { + let weak = mix(base, background, 0.4); + let strong = deviate(base, 0.1); + + Self { + base: Pair::new(base, text), + weak: Pair::new(weak, text), + strong: Pair::new(strong, text), } } } @@ -174,6 +245,14 @@ fn lighten(color: Color, amount: f32) -> Color { from_hsl(hsl) } +fn deviate(color: Color, amount: f32) -> Color { + if is_dark(color) { + lighten(color, amount) + } else { + darken(color, amount) + } +} + fn mix(a: Color, b: Color, factor: f32) -> Color { let a_lin = Srgb::from(a).into_linear(); let b_lin = Srgb::from(b).into_linear(); |