1use crate::{prelude::*, AlphaType};
2use sb::SkColorType;
3use skia_bindings as sb;
4
5/// Describes how pixel bits encode color. A pixel may be an alpha mask, a grayscale, RGB, or ARGB.
6///
7/// The names of each variant implicitly define the channel ordering and size in memory. Due to
8/// historical reasons the names do not follow 100% identical convention, but are typically labeled
9/// from least significant to most significant.
10///
11/// Unless specified otherwise, a channel's value is treated as an unsigned integer with a range of
12/// [0, 2^N-1] and this is mapped uniformly to a floating point value of [0.0, 1.0]. Some color
13/// types instead store data directly in 32-bit floating point (assumed to be IEEE), or in 16-bit
14/// "half" floating point values.
15///
16/// Note: By default, Skia operates with the assumption of a little-Endian system. The bit patterns
17/// shown in the documentation assume LE byte order.
18#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
19#[repr(i32)]
20pub enum ColorType {
21 /// Unknown or unrepresentable as an SkColorType.
22 Unknown = SkColorType::kUnknown_SkColorType as _,
23 /// Single channel data (8-bit) interpreted as an alpha value. RGB are 0.
24 /// Bits: [A:7..0]
25 Alpha8 = SkColorType::kAlpha_8_SkColorType as _,
26 /// Three channel BGR data (5 bits red, 6 bits green, 5 bits blue) packed into a LE 16-bit word.
27 /// Bits: [R:15..11 G:10..5 B:4..0]
28 RGB565 = SkColorType::kRGB_565_SkColorType as _,
29 /// Four channel ABGR data (4 bits per channel) packed into a LE 16-bit word.
30 /// Bits: [R:15..12 G:11..8 B:7..4 A:3..0]
31 ARGB4444 = SkColorType::kARGB_4444_SkColorType as _,
32 /// Four channel RGBA data (8 bits per channel) packed into a LE 32-bit word.
33 /// Bits: [A:31..24 B:23..16 G:15..8 R:7..0]
34 RGBA8888 = SkColorType::kRGBA_8888_SkColorType as _,
35 /// Three channel RGB data (8 bits per channel) packed into a LE 32-bit word. The remaining bits
36 /// are ignored and alpha is forced to opaque.
37 /// Bits: [x:31..24 B:23..16 G:15..8 R:7..0]
38 RGB888x = SkColorType::kRGB_888x_SkColorType as _,
39 /// Four channel BGRA data (8 bits per channel) packed into a LE 32-bit word. R and B are swapped
40 /// relative to RGBA8888.
41 /// Bits: [A:31..24 R:23..16 G:15..8 B:7..0]
42 BGRA8888 = SkColorType::kBGRA_8888_SkColorType as _,
43 /// Four channel RGBA data (10 bits per color, 2 bits for alpha) packed into a LE 32-bit word.
44 /// Bits: [A:31..30 B:29..20 G:19..10 R:9..0]
45 RGBA1010102 = SkColorType::kRGBA_1010102_SkColorType as _,
46 /// Four channel BGRA data (10 bits per color, 2 bits for alpha) packed into a LE 32-bit word.
47 /// R and B are swapped relative to RGBA1010102.
48 /// Bits: [A:31..30 R:29..20 G:19..10 B:9..0]
49 BGRA1010102 = SkColorType::kBGRA_1010102_SkColorType as _,
50 /// Three channel RGB data (10 bits per channel) packed into a LE 32-bit word. The remaining bits
51 /// are ignored and alpha is forced to opaque.
52 /// Bits: [x:31..30 B:29..20 G:19..10 R:9..0]
53 RGB101010x = SkColorType::kRGB_101010x_SkColorType as _,
54 /// Three channel BGR data (10 bits per channel) packed into a LE 32-bit word. The remaining bits
55 /// are ignored and alpha is forced to opaque. R and B are swapped relative to RGB101010x.
56 /// Bits: [x:31..30 R:29..20 G:19..10 B:9..0]
57 BGR101010x = SkColorType::kBGR_101010x_SkColorType as _,
58 /// Three channel BGR data (10 bits per channel) packed into a LE 32-bit word. The remaining bits
59 /// are ignored and alpha is forced to opaque. Instead of normalizing [0, 1023] to [0.0, 1.0] the
60 /// color channels map to an extended range of [-0.752941, 1.25098].
61 /// Bits: [x:31..30 R:29..20 G:19..10 B:9..0]
62 BGR101010xXR = SkColorType::kBGR_101010x_XR_SkColorType as _,
63 /// Four channel BGRA data (10 bits per channel) packed into a LE 64-bit word. Each channel is
64 /// preceded by 6 bits of padding. Instead of normalizing [0, 1023] to [0.0, 1.0] the color and
65 /// alpha channels map to an extended range of [-0.752941, 1.25098].
66 /// Bits: [A:63..54 x:53..48 R:47..38 x:37..32 G:31..22 x:21..16 B:15..6 x:5..0]
67 BGRA10101010XR = SkColorType::kBGRA_10101010_XR_SkColorType as _,
68 /// Four channel RGBA data (10 bits per channel) packed into a LE 64-bit word. Each channel is
69 /// preceded by 6 bits of padding.
70 /// Bits: [A:63..54 x:53..48 B:47..38 x:37..32 G:31..22 x:21..16 R:15..6 x:5..0]
71 RGBA10x6 = SkColorType::kRGBA_10x6_SkColorType as _,
72 /// Single channel data (8-bit) interpreted as a grayscale value (e.g. replicated to RGB).
73 /// Bits: [G:7..0]
74 Gray8 = SkColorType::kGray_8_SkColorType as _,
75 /// Four channel RGBA data (16-bit half-float per channel) packed into a LE 64-bit word. Values
76 /// are assumed to be in [0.0,1.0] range, unlike RGBAF16.
77 /// Bits: [A:63..48 B:47..32 G:31..16 R:15..0]
78 RGBAF16Norm = SkColorType::kRGBA_F16Norm_SkColorType as _,
79 /// Four channel RGBA data (16-bit half-float per channel) packed into a LE 64-bit word.
80 /// This has extended range compared to RGBAF16Norm.
81 /// Bits: [A:63..48 B:47..32 G:31..16 R:15..0]
82 RGBAF16 = SkColorType::kRGBA_F16_SkColorType as _,
83 /// Three channel RGB data (16-bit half-float per channel) packed into a LE 64-bit word. The last
84 /// 16 bits are ignored and alpha is forced to opaque.
85 /// Bits: [x:63..48 B:47..32 G:31..16 R:15..0]
86 RGBF16F16F16x = SkColorType::kRGB_F16F16F16x_SkColorType as _,
87 /// Four channel RGBA data (32-bit float per channel) packed into a LE 128-bit word.
88 /// Bits: [A:127..96 B:95..64 G:63..32 R:31..0]
89 RGBAF32 = SkColorType::kRGBA_F32_SkColorType as _,
90
91 // The following 6 color types are just for reading from - not for rendering to
92 /// Two channel RG data (8 bits per channel). Blue is forced to 0, alpha is forced to opaque.
93 /// Bits: [G:15..8 R:7..0]
94 R8G8UNorm = SkColorType::kR8G8_unorm_SkColorType as _,
95 /// Single channel data (16-bit half-float) interpreted as alpha. RGB are 0.
96 /// Bits: [A:15..0]
97 A16Float = SkColorType::kA16_float_SkColorType as _,
98 /// Two channel RG data (16-bit half-float per channel) packed into a LE 32-bit word.
99 /// Blue is forced to 0, alpha is forced to opaque.
100 /// Bits: [G:31..16 R:15..0]
101 R16G16Float = SkColorType::kR16G16_float_SkColorType as _,
102 /// Single channel data (16 bits) interpreted as alpha. RGB are 0.
103 /// Bits: [A:15..0]
104 A16UNorm = SkColorType::kA16_unorm_SkColorType as _,
105 /// Two channel RG data (16 bits per channel) packed into a LE 32-bit word. B is forced to 0,
106 /// alpha is forced to opaque.
107 /// Bits: [G:31..16 R:15..0]
108 R16G16UNorm = SkColorType::kR16G16_unorm_SkColorType as _,
109 /// Four channel RGBA data (16 bits per channel) packed into a LE 64-bit word.
110 /// Bits: [A:63..48 B:47..32 G:31..16 R:15..0]
111 R16G16B16A16UNorm = SkColorType::kR16G16B16A16_unorm_SkColorType as _,
112 /// Four channel RGBA data (8 bits per channel) packed into a LE 32-bit word. The RGB values are
113 /// assumed to be encoded with the sRGB transfer function.
114 /// Bits: [A:31..24 B:23..16 G:15..8 R:7..0]
115 SRGBA8888 = SkColorType::kSRGBA_8888_SkColorType as _,
116 /// Single channel data (8 bits) interpreted as red. G and B are forced to 0, alpha is forced to
117 /// opaque.
118 /// Bits: [R:7..0]
119 R8UNorm = SkColorType::kR8_unorm_SkColorType as _,
120}
121
122native_transmutable!(SkColorType, ColorType, color_type_layout);
123
124impl ColorType {
125 #[deprecated(since = "0.51.0", note = "Use ColorType::N32 ")]
126 pub const fn n32() -> Self {
127 Self::N32
128 }
129
130 pub const N32: Self = unsafe { *((&SkColorType::kN32_SkColorType) as *const _ as *const _) };
131
132 pub const COUNT: usize =
133 unsafe { *((&SkColorType::kLastEnum_SkColorType) as *const _ as *const _) } as usize
134 + 1usize;
135
136 pub fn bytes_per_pixel(self) -> usize {
137 unsafe {
138 sb::SkColorTypeBytesPerPixel(self.into_native())
139 .try_into()
140 .unwrap()
141 }
142 }
143
144 pub fn is_always_opaque(self) -> bool {
145 unsafe { sb::SkColorTypeIsAlwaysOpaque(self.into_native()) }
146 }
147
148 pub fn validate_alpha_type(self, alpha_type: AlphaType) -> Option<AlphaType> {
149 let mut alpha_type_r = AlphaType::Unknown;
150 unsafe {
151 sb::SkColorTypeValidateAlphaType(self.into_native(), alpha_type, &mut alpha_type_r)
152 }
153 .if_true_some(alpha_type_r)
154 }
155}
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160
161 #[test]
162 fn n32_matches() {
163 assert_eq!(
164 ColorType::from_native_c(skia_bindings::SkColorType::kN32_SkColorType),
165 ColorType::N32
166 );
167 }
168}
169