From 9b9443ead71a4c0874a21b767b4df19e8f5a89fb Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Wed, 18 May 2022 09:45:39 +0200 Subject: [PATCH] feat(core/rust): added color constructor from rgba format, added text display functions using top-left coordinates --- core/embed/rust/src/ui/display/mod.rs | 28 ++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/core/embed/rust/src/ui/display/mod.rs b/core/embed/rust/src/ui/display/mod.rs index c146d35447..fcd668d085 100644 --- a/core/embed/rust/src/ui/display/mod.rs +++ b/core/embed/rust/src/ui/display/mod.rs @@ -1050,6 +1050,17 @@ impl Font { #[derive(Copy, Clone, PartialEq, Eq)] pub struct Color(u16); +#[macro_export] +macro_rules! alpha { + ($n: expr) => { + if ($n >= 1.0) { + 256_u16 + } else { + ($n * 256.0) as u16 + } + }; +} + impl Color { pub const fn from_u16(val: u16) -> Self { Self(val) @@ -1063,11 +1074,26 @@ impl Color { } pub const fn luminance(self) -> u32 { - ((self.r() as u32 * 299) / 1000) + (self.r() as u32 * 299) / 1000 + (self.g() as u32 * 587) / 1000 + (self.b() as u32 * 114) / 1000 } + pub const fn rgba(bg: Color, r: u8, g: u8, b: u8, alpha: u16) -> Self { + let r_u16 = r as u16; + let g_u16 = g as u16; + let b_u16 = b as u16; + + let r = ((256 - alpha) * bg.r() as u16 + alpha * r_u16) >> 8; + let g = ((256 - alpha) * bg.g() as u16 + alpha * g_u16) >> 8; + let b = ((256 - alpha) * bg.b() as u16 + alpha * b_u16) >> 8; + + let r = (r & 0xF8) << 8; + let g = (g & 0xFC) << 3; + let b = (b & 0xF8) >> 3; + Self(r | g | b) + } + pub const fn r(self) -> u8 { (self.0 >> 8) as u8 & 0xF8 }