re_types/datatypes/
color_model.rs1#![allow(unused_braces)]
5#![allow(unused_imports)]
6#![allow(unused_parens)]
7#![allow(clippy::clone_on_copy)]
8#![allow(clippy::cloned_instead_of_copied)]
9#![allow(clippy::map_flatten)]
10#![allow(clippy::needless_question_mark)]
11#![allow(clippy::new_without_default)]
12#![allow(clippy::redundant_closure)]
13#![allow(clippy::too_many_arguments)]
14#![allow(clippy::too_many_lines)]
15#![allow(non_camel_case_types)]
16
17use ::re_types_core::try_serialize_field;
18use ::re_types_core::SerializationResult;
19use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
20use ::re_types_core::{ComponentDescriptor, ComponentType};
21use ::re_types_core::{DeserializationError, DeserializationResult};
22
23#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Default)]
27#[repr(u8)]
28pub enum ColorModel {
29 #[default]
31 L = 1,
32
33 #[allow(clippy::upper_case_acronyms)]
35 RGB = 2,
36
37 #[allow(clippy::upper_case_acronyms)]
39 RGBA = 3,
40
41 #[allow(clippy::upper_case_acronyms)]
43 BGR = 4,
44
45 #[allow(clippy::upper_case_acronyms)]
47 BGRA = 5,
48}
49
50::re_types_core::macros::impl_into_cow!(ColorModel);
51
52impl ::re_types_core::Loggable for ColorModel {
53 #[inline]
54 fn arrow_datatype() -> arrow::datatypes::DataType {
55 #![allow(clippy::wildcard_imports)]
56 use arrow::datatypes::*;
57 DataType::UInt8
58 }
59
60 fn to_arrow_opt<'a>(
61 data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
62 ) -> SerializationResult<arrow::array::ArrayRef>
63 where
64 Self: Clone + 'a,
65 {
66 #![allow(clippy::wildcard_imports)]
67 #![allow(clippy::manual_is_variant_and)]
68 use ::re_types_core::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
69 use arrow::{array::*, buffer::*, datatypes::*};
70 Ok({
71 let (somes, data0): (Vec<_>, Vec<_>) = data
72 .into_iter()
73 .map(|datum| {
74 let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
75 let datum = datum.map(|datum| *datum as u8);
76 (datum.is_some(), datum)
77 })
78 .unzip();
79 let data0_validity: Option<arrow::buffer::NullBuffer> = {
80 let any_nones = somes.iter().any(|some| !*some);
81 any_nones.then(|| somes.into())
82 };
83 as_array_ref(PrimitiveArray::<UInt8Type>::new(
84 ScalarBuffer::from(
85 data0
86 .into_iter()
87 .map(|v| v.unwrap_or_default())
88 .collect::<Vec<_>>(),
89 ),
90 data0_validity,
91 ))
92 })
93 }
94
95 fn from_arrow_opt(
96 arrow_data: &dyn arrow::array::Array,
97 ) -> DeserializationResult<Vec<Option<Self>>>
98 where
99 Self: Sized,
100 {
101 #![allow(clippy::wildcard_imports)]
102 use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
103 use arrow::{array::*, buffer::*, datatypes::*};
104 Ok(arrow_data
105 .as_any()
106 .downcast_ref::<UInt8Array>()
107 .ok_or_else(|| {
108 let expected = Self::arrow_datatype();
109 let actual = arrow_data.data_type().clone();
110 DeserializationError::datatype_mismatch(expected, actual)
111 })
112 .with_context("rerun.datatypes.ColorModel#enum")?
113 .into_iter()
114 .map(|typ| match typ {
115 Some(1) => Ok(Some(Self::L)),
116 Some(2) => Ok(Some(Self::RGB)),
117 Some(3) => Ok(Some(Self::RGBA)),
118 Some(4) => Ok(Some(Self::BGR)),
119 Some(5) => Ok(Some(Self::BGRA)),
120 None => Ok(None),
121 Some(invalid) => Err(DeserializationError::missing_union_arm(
122 Self::arrow_datatype(),
123 "<invalid>",
124 invalid as _,
125 )),
126 })
127 .collect::<DeserializationResult<Vec<Option<_>>>>()
128 .with_context("rerun.datatypes.ColorModel")?)
129 }
130}
131
132impl std::fmt::Display for ColorModel {
133 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
134 match self {
135 Self::L => write!(f, "L"),
136 Self::RGB => write!(f, "RGB"),
137 Self::RGBA => write!(f, "RGBA"),
138 Self::BGR => write!(f, "BGR"),
139 Self::BGRA => write!(f, "BGRA"),
140 }
141 }
142}
143
144impl ::re_types_core::reflection::Enum for ColorModel {
145 #[inline]
146 fn variants() -> &'static [Self] {
147 &[Self::L, Self::RGB, Self::RGBA, Self::BGR, Self::BGRA]
148 }
149
150 #[inline]
151 fn docstring_md(self) -> &'static str {
152 match self {
153 Self::L => "Grayscale luminance intencity/brightness/value, sometimes called `Y`",
154 Self::RGB => "Red, Green, Blue",
155 Self::RGBA => "Red, Green, Blue, Alpha",
156 Self::BGR => "Blue, Green, Red",
157 Self::BGRA => "Blue, Green, Red, Alpha",
158 }
159 }
160}
161
162impl ::re_byte_size::SizeBytes for ColorModel {
163 #[inline]
164 fn heap_size_bytes(&self) -> u64 {
165 0
166 }
167
168 #[inline]
169 fn is_pod() -> bool {
170 true
171 }
172}