zune_core/
colorspace.rs

1/*
2 * Copyright (c) 2023.
3 *
4 * This software is free software;
5 *
6 * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
7 */
8
9//! Image Colorspace information and manipulation utilities.
10
11/// All possible image colorspaces
12/// Some of them aren't yet supported exist here.
13#[allow(clippy::upper_case_acronyms)]
14#[derive(Copy, Clone, Debug, Eq, PartialEq)]
15#[non_exhaustive]
16pub enum ColorSpace {
17    /// Red, Green , Blue
18    RGB,
19    /// Red, Green, Blue, Alpha
20    RGBA,
21    /// YUV colorspace
22    YCbCr,
23    /// Grayscale colorspace
24    Luma,
25    /// Grayscale with alpha colorspace
26    LumaA,
27    YCCK,
28    /// Cyan , Magenta, Yellow, Black
29    CMYK,
30    /// Blue, Green, Red
31    BGR,
32    /// Blue, Green, Red, Alpha
33    BGRA,
34    /// The colorspace is unknown
35    Unknown,
36    /// Alpha Red Green Blue
37    ARGB,
38    /// Hue,Saturation,Lightness
39    /// Conversion from RGB to HSL and back matches that of Python [colorsys](https://docs.python.org/3/library/colorsys.html) module
40    /// Color type is expected to be in floating point
41    HSL,
42    /// Hue, Saturation,Value
43    ///
44    /// Conversion from RGB to HSV and back matches that of Python [colorsys](https://docs.python.org/3/library/colorsys.html) module
45    /// Color type is expected to be in floating point
46    HSV
47}
48
49impl ColorSpace {
50    /// Number of color channels present for a certain colorspace
51    ///
52    /// E.g. RGB returns 3 since it contains R,G and B colors to make up a pixel
53    pub const fn num_components(&self) -> usize {
54        match self {
55            Self::RGB | Self::YCbCr | Self::BGR | Self::HSV | Self::HSL => 3,
56            Self::RGBA | Self::YCCK | Self::CMYK | Self::BGRA | Self::ARGB => 4,
57            Self::Luma => 1,
58            Self::LumaA => 2,
59            Self::Unknown => 0
60        }
61    }
62
63    pub const fn has_alpha(&self) -> bool {
64        matches!(self, Self::RGBA | Self::LumaA | Self::BGRA | Self::ARGB)
65    }
66
67    pub const fn is_grayscale(&self) -> bool {
68        matches!(self, Self::LumaA | Self::Luma)
69    }
70
71    /// Returns the position of the alpha pixel in a pixel
72    ///
73    ///
74    /// That is for an array of color components say `[0,1,2,3]` if the image has an alpha channel
75    /// and is in RGBA format, this will return `Some(3)`, indicating alpha is found in the third index
76    /// but if the image is in `ARGB` format, it will return `Some(0)` indicating alpha is found in  
77    /// index 0
78    ///
79    /// If an image doesn't have an alpha channel returns `None`
80    ///
81    pub const fn alpha_position(&self) -> Option<usize> {
82        match self {
83            ColorSpace::RGBA => Some(3),
84            ColorSpace::LumaA => Some(1),
85            ColorSpace::BGRA => Some(3),
86            ColorSpace::ARGB => Some(0),
87            _ => None
88        }
89    }
90}
91
92/// Encapsulates all colorspaces supported by
93/// the library
94pub static ALL_COLORSPACES: [ColorSpace; 12] = [
95    ColorSpace::RGB,
96    ColorSpace::RGBA,
97    ColorSpace::LumaA,
98    ColorSpace::Luma,
99    ColorSpace::CMYK,
100    ColorSpace::BGRA,
101    ColorSpace::BGR,
102    ColorSpace::YCCK,
103    ColorSpace::YCbCr,
104    ColorSpace::ARGB,
105    ColorSpace::HSL,
106    ColorSpace::HSV
107];
108
109/// Color characteristics
110///
111/// Gives more information about values in a certain
112/// colorspace
113#[allow(non_camel_case_types)]
114#[derive(Copy, Clone, Debug, PartialEq)]
115pub enum ColorCharacteristics {
116    /// Normal default gamma setting
117    /// The float contains gamma present
118    ///
119    /// The default gamma value is 2.2 but for
120    /// decoders that allow specifying gamma values,e.g PNG,
121    /// the gamma value becomes the specified value by the decoder
122    sRGB,
123    /// Linear transfer characteristics
124    /// The image is in linear colorspace
125    Linear
126}