Skip to content

Commit e823a25

Browse files
authored
Merge 6ef51d8 into ee54371
2 parents ee54371 + 6ef51d8 commit e823a25

14 files changed

+1419
-497
lines changed

no_std_test/src/main.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg_attr(feature = "nightly", feature(lang_items, start))]
1+
#![cfg_attr(feature = "nightly", feature(start))]
22
#![no_std]
33

44
#[cfg(feature = "nightly")]
@@ -14,10 +14,6 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
1414
0
1515
}
1616

17-
#[cfg(feature = "nightly")]
18-
#[lang = "eh_personality"]
19-
extern "C" fn eh_personality() {}
20-
2117
#[cfg(feature = "nightly")]
2218
#[panic_handler]
2319
fn panic(_info: &PanicInfo) -> ! {

palette/README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ A longer and more advanced example that shows how to implement the conversion tr
9292
When working with image or pixel buffers, or any color type that can be converted to a slice of components (ex. `&[u8]`), the `cast` module provides traits and functions for turning them into slices of Palette colors without cloning the whole buffer:
9393

9494
```rust
95-
use palette::{cast::ComponentsInto, Srgb};
95+
use palette::{cast::ComponentsAsMut, Srgb};
9696

9797
// The input to this function could be data from an image file or
9898
// maybe a texture in a game.
9999
fn swap_red_and_blue(my_rgb_image: &mut [u8]) {
100100
// Convert `my_rgb_image` into `&mut [Srgb<u8>]` without copying.
101-
let my_rgb_image: &mut [Srgb<u8>] = my_rgb_image.components_into();
101+
let my_rgb_image: &mut [Srgb<u8>] = my_rgb_image.components_as_mut();
102102

103103
for color in my_rgb_image {
104104
std::mem::swap(&mut color.red, &mut color.blue);
@@ -152,13 +152,17 @@ This image shows the transition from the color to `new_color` in HSL and HSV:
152152
In addition to the operator traits, the SVG blend and composition functions have also been implemented.
153153

154154
```rust
155-
use palette::{blend::Compose, cast::ComponentsInto, Srgb, WithAlpha};
155+
use palette::{
156+
blend::Compose,
157+
cast::{ComponentsAs, ComponentsAsMut},
158+
Srgb, WithAlpha,
159+
};
156160

157161
// The input to this function could be data from image files.
158162
fn alpha_blend_images(image1: &mut [u8], image2: &[u8]) {
159163
// Convert the images into `&mut [Srgb<u8>]` and `&[Srgb<u8>]` without copying.
160-
let image1: &mut [Srgb<u8>] = image1.components_into();
161-
let image2: &[Srgb<u8>] = image2.components_into();
164+
let image1: &mut [Srgb<u8>] = image1.components_as_mut();
165+
let image2: &[Srgb<u8>] = image2.components_as();
162166

163167
for (color1, color2) in image1.iter_mut().zip(image2) {
164168
// Convert the colors to linear floating point format and give them transparency values.

palette/examples/readme_examples.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ fn converting() {
2626
}
2727

2828
fn pixels_and_buffers() {
29-
use palette::{cast::ComponentsInto, Srgb};
29+
use palette::{cast::ComponentsAsMut, Srgb};
3030

3131
// The input to this function could be data from an image file or
3232
// maybe a texture in a game.
3333
fn swap_red_and_blue(my_rgb_image: &mut [u8]) {
3434
// Convert `my_rgb_image` into `&mut [Srgb<u8>]` without copying.
35-
let my_rgb_image: &mut [Srgb<u8>] = my_rgb_image.components_into();
35+
let my_rgb_image: &mut [Srgb<u8>] = my_rgb_image.components_as_mut();
3636

3737
for color in my_rgb_image {
3838
std::mem::swap(&mut color.red, &mut color.blue);
@@ -91,13 +91,17 @@ fn color_operations_1() {
9191
}
9292

9393
fn color_operations_2() {
94-
use palette::{blend::Compose, cast::ComponentsInto, Srgb, WithAlpha};
94+
use palette::{
95+
blend::Compose,
96+
cast::{ComponentsAs, ComponentsAsMut},
97+
Srgb, WithAlpha,
98+
};
9599

96100
// The input to this function could be data from image files.
97101
fn alpha_blend_images(image1: &mut [u8], image2: &[u8]) {
98102
// Convert the images into `&mut [Srgb<u8>]` and `&[Srgb<u8>]` without copying.
99-
let image1: &mut [Srgb<u8>] = image1.components_into();
100-
let image2: &[Srgb<u8>] = image2.components_into();
103+
let image1: &mut [Srgb<u8>] = image1.components_as_mut();
104+
let image2: &[Srgb<u8>] = image2.components_as();
101105

102106
for (color1, color2) in image1.iter_mut().zip(image2) {
103107
// Convert the colors to linear floating point format and give them transparency values.

palette/examples/struct_of_arrays.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use palette::{cast::ComponentsInto, color_difference::EuclideanDistance, IntoColor, Oklab, Srgb};
1+
use palette::{cast::ComponentsAs, color_difference::EuclideanDistance, IntoColor, Oklab, Srgb};
22

33
fn main() {
44
let image = image::open("example-data/input/fruits.png")
55
.expect("could not open 'example-data/input/fruits.png'")
66
.to_rgb8();
77

8-
let image: &[Srgb<u8>] = image.as_raw().components_into();
8+
let image: &[Srgb<u8>] = image.components_as();
99

1010
// Convert and collect the colors in a struct-of-arrays (SoA) format, where
1111
// each component is a Vec of all the pixels' component values.

palette/src/cast.rs

+36-26
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@
3333
//! same after casting.
3434
//!
3535
//! ```
36-
//! use palette::{cast::{self, ArraysInto}, Srgb, IntoColor};
36+
//! use palette::{cast::{self, ArraysAsMut}, Srgb, IntoColor};
3737
//!
3838
//! let color = cast::from_array::<Srgb<u8>>([23, 198, 76]).into_linear();
3939
//! // Note: `Srgb::<u8>::from([23, 198, 76])` works too.
4040
//!
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();
4343
//!
4444
//! for destination in color_buffer {
4545
//! let linear_dst = destination.into_linear::<f32>();
4646
//! *destination = (linear_dst + color).into_encoding();
4747
//! }
4848
//! ```
4949
//!
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:
5151
//!
5252
//! ```compile_fail
5353
//! use palette::{cast, Srgb};
@@ -68,35 +68,37 @@
6868
//! or multiplying the length.
6969
//!
7070
//! ```
71-
//! use palette::{cast::{self, TryFromComponents}, Srgb};
71+
//! use palette::{cast::{self, TryComponentsAs}, Srgb};
7272
//!
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());
7576
//!
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());
7880
//! ```
7981
//!
8082
//! 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.
8385
//!
8486
//! This works:
8587
//!
8688
//! ```
87-
//! use palette::{cast::ComponentsInto, Srgb};
89+
//! use palette::{cast::ComponentsAs, Srgb};
8890
//!
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();
9193
//! ```
9294
//!
9395
//! But this panics:
9496
//!
9597
//! ```should_panic
96-
//! use palette::{cast::ComponentsInto, Srgb};
98+
//! use palette::{cast::ComponentsAs, Srgb};
9799
//!
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();
100102
//! ```
101103
//!
102104
//! ## Casting Single Colors
@@ -128,10 +130,10 @@
128130
//!
129131
//! ```
130132
//! // `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};
132134
//!
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();
135137
//!
136138
//! // Notice how the alpha values have moved from the beginning to the end:
137139
//! assert_eq!(Srgba::from(colors[0]), Srgba::new(0.8, 0.2, 0.3, 1.0));
@@ -152,20 +154,28 @@
152154
//!
153155
//! ```
154156
//! // `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};
156158
//!
157-
//! let raw = &[0xFF7F0080u32, 0xFF60BBCC];
158-
//! let colors: &[PackedArgb] = raw.uints_into();
159+
//! let raw = [0xFF7F0080u32, 0xFF60BBCC];
160+
//! let colors: &[PackedArgb] = raw.uints_as();
159161
//!
160162
//! assert_eq!(colors.len(), 2);
161163
//! assert_eq!(Srgba::from(colors[0]), Srgba::new(0x7F, 0x00, 0x80, 0xFF));
162164
//! assert_eq!(Srgba::from(colors[1]), Srgba::new(0x60, 0xBB, 0xCC, 0xFF));
163165
//! ```
164166
165167
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;
167174
mod packed;
168175
mod uint;
169-
mod uint_traits;
170176

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

Comments
 (0)