rgb/legacy/internal/convert/
array.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::alt::ARGB;
use crate::alt::{BGR, BGRA};
use crate::{RGB, RGBA};

impl<T: Copy> From<[T; 3]> for RGB<T> {
    #[inline(always)]
    fn from(other: [T; 3]) -> Self {
        Self {
            r: other[0],
            g: other[1],
            b: other[2],
        }
    }
}

impl<T> From<RGB<T>> for [T; 3] {
    #[inline(always)]
    fn from(value: RGB<T>) -> Self {
        [value.r, value.g, value.b]
    }
}

impl<T: Copy> From<[T; 4]> for RGBA<T> {
    #[inline(always)]
    fn from(other: [T; 4]) -> Self {
        Self {
            r: other[0],
            g: other[1],
            b: other[2],
            a: other[3],
        }
    }
}

impl<T> From<RGBA<T>> for [T; 4] {
    #[inline(always)]
    fn from(value: RGBA<T>) -> Self {
        [value.r, value.g, value.b, value.a]
    }
}

impl<T: Copy> From<[T; 4]> for ARGB<T> {
    #[inline(always)]
    fn from(other: [T; 4]) -> Self {
        Self {
            a: other[0],
            r: other[1],
            g: other[2],
            b: other[3],
        }
    }
}

impl<T> Into<[T; 4]> for ARGB<T> {
    #[inline(always)]
    fn into(self) -> [T; 4] {
        [self.a, self.r, self.g, self.b]
    }
}

impl<T: Copy> From<[T; 3]> for BGR<T> {
    #[inline(always)]
    fn from(other: [T; 3]) -> Self {
        Self {
            b: other[0],
            g: other[1],
            r: other[2],
        }
    }
}

impl<T> From<BGR<T>> for [T; 3] {
    #[inline(always)]
    fn from(value: BGR<T>) -> Self {
        [value.b, value.g, value.r]
    }
}

impl<T: Copy> From<[T; 4]> for BGRA<T> {
    #[inline(always)]
    fn from(other: [T; 4]) -> Self {
        Self {
            b: other[0],
            g: other[1],
            r: other[2],
            a: other[3],
        }
    }
}

impl<T> From<BGRA<T>> for [T; 4] {
    #[inline(always)]
    fn from(value: BGRA<T>) -> Self {
        [value.b, value.g, value.r, value.a]
    }
}

#[test]
#[allow(deprecated)]
fn convert_array() {
    use crate::alt::{BGR8, BGRA8};
    use crate::{RGB8, RGBA8};

    assert_eq!(RGB8::from([1, 2, 3]), RGB8::new(1, 2, 3));
    assert_eq!(Into::<[u8; 3]>::into(RGB8::new(1, 2, 3)), [1, 2, 3]);
    assert_eq!(RGBA8::from([1, 2, 3, 4]), RGBA8::new(1, 2, 3, 4));
    assert_eq!(Into::<[u8; 4]>::into(RGBA8::new(1, 2, 3, 4)), [1, 2, 3, 4]);
    assert_eq!(BGR8::from([3, 2, 1]), BGR8::new(1, 2, 3));
    assert_eq!(Into::<[u8; 3]>::into(BGR8::new(1, 2, 3)), [3, 2, 1]);
    assert_eq!(BGRA8::from([3, 2, 1, 4]), BGRA8::new(1, 2, 3, 4));
    assert_eq!(Into::<[u8; 4]>::into(BGRA8::new(1, 2, 3, 4)), [3, 2, 1, 4]);
}