Introduce specific types for each palette::Extended field

We will have more control over color calculations for each semantic purpose this way.
This commit is contained in:
Héctor Ramón Jiménez 2022-05-26 23:58:56 +02:00
parent d5bc610d01
commit d988d813d7
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
4 changed files with 142 additions and 88 deletions

View file

@ -12,8 +12,8 @@ use iced::theme::{self, Theme};
use iced::time; use iced::time;
use iced::window; use iced::window;
use iced::{ use iced::{
Alignment, Application, Checkbox, Column, Command, Container, Element, Alignment, Application, Checkbox, Column, Command, Element, Length, Row,
Length, Row, Settings, Subscription, Text, Settings, Subscription, Text,
}; };
use preset::Preset; use preset::Preset;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -143,20 +143,19 @@ impl Application for GameOfLife {
self.grid.preset(), self.grid.preset(),
); );
let content = Column::new() Column::new()
.push( .push(
self.grid self.grid
.view() .view()
.map(move |message| Message::Grid(message, version)), .map(move |message| Message::Grid(message, version)),
) )
.push(controls); .push(controls)
Container::new(content)
.width(Length::Fill)
.height(Length::Fill)
.style(style::Container)
.into() .into()
} }
fn theme(&self) -> Theme {
Theme::Dark
}
} }
mod grid { mod grid {

View file

@ -1,4 +1,4 @@
use iced::{container, pick_list, Background, Color}; use iced::{pick_list, Color};
const BACKGROUND: Color = Color::from_rgb( const BACKGROUND: Color = Color::from_rgb(
0x2F as f32 / 255.0, 0x2F as f32 / 255.0,
@ -6,20 +6,6 @@ const BACKGROUND: Color = Color::from_rgb(
0x36 as f32 / 255.0, 0x36 as f32 / 255.0,
); );
pub struct Container;
impl container::StyleSheet for Container {
fn style(&self) -> container::Style {
container::Style {
background: Some(Background::Color(Color::from_rgb8(
0x36, 0x39, 0x3F,
))),
text_color: Some(Color::WHITE),
..container::Style::default()
}
}
}
pub struct PickList; pub struct PickList;
impl pick_list::StyleSheet for PickList { impl pick_list::StyleSheet for PickList {

View file

@ -40,13 +40,13 @@ impl application::StyleSheet for Theme {
fn background_color(&self) -> Color { fn background_color(&self) -> Color {
let palette = self.extended_palette(); let palette = self.extended_palette();
palette.background.base palette.background.base.color
} }
fn text_color(&self) -> Color { fn text_color(&self) -> Color {
let palette = self.extended_palette(); let palette = self.extended_palette();
palette.background.text palette.background.base.text
} }
} }
@ -76,29 +76,19 @@ impl button::StyleSheet for Theme {
..button::Appearance::default() ..button::Appearance::default()
}; };
let from_pair = |pair: palette::Pair| button::Appearance {
background: Some(pair.color.into()),
text_color: pair.text,
..appearance
};
match style { match style {
Button::Primary => button::Appearance { Button::Primary => from_pair(palette.primary.strong),
background: Some(palette.primary.strong.into()), Button::Secondary => from_pair(palette.secondary.base),
text_color: palette.primary.text, Button::Positive => from_pair(palette.success.base),
..appearance Button::Destructive => from_pair(palette.danger.base),
},
Button::Secondary => button::Appearance {
background: Some(palette.background.weak.into()),
text_color: palette.background.text,
..appearance
},
Button::Positive => button::Appearance {
background: Some(palette.success.base.into()),
text_color: palette.success.text,
..appearance
},
Button::Destructive => button::Appearance {
background: Some(palette.danger.base.into()),
text_color: palette.danger.text,
..appearance
},
Button::Text => button::Appearance { Button::Text => button::Appearance {
text_color: palette.background.text, text_color: palette.background.base.text,
..appearance ..appearance
}, },
} }
@ -109,10 +99,10 @@ impl button::StyleSheet for Theme {
let palette = self.extended_palette(); let palette = self.extended_palette();
let background = match style { let background = match style {
Button::Primary => Some(palette.primary.base), Button::Primary => Some(palette.primary.base.color),
Button::Secondary => Some(palette.background.strong), Button::Secondary => Some(palette.background.strong.color),
Button::Positive => Some(palette.success.strong), Button::Positive => Some(palette.success.strong.color),
Button::Destructive => Some(palette.danger.strong), Button::Destructive => Some(palette.danger.strong.color),
Button::Text => None, Button::Text => None,
}; };
@ -140,10 +130,10 @@ impl slider::StyleSheet for Theme {
}; };
slider::Appearance { slider::Appearance {
rail_colors: (palette.primary.base, palette.background.base), rail_colors: (palette.primary.base.color, Color::TRANSPARENT),
handle: slider::Handle { handle: slider::Handle {
color: palette.background.base, color: palette.background.base.color,
border_color: palette.primary.base, border_color: palette.primary.base.color,
..handle ..handle
}, },
} }
@ -155,7 +145,7 @@ impl slider::StyleSheet for Theme {
slider::Appearance { slider::Appearance {
handle: slider::Handle { handle: slider::Handle {
color: palette.primary.weak, color: palette.primary.weak.color,
..active.handle ..active.handle
}, },
..active ..active
@ -168,7 +158,7 @@ impl slider::StyleSheet for Theme {
slider::Appearance { slider::Appearance {
handle: slider::Handle { handle: slider::Handle {
color: palette.primary.base, color: palette.primary.base.color,
..active.handle ..active.handle
}, },
..active ..active

View file

@ -60,9 +60,10 @@ impl Palette {
pub struct Extended { pub struct Extended {
pub background: Background, pub background: Background,
pub primary: Group, pub primary: Primary,
pub success: Group, pub secondary: Secondary,
pub danger: Group, pub success: Success,
pub danger: Danger,
} }
lazy_static! { lazy_static! {
@ -75,17 +76,18 @@ impl Extended {
pub fn generate(palette: Palette) -> Self { pub fn generate(palette: Palette) -> Self {
Self { Self {
background: Background::new(palette.background, palette.text), background: Background::new(palette.background, palette.text),
primary: Group::new( primary: Primary::generate(
palette.primary, palette.primary,
palette.background, palette.background,
palette.text, palette.text,
), ),
success: Group::new( secondary: Secondary::generate(palette.background, palette.text),
success: Success::generate(
palette.success, palette.success,
palette.background, palette.background,
palette.text, palette.text,
), ),
danger: Group::new( danger: Danger::generate(
palette.danger, palette.danger,
palette.background, palette.background,
palette.text, palette.text,
@ -94,44 +96,113 @@ impl Extended {
} }
} }
pub struct Background { #[derive(Debug, Clone, Copy)]
pub base: Color, pub struct Pair {
pub weak: Color, pub color: Color,
pub strong: Color,
pub text: Color, pub text: Color,
} }
impl Background { impl Pair {
pub fn new(base: Color, text: Color) -> Self { pub fn new(color: Color, text: Color) -> Self {
Self { Self {
base, color,
weak: mix(base, text, 0.15), text: readable(color, text),
strong: mix(base, text, 0.25),
text,
} }
} }
} }
pub struct Group { pub struct Background {
pub base: Color, pub base: Pair,
pub weak: Color, pub weak: Pair,
pub strong: Color, pub strong: Pair,
pub text: Color,
} }
impl Group { impl Background {
pub fn new(base: Color, background: Color, text: Color) -> Self { pub fn new(base: Color, text: Color) -> Self {
let strong = if is_dark(base) { let weak = mix(base, text, 0.15);
lighten(base, 0.1) let strong = mix(base, text, 0.25);
} else {
darken(base, 0.1)
};
Self { Self {
base, base: Pair::new(base, text),
weak: mix(base, background, 0.4), weak: Pair::new(weak, text),
strong, strong: Pair::new(strong, text),
text: readable(strong, text), }
}
}
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),
}
}
}
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: 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) 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 { fn mix(a: Color, b: Color, factor: f32) -> Color {
let a_lin = Srgb::from(a).into_linear(); let a_lin = Srgb::from(a).into_linear();
let b_lin = Srgb::from(b).into_linear(); let b_lin = Srgb::from(b).into_linear();