rgb/legacy/internal/convert/
array.rs

1use crate::alt::ARGB;
2use crate::alt::{BGR, BGRA};
3use crate::{RGB, RGBA};
4
5impl<T: Copy> From<[T; 3]> for RGB<T> {
6    #[inline(always)]
7    fn from(other: [T; 3]) -> Self {
8        Self {
9            r: other[0],
10            g: other[1],
11            b: other[2],
12        }
13    }
14}
15
16impl<T> From<RGB<T>> for [T; 3] {
17    #[inline(always)]
18    fn from(value: RGB<T>) -> Self {
19        [value.r, value.g, value.b]
20    }
21}
22
23impl<T: Copy> From<[T; 4]> for RGBA<T> {
24    #[inline(always)]
25    fn from(other: [T; 4]) -> Self {
26        Self {
27            r: other[0],
28            g: other[1],
29            b: other[2],
30            a: other[3],
31        }
32    }
33}
34
35impl<T> From<RGBA<T>> for [T; 4] {
36    #[inline(always)]
37    fn from(value: RGBA<T>) -> Self {
38        [value.r, value.g, value.b, value.a]
39    }
40}
41
42impl<T: Copy> From<[T; 4]> for ARGB<T> {
43    #[inline(always)]
44    fn from(other: [T; 4]) -> Self {
45        Self {
46            a: other[0],
47            r: other[1],
48            g: other[2],
49            b: other[3],
50        }
51    }
52}
53
54impl<T> Into<[T; 4]> for ARGB<T> {
55    #[inline(always)]
56    fn into(self) -> [T; 4] {
57        [self.a, self.r, self.g, self.b]
58    }
59}
60
61impl<T: Copy> From<[T; 3]> for BGR<T> {
62    #[inline(always)]
63    fn from(other: [T; 3]) -> Self {
64        Self {
65            b: other[0],
66            g: other[1],
67            r: other[2],
68        }
69    }
70}
71
72impl<T> From<BGR<T>> for [T; 3] {
73    #[inline(always)]
74    fn from(value: BGR<T>) -> Self {
75        [value.b, value.g, value.r]
76    }
77}
78
79impl<T: Copy> From<[T; 4]> for BGRA<T> {
80    #[inline(always)]
81    fn from(other: [T; 4]) -> Self {
82        Self {
83            b: other[0],
84            g: other[1],
85            r: other[2],
86            a: other[3],
87        }
88    }
89}
90
91impl<T> From<BGRA<T>> for [T; 4] {
92    #[inline(always)]
93    fn from(value: BGRA<T>) -> Self {
94        [value.b, value.g, value.r, value.a]
95    }
96}
97
98#[test]
99#[allow(deprecated)]
100fn convert_array() {
101    use crate::alt::{BGR8, BGRA8};
102    use crate::{RGB8, RGBA8};
103
104    assert_eq!(RGB8::from([1, 2, 3]), RGB8::new(1, 2, 3));
105    assert_eq!(Into::<[u8; 3]>::into(RGB8::new(1, 2, 3)), [1, 2, 3]);
106    assert_eq!(RGBA8::from([1, 2, 3, 4]), RGBA8::new(1, 2, 3, 4));
107    assert_eq!(Into::<[u8; 4]>::into(RGBA8::new(1, 2, 3, 4)), [1, 2, 3, 4]);
108    assert_eq!(BGR8::from([3, 2, 1]), BGR8::new(1, 2, 3));
109    assert_eq!(Into::<[u8; 3]>::into(BGR8::new(1, 2, 3)), [3, 2, 1]);
110    assert_eq!(BGRA8::from([3, 2, 1, 4]), BGRA8::new(1, 2, 3, 4));
111    assert_eq!(Into::<[u8; 4]>::into(BGRA8::new(1, 2, 3, 4)), [3, 2, 1, 4]);
112}