From 9ef519164b27de5e14d5c5d135dc565034b16dbb Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 29 Nov 2021 22:23:39 +0000 Subject: [PATCH] Add ColorSpace type --- src/colorspace.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 2 files changed, 60 insertions(+) create mode 100644 src/colorspace.rs diff --git a/src/colorspace.rs b/src/colorspace.rs new file mode 100644 index 0000000..6b8b600 --- /dev/null +++ b/src/colorspace.rs @@ -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 for ColorSpace { + fn from(value: u8) -> Self { + Self::from_u8(value) + } +} + +impl From for u8 { + fn from(value: ColorSpace) -> Self { + value.to_u8() + } +} diff --git a/src/lib.rs b/src/lib.rs index 36ae580..46ab40c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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};