Add ColorSpace type

This commit is contained in:
Ivan Smirnov 2021-11-29 22:23:39 +00:00
parent 3dc0d6adab
commit 9ef519164b
2 changed files with 60 additions and 0 deletions

58
src/colorspace.rs Normal file
View file

@ -0,0 +1,58 @@
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct ColorSpace {
pub r_linear: bool,
pub g_linear: bool,
pub b_linear: bool,
pub a_linear: bool,
}
impl ColorSpace {
pub const SRGB: Self = Self::new(false, false, false, false);
pub const LINEAR: Self = Self::new(true, true, true, true);
pub const SRGB_LINEAR_ALPHA: Self = Self::new(false, false, false, true);
pub const fn new(r_linear: bool, g_linear: bool, b_linear: bool, a_linear: bool) -> Self {
Self { r_linear, g_linear, b_linear, a_linear }
}
pub const fn is_srgb(self) -> bool {
!self.r_linear && !self.g_linear && !self.b_linear && !self.a_linear
}
pub const fn is_linear(self) -> bool {
self.r_linear && self.g_linear && self.b_linear && self.a_linear
}
pub const fn is_srgb_linear_alpha(self) -> bool {
!self.r_linear && !self.g_linear && !self.b_linear && self.a_linear
}
pub const fn to_u8(self) -> u8 {
(self.r_linear as u8) << 3
| (self.g_linear as u8) << 2
| (self.b_linear as u8) << 1
| (self.a_linear as u8)
}
pub const fn from_u8(bits: u8) -> Self {
Self::new(bits & 0x08 != 0, bits & 0x04 != 0, bits & 0x02 != 0, bits & 0x01 != 0)
}
}
impl Default for ColorSpace {
fn default() -> Self {
Self::SRGB
}
}
impl From<u8> for ColorSpace {
fn from(value: u8) -> Self {
Self::from_u8(value)
}
}
impl From<ColorSpace> for u8 {
fn from(value: ColorSpace) -> Self {
value.to_u8()
}
}

View file

@ -1,3 +1,4 @@
mod colorspace;
mod consts;
mod decode;
mod encode;
@ -5,6 +6,7 @@ mod error;
mod header;
mod pixel;
pub use crate::colorspace::ColorSpace;
pub use crate::decode::qoi_decode_to_vec;
pub use crate::encode::qoi_encode_to_vec;
pub use crate::error::{Error, Result};