mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-19 14:08:11 +00:00
feat(core/rust): added color constructor from rgba format, added text display functions using top-left coordinates
This commit is contained in:
parent
a1312d8453
commit
9b9443ead7
@ -1050,6 +1050,17 @@ impl Font {
|
|||||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
pub struct Color(u16);
|
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 {
|
impl Color {
|
||||||
pub const fn from_u16(val: u16) -> Self {
|
pub const fn from_u16(val: u16) -> Self {
|
||||||
Self(val)
|
Self(val)
|
||||||
@ -1063,11 +1074,26 @@ impl Color {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub const fn luminance(self) -> u32 {
|
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.g() as u32 * 587) / 1000
|
||||||
+ (self.b() as u32 * 114) / 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 {
|
pub const fn r(self) -> u8 {
|
||||||
(self.0 >> 8) as u8 & 0xF8
|
(self.0 >> 8) as u8 & 0xF8
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user