abletk/abletk-common/src/color.rs

55 lines
1.4 KiB
Rust
Executable File

/*
* Copyright (C) 2022 Umut İnan Erdoğan <umutinanerdogan@pm.me>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#[cfg(windows)]
use windows::Win32::Graphics::Direct2D::Common::D2D1_COLOR_F;
#[derive(Copy, Clone, Debug)]
pub struct Color(pub f32, pub f32, pub f32, pub f32);
impl Color {
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)
}
}
impl From<Color> for (f64, f64, f64, f64) {
fn from(color: Color) -> Self {
(color.0.into(), color.1.into(), color.2.into(), color.3.into())
}
}
#[cfg(windows)]
impl From<Color> for D2D1_COLOR_F {
fn from(color: Color) -> Self {
Self {
r: color.0,
g: color.1,
b: color.2,
a: color.3,
}
}
}
#[macro_export]
macro_rules! rgb {
($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)
};
}