1
0
Fork 0
forked from koniifer/ableos
ableos-framebuffer/shadeable/src/pixel_format.rs

68 lines
1.5 KiB
Rust
Raw Normal View History

2022-02-03 22:02:58 -06:00
use core::ops::{BitAnd, BitOr, Shr};
2022-01-16 19:42:11 -06:00
pub type Rgba64 = u64;
2022-06-02 06:00:26 -05:00
pub enum ChannelValue {
2022-04-11 17:23:11 -05:00
Dark,
Low,
Mid,
High,
}
impl From<u8> for ChannelValue {
fn from(b: u8) -> Self {
use ChannelValue::*;
match b {
0x00..=0x3f => Dark,
0x40..=0x7f => Low,
0x80..=0xbf => Mid,
0xc0..=0xff => High,
}
}
}
2022-01-16 19:42:11 -06:00
pub fn get_r(rgba: Rgba64) -> u8 {
2022-02-18 10:04:10 -06:00
rgba.bitand(0xff_00_00_00).shr(0o30) as u8
2022-01-16 19:42:11 -06:00
}
pub fn get_g(rgba: Rgba64) -> u8 {
2022-02-18 10:04:10 -06:00
rgba.bitand(0xff_00_00).shr(0o20) as u8
2022-01-16 19:42:11 -06:00
}
2022-01-26 23:38:51 -06:00
pub fn get_b(rgba: Rgba64) -> u8 {
2022-02-18 10:04:10 -06:00
rgba.bitand(0xff_00).shr(0o10) as u8
2022-01-16 19:42:11 -06:00
}
2022-01-26 23:38:51 -06:00
pub fn get_a(rgba: Rgba64) -> u8 {
2022-02-18 10:04:10 -06:00
(rgba & 0xff) as u8
2022-01-16 19:42:11 -06:00
}
pub fn set_r(rgba: Rgba64, r: u8) -> Rgba64 {
2022-02-18 10:04:10 -06:00
rgba.bitand(0xffffffff_00_ff_ff_ff)
.bitor((r as Rgba64).shr(0o30))
2022-01-16 19:42:11 -06:00
}
2022-01-18 06:15:51 -06:00
pub fn set_g(rgba: Rgba64, g: u8) -> Rgba64 {
2022-02-18 10:04:10 -06:00
rgba.bitand(0xffffffff_ff_00_ff_ff)
.bitor((g as Rgba64).shr(0o20))
2022-01-16 19:42:11 -06:00
}
2022-01-18 06:15:51 -06:00
pub fn set_b(rgba: Rgba64, b: u8) -> Rgba64 {
2022-02-18 10:04:10 -06:00
rgba.bitand(0xffffffff_ff_ff_00_ff)
.bitor((b as Rgba64).shr(0o10))
2022-01-16 19:42:11 -06:00
}
2022-01-18 06:15:51 -06:00
pub fn set_a(rgba: Rgba64, a: u8) -> Rgba64 {
2022-02-18 10:04:10 -06:00
rgba.bitand(0xffffffff_ff_ff_ff_00).bitor(a as Rgba64)
2022-01-16 19:42:11 -06:00
}
pub fn rgba_div(a: Rgba64, b: Rgba64) -> Rgba64 {
2022-02-18 10:04:10 -06:00
set_r(0, get_r(a) / get_r(b))
| set_g(0, get_g(a) / get_g(b))
| set_g(0, get_b(a) / get_b(b))
| set_g(0, get_a(a) / get_a(b))
2022-01-16 19:42:11 -06:00
}
2022-01-18 06:15:51 -06:00
pub fn new_rgba64(r: u8, g: u8, b: u8, a: u8) -> Rgba64 {
2022-02-18 10:04:10 -06:00
set_r(0, r) | set_g(0, g) | set_b(0, b) | set_a(0, a)
2022-01-18 06:15:51 -06:00
}