Use constants for colors in styling example

This commit is contained in:
Héctor Ramón Jiménez 2020-01-07 01:17:32 +01:00
parent b329003c8f
commit fce89d0ffe
2 changed files with 39 additions and 23 deletions

View File

@ -33,6 +33,13 @@ impl Color {
a: 0.0, a: 0.0,
}; };
/// Creates a [`Color`] from its RGB components.
///
/// [`Color`]: struct.Color.html
pub const fn from_rgb(r: f32, g: f32, b: f32) -> Color {
Color { r, g, b, a: 1.0 }
}
/// Creates a [`Color`] from its RGB8 components. /// Creates a [`Color`] from its RGB8 components.
/// ///
/// [`Color`]: struct.Color.html /// [`Color`]: struct.Color.html
@ -45,13 +52,6 @@ impl Color {
} }
} }
/// Creates a [`Color`] from its RGB components.
///
/// [`Color`]: struct.Color.html
pub fn from_rgb(r: f32, g: f32, b: f32) -> Color {
Color { r, g, b, a: 1.0 }
}
/// Converts the [`Color`] into its linear values. /// Converts the [`Color`] into its linear values.
/// ///
/// [`Color`]: struct.Color.html /// [`Color`]: struct.Color.html

View File

@ -201,6 +201,30 @@ mod style {
Color, Color,
}; };
const SURFACE: Color = Color::from_rgb(
0x40 as f32 / 255.0,
0x44 as f32 / 255.0,
0x4B as f32 / 255.0,
);
const ACCENT: Color = Color::from_rgb(
0x6F as f32 / 255.0,
0xFF as f32 / 255.0,
0xE9 as f32 / 255.0,
);
const ACTIVE: Color = Color::from_rgb(
0x72 as f32 / 255.0,
0x89 as f32 / 255.0,
0xDA as f32 / 255.0,
);
const HOVERED: Color = Color::from_rgb(
0x67 as f32 / 255.0,
0x7B as f32 / 255.0,
0xC4 as f32 / 255.0,
);
pub struct Container; pub struct Container;
impl container::StyleSheet for Container { impl container::StyleSheet for Container {
@ -220,9 +244,7 @@ mod style {
impl text_input::StyleSheet for TextInput { impl text_input::StyleSheet for TextInput {
fn active(&self) -> text_input::Style { fn active(&self) -> text_input::Style {
text_input::Style { text_input::Style {
background: Background::Color(Color::from_rgb8( background: Background::Color(SURFACE),
0x40, 0x44, 0x4B,
)),
border_radius: 2, border_radius: 2,
border_width: 0, border_width: 0,
border_color: Color::TRANSPARENT, border_color: Color::TRANSPARENT,
@ -232,7 +254,7 @@ mod style {
fn focused(&self) -> text_input::Style { fn focused(&self) -> text_input::Style {
text_input::Style { text_input::Style {
border_width: 1, border_width: 1,
border_color: Color::from_rgb8(0x6F, 0xFF, 0xE9), border_color: ACCENT,
..self.active() ..self.active()
} }
} }
@ -240,7 +262,7 @@ mod style {
fn hovered(&self) -> text_input::Style { fn hovered(&self) -> text_input::Style {
text_input::Style { text_input::Style {
border_width: 1, border_width: 1,
border_color: Color::from_rgb8(0x5B, 0xC0, 0xBE), border_color: Color { a: 0.3, ..ACCENT },
..self.focused() ..self.focused()
} }
} }
@ -259,9 +281,7 @@ mod style {
impl button::StyleSheet for Button { impl button::StyleSheet for Button {
fn active(&self) -> button::Style { fn active(&self) -> button::Style {
button::Style { button::Style {
background: Some(Background::Color(Color::from_rgb8( background: Some(Background::Color(ACTIVE)),
0x72, 0x89, 0xDA,
))),
border_radius: 3, border_radius: 3,
text_color: Color::WHITE, text_color: Color::WHITE,
..button::Style::default() ..button::Style::default()
@ -270,9 +290,7 @@ mod style {
fn hovered(&self) -> button::Style { fn hovered(&self) -> button::Style {
button::Style { button::Style {
background: Some(Background::Color(Color::from_rgb8( background: Some(Background::Color(HOVERED)),
0x67, 0x7B, 0xC4,
))),
text_color: Color::WHITE, text_color: Color::WHITE,
..self.active() ..self.active()
} }
@ -319,13 +337,11 @@ mod style {
impl slider::StyleSheet for Slider { impl slider::StyleSheet for Slider {
fn active(&self) -> slider::Style { fn active(&self) -> slider::Style {
let blue = Color::from_rgb8(0x72, 0x89, 0xDA);
slider::Style { slider::Style {
rail_colors: (blue, Color { a: 0.1, ..blue }), rail_colors: (ACTIVE, Color { a: 0.1, ..ACTIVE }),
handle: slider::Handle { handle: slider::Handle {
shape: slider::HandleShape::Circle { radius: 9 }, shape: slider::HandleShape::Circle { radius: 9 },
color: blue, color: ACTIVE,
border_width: 0, border_width: 0,
border_color: Color::TRANSPARENT, border_color: Color::TRANSPARENT,
}, },
@ -337,7 +353,7 @@ mod style {
slider::Style { slider::Style {
handle: slider::Handle { handle: slider::Handle {
color: Color::from_rgb(0.90, 0.90, 0.90), color: HOVERED,
..active.handle ..active.handle
}, },
..active ..active