|
33 | 33 | //! same after casting.
|
34 | 34 | //!
|
35 | 35 | //! ```
|
36 |
| -//! use palette::{cast::{self, ArraysInto}, Srgb, IntoColor}; |
| 36 | +//! use palette::{cast::{self, ArraysAsMut}, Srgb, IntoColor}; |
37 | 37 | //!
|
38 | 38 | //! let color = cast::from_array::<Srgb<u8>>([23, 198, 76]).into_linear();
|
39 | 39 | //! // Note: `Srgb::<u8>::from([23, 198, 76])` works too.
|
40 | 40 | //!
|
41 |
| -//! let buffer = &mut [[64, 139, 10], [93, 18, 214]]; |
42 |
| -//! let color_buffer: &mut [Srgb<u8>] = buffer.arrays_into(); |
| 41 | +//! let mut buffer = [[64, 139, 10], [93, 18, 214]]; |
| 42 | +//! let color_buffer: &mut [Srgb<u8>] = buffer.arrays_as_mut(); |
43 | 43 | //!
|
44 | 44 | //! for destination in color_buffer {
|
45 | 45 | //! let linear_dst = destination.into_linear::<f32>();
|
46 | 46 | //! *destination = (linear_dst + color).into_encoding();
|
47 | 47 | //! }
|
48 | 48 | //! ```
|
49 | 49 | //!
|
50 |
| -//! Trying to cast an array of the wrong size will not compile: |
| 50 | +//! Trying to cast a single array of the wrong size will not compile: |
51 | 51 | //!
|
52 | 52 | //! ```compile_fail
|
53 | 53 | //! use palette::{cast, Srgb};
|
|
68 | 68 | //! or multiplying the length.
|
69 | 69 | //!
|
70 | 70 | //! ```
|
71 |
| -//! use palette::{cast::{self, TryFromComponents}, Srgb}; |
| 71 | +//! use palette::{cast::{self, TryComponentsAs}, Srgb}; |
72 | 72 | //!
|
73 |
| -//! let correct_buffer = &[64, 139, 10, 93, 18, 214]; |
74 |
| -//! assert!(<&[Srgb<u8>]>::try_from_components(correct_buffer).is_ok()); |
| 73 | +//! let correct_buffer = [64, 139, 10, 93, 18, 214]; |
| 74 | +//! let should_be_ok: Result<&[Srgb<u8>], _> = correct_buffer.try_components_as(); |
| 75 | +//! assert!(should_be_ok.is_ok()); |
75 | 76 | //!
|
76 |
| -//! let incorrect_buffer = &[64, 139, 10, 93, 18, 214, 198, 76]; |
77 |
| -//! assert!(<&[Srgb<u8>]>::try_from_components(incorrect_buffer).is_err()); |
| 77 | +//! let incorrect_buffer = [64, 139, 10, 93, 18, 214, 198, 76]; |
| 78 | +//! let should_be_err: Result<&[Srgb<u8>], _> = incorrect_buffer.try_components_as(); |
| 79 | +//! assert!(should_be_err.is_err()); |
78 | 80 | //! ```
|
79 | 81 | //!
|
80 | 82 | //! An alternative, for when the length can be trusted to be correct, is to use
|
81 |
| -//! the `ComponentsInto::components_into` and `FromComponents::from_components` |
82 |
| -//! methods, or the `from_component_*` functions, that panic on error. |
| 83 | +//! methods without the `try_*` prefix, such as `ComponentsAs::components_as`, |
| 84 | +//! or the `from_component_*` functions, that panic on error. |
83 | 85 | //!
|
84 | 86 | //! This works:
|
85 | 87 | //!
|
86 | 88 | //! ```
|
87 |
| -//! use palette::{cast::ComponentsInto, Srgb}; |
| 89 | +//! use palette::{cast::ComponentsAs, Srgb}; |
88 | 90 | //!
|
89 |
| -//! let correct_buffer = &[64, 139, 10, 93, 18, 214]; |
90 |
| -//! let color_buffer: &[Srgb<u8>] = correct_buffer.components_into(); |
| 91 | +//! let correct_buffer = [64, 139, 10, 93, 18, 214]; |
| 92 | +//! let color_buffer: &[Srgb<u8>] = correct_buffer.components_as(); |
91 | 93 | //! ```
|
92 | 94 | //!
|
93 | 95 | //! But this panics:
|
94 | 96 | //!
|
95 | 97 | //! ```should_panic
|
96 |
| -//! use palette::{cast::ComponentsInto, Srgb}; |
| 98 | +//! use palette::{cast::ComponentsAs, Srgb}; |
97 | 99 | //!
|
98 |
| -//! let incorrect_buffer = &[64, 139, 10, 93, 18, 214, 198, 76]; |
99 |
| -//! let color_buffer: &[Srgb<u8>] = incorrect_buffer.components_into(); |
| 100 | +//! let incorrect_buffer = [64, 139, 10, 93, 18, 214, 198, 76]; |
| 101 | +//! let color_buffer: &[Srgb<u8>] = incorrect_buffer.components_as(); |
100 | 102 | //! ```
|
101 | 103 | //!
|
102 | 104 | //! ## Casting Single Colors
|
|
128 | 130 | //!
|
129 | 131 | //! ```
|
130 | 132 | //! // `PackedArgb` is an alias for `Packed<rgb::channels::Argb, P = u32>`.
|
131 |
| -//! use palette::{rgb::PackedArgb, cast::ComponentsInto, Srgba}; |
| 133 | +//! use palette::{rgb::PackedArgb, cast::ComponentsAs, Srgba}; |
132 | 134 | //!
|
133 |
| -//! let components = &[1.0f32, 0.8, 0.2, 0.3, 1.0, 0.5, 0.7, 0.6]; |
134 |
| -//! let colors: &[PackedArgb<_>] = components.components_into(); |
| 135 | +//! let components = [1.0f32, 0.8, 0.2, 0.3, 1.0, 0.5, 0.7, 0.6]; |
| 136 | +//! let colors: &[PackedArgb<_>] = components.components_as(); |
135 | 137 | //!
|
136 | 138 | //! // Notice how the alpha values have moved from the beginning to the end:
|
137 | 139 | //! assert_eq!(Srgba::from(colors[0]), Srgba::new(0.8, 0.2, 0.3, 1.0));
|
|
152 | 154 | //!
|
153 | 155 | //! ```
|
154 | 156 | //! // `PackedArgb` is an alias for `Packed<rgb::channels::Argb, P = u32>`.
|
155 |
| -//! use palette::{rgb::PackedArgb, cast::UintsInto, Srgba}; |
| 157 | +//! use palette::{rgb::PackedArgb, cast::UintsAs, Srgba}; |
156 | 158 | //!
|
157 |
| -//! let raw = &[0xFF7F0080u32, 0xFF60BBCC]; |
158 |
| -//! let colors: &[PackedArgb] = raw.uints_into(); |
| 159 | +//! let raw = [0xFF7F0080u32, 0xFF60BBCC]; |
| 160 | +//! let colors: &[PackedArgb] = raw.uints_as(); |
159 | 161 | //!
|
160 | 162 | //! assert_eq!(colors.len(), 2);
|
161 | 163 | //! assert_eq!(Srgba::from(colors[0]), Srgba::new(0x7F, 0x00, 0x80, 0xFF));
|
162 | 164 | //! assert_eq!(Srgba::from(colors[1]), Srgba::new(0x60, 0xBB, 0xCC, 0xFF));
|
163 | 165 | //! ```
|
164 | 166 |
|
165 | 167 | mod array;
|
166 |
| -mod array_traits; |
| 168 | +mod as_arrays_traits; |
| 169 | +mod as_components_traits; |
| 170 | +mod as_uints_traits; |
| 171 | +mod from_into_arrays_traits; |
| 172 | +mod from_into_components_traits; |
| 173 | +mod from_into_uints_traits; |
167 | 174 | mod packed;
|
168 | 175 | mod uint;
|
169 |
| -mod uint_traits; |
170 | 176 |
|
171 |
| -pub use self::{array::*, array_traits::*, packed::*, uint::*, uint_traits::*}; |
| 177 | +pub use self::{ |
| 178 | + array::*, as_arrays_traits::*, as_components_traits::*, as_uints_traits::*, |
| 179 | + from_into_arrays_traits::*, from_into_components_traits::*, from_into_uints_traits::*, |
| 180 | + packed::*, uint::*, |
| 181 | +}; |
0 commit comments