Compare commits

...

2 Commits

Author SHA1 Message Date
TheOddGarlic a99e19de4b color macro 2022-04-23 12:00:05 +03:00
TheOddGarlic 40084591ff make Color a tuple struct that's always RGBA 2022-04-23 11:19:00 +03:00
5 changed files with 30 additions and 18 deletions

View File

@ -10,29 +10,41 @@
use windows::Win32::Graphics::Direct2D::Common::D2D1_COLOR_F;
#[derive(Copy, Clone, Debug)]
pub enum Color {
RGBA(f32, f32, f32, f32),
RGB(f32, f32, f32),
}
pub struct Color(pub f32, pub f32, pub f32, pub f32);
impl Color {
pub fn to_rgba(&self) -> (f32, f32, f32, f32) {
match *self {
Self::RGBA(r, g, b, a) => (r, g, b, a),
Self::RGB(r, g, b) => (r, g, b, 1.0),
}
pub fn from_rgba(rgba: u32) -> Self {
let r = ((rgba >> 24) & 0xFF) as f32 / 255.0;
let g = ((rgba >> 16) & 0xFF) as f32 / 255.0;
let b = ((rgba >> 8) & 0xFF) as f32 / 255.0;
let a = (rgba & 0xFF) as f32 / 255.0;
Self(r, g, b, a)
}
}
#[cfg(windows)]
impl From<Color> for D2D1_COLOR_F {
fn from(color: Color) -> Self {
let rgba = color.to_rgba();
Self {
r: rgba.0,
g: rgba.1,
b: rgba.2,
a: rgba.3,
r: color.0,
g: color.1,
b: color.2,
a: color.3,
}
}
}
// fixme: figure out how to tell RGB, RGBA, HSL, HSLA, any of those in
// hexadecimal numbers, etc. apart.
#[macro_export]
macro_rules! color {
($r:expr, $g:expr, $b:expr, $a:expr) => {
$crate::color::Color($r, $g, $b, $a)
};
($r:expr, $g:expr, $b:expr) => {
$crate::color::Color($r, $g, $b, 1.0)
};
// ($rgba:expr) => {
// $crate::color::Color::from_rgba($rgba)
// };
}

View File

@ -26,5 +26,6 @@ pub mod prelude {
pub use crate::widget::Label;
pub use crate::widget::Widget;
pub use crate::window::*;
pub use crate::color;
pub use crate::launch;
}

View File

@ -15,6 +15,5 @@ fn launch() -> _ {
.apply_plugin(QuitPlugin)
.add_window(Window::builder(
Label::new("Hello, AbleTK!")
.color(Color::RGB(1.0, 0.0, 1.0))
))
.color(color!(0xFF00FFFF))))
}

View File

@ -29,7 +29,7 @@ impl Label {
pub fn new<S: Into<String>>(text: S) -> Self {
Self {
text: text.into(),
color: Color::RGB(1.0, 1.0, 1.0),
color: Color(1.0, 1.0, 1.0, 1.0),
}
}

View File

@ -209,7 +209,7 @@ impl WindowBuilder {
self.events,
// todo: make this the application name
self.always_on_top,
self.background.unwrap_or(Color::RGB(1.0, 1.0, 1.0)),
self.background.unwrap_or(Color(1.0, 1.0, 1.0, 1.0)),
self.decorations,
self.maximized,
self.resizable,