Skip to content

Commit 4717c56

Browse files
authored
Merge 4215bb8 into fa94527
2 parents fa94527 + 4215bb8 commit 4717c56

39 files changed

+1804
-880
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
os: [ubuntu-latest, macos-latest, windows-latest]
18-
toolchain: [1.48.0, stable, beta, nightly]
18+
toolchain: [1.51.0, stable, beta, nightly]
1919
runs-on: ${{ matrix.os }}
2020
env:
2121
RUSTFLAGS: -D warnings

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A color management and conversion library that focuses on maintaining correctnes
1515

1616
## Minimum Supported Rust Version (MSRV)
1717

18-
This version of Palette has been automatically tested with Rust version `1.48.0` and the `stable`, `beta`, and `nightly` channels. Future versions of the library may advance the minimum supported version to make use of new language features, but this will be considered a breaking change.
18+
This version of Palette has been automatically tested with Rust version `1.51.0` and the `stable`, `beta`, and `nightly` channels. Future versions of the library may advance the minimum supported version to make use of new language features, but this will be considered a breaking change.
1919

2020
## Contributing
2121

palette/README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A color management and conversion library that focuses on maintaining correctnes
1616

1717
## Minimum Supported Rust Version (MSRV)
1818

19-
This version of Palette has been automatically tested with Rust version `1.48.0` and the `stable`, `beta`, and `nightly` channels. Future versions of the library may advance the minimum supported version to make use of new language features, but this will be considered a breaking change.
19+
This version of Palette has been automatically tested with Rust version `1.51.0` and the `stable`, `beta`, and `nightly` channels. Future versions of the library may advance the minimum supported version to make use of new language features, but this will be considered a breaking change.
2020

2121
## Getting Started
2222

@@ -93,16 +93,16 @@ A longer and more advanced example that shows how to implement the conversion tr
9393

9494
### Pixels And Buffers
9595

96-
When working with image or pixel buffers, or any color type that can be converted to a slice of components (ex. `&[u8]`), the `Pixel` trait provides methods for turning them into slices of Palette colors without cloning the whole buffer:
96+
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 functions for turning them into slices of Palette colors without cloning the whole buffer:
9797

9898
```rust
99-
use palette::{Srgb, Pixel};
99+
use palette::{cast, Srgb};
100100

101101
// The input to this function could be data from an image file or
102102
// maybe a texture in a game.
103103
fn swap_red_and_blue(my_rgb_image: &mut [u8]) {
104104
// Convert `my_rgb_image` into `&mut [Srgb<u8>]` without copying.
105-
let my_rgb_image = Srgb::from_raw_slice_mut(my_rgb_image);
105+
let my_rgb_image: &mut [Srgb<u8>] = cast::from_component_slice_mut(my_rgb_image);
106106

107107
for color in my_rgb_image {
108108
std::mem::swap(&mut color.red, &mut color.blue);
@@ -117,10 +117,10 @@ fn swap_red_and_blue(my_rgb_image: &mut [u8]) {
117117
It's also possible to create a single color from a slice or array. Let's say we are using something that implements `AsMut<[u8; 3]>`:
118118

119119
```rust
120-
use palette::{Srgb, Pixel};
120+
use palette::Srgb;
121121

122122
fn swap_red_and_blue(mut my_rgb: impl AsMut<[u8; 3]>) {
123-
let my_rgb = Srgb::from_raw_mut(my_rgb.as_mut());
123+
let my_rgb: &mut Srgb<u8> = my_rgb.as_mut().into();
124124

125125
std::mem::swap(&mut my_rgb.red, &mut my_rgb.blue);
126126
}
@@ -156,13 +156,13 @@ This image shows the transition from the color to `new_color` in HSL and HSV:
156156
In addition to the operator traits, the SVG blend functions have also been implemented.
157157

158158
```rust
159-
use palette::{blend::Blend, Pixel, Srgb, WithAlpha};
159+
use palette::{blend::Blend, cast, Srgb, WithAlpha};
160160

161161
// The input to this function could be data from image files.
162162
fn alpha_blend_images(image1: &mut [u8], image2: &[u8]) {
163163
// Convert the images into `&mut [Srgb<u8>]` and `&[Srgb<u8>]` without copying.
164-
let image1 = Srgb::from_raw_slice_mut(image1);
165-
let image2 = Srgb::from_raw_slice(image2);
164+
let image1: &mut [Srgb<u8>] = cast::from_component_slice_mut(image1);
165+
let image2: &[Srgb<u8>] = cast::from_component_slice(image2);
166166

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

palette/examples/color_scheme.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use palette::{Darken, IntoColor, Lch, Lighten, LinSrgb, Pixel, ShiftHue, Srgb};
1+
use palette::{Darken, IntoColor, Lch, Lighten, LinSrgb, ShiftHue, Srgb};
22

33
use image::{GenericImage, GenericImageView, RgbImage, SubImage};
44

@@ -170,18 +170,18 @@ fn blit_shades(color: LinSrgb<f32>, mut canvas: SubImage<&mut RgbImage>) {
170170
let width = canvas.width();
171171
let height = canvas.height();
172172

173-
let primary = Srgb::from_linear(color).into_format().into_raw();
173+
let primary = Srgb::from_linear(color).into_format().into();
174174

175175
//Generate one lighter and two darker versions of the color
176176
let light = Srgb::from_linear(color.lighten(0.1).into())
177177
.into_format()
178-
.into_raw();
178+
.into();
179179
let dark1 = Srgb::from_linear(color.darken(0.1).into())
180180
.into_format()
181-
.into_raw();
181+
.into();
182182
let dark2 = Srgb::from_linear(color.darken(0.2).into())
183183
.into_format()
184-
.into_raw();
184+
.into();
185185

186186
for x in 0..width {
187187
for y in 0..height {

palette/examples/gradient.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55

66
#[cfg(feature = "std")]
77
fn main() {
8-
use palette::{FromColor, Gradient, IntoColor, Lch, LinSrgb, Pixel, Srgb};
8+
use palette::{FromColor, Gradient, IntoColor, Lch, LinSrgb, Srgb};
99

1010
use image::{GenericImage, GenericImageView, RgbImage};
1111

@@ -47,10 +47,10 @@ fn main() {
4747
.zip(grad3.take(256).zip(grad4.take(256)))
4848
.enumerate()
4949
{
50-
let c1 = Srgb::from_linear(c1).into_format().into_raw();
51-
let c2 = Srgb::from_linear(c2).into_format().into_raw();
52-
let c3 = Srgb::from_linear(c3.into_color()).into_format().into_raw();
53-
let c4 = Srgb::from_linear(c4.into_color()).into_format().into_raw();
50+
let c1 = Srgb::from_linear(c1).into_format().into();
51+
let c2 = Srgb::from_linear(c2).into_format().into();
52+
let c3 = Srgb::from_linear(c3.into_color()).into_format().into();
53+
let c4 = Srgb::from_linear(c4.into_color()).into_format().into();
5454

5555
{
5656
let mut sub_image = image.sub_image(i as u32, 0, 1, 31);

palette/examples/hue.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use palette::{FromColor, Hsl, Lch, Pixel, ShiftHue, Srgb};
1+
use palette::{FromColor, Hsl, Lch, ShiftHue, Srgb};
22

33
fn main() {
44
let mut image = image::open("example-data/input/fruits.png")
@@ -9,14 +9,14 @@ fn main() {
99
//right part. Notice how LCh manages to preserve the apparent lightness of
1010
//of the colors, compared to the original.
1111
for (x, y, pixel) in image.enumerate_pixels_mut() {
12-
let color = Srgb::from_raw(&pixel.0).into_format();
12+
let color = Srgb::from(pixel.0).into_format();
1313

1414
pixel.0 = if x < y {
1515
let hue_shifted = Hsl::from_color(color).shift_hue(180.0);
16-
Srgb::from_color(hue_shifted).into_format().into_raw()
16+
Srgb::from_color(hue_shifted).into_format().into()
1717
} else {
1818
let hue_shifted = Lch::from_color(color).shift_hue(180.0);
19-
Srgb::from_color(hue_shifted).into_format().into_raw()
19+
Srgb::from_color(hue_shifted).into_format().into()
2020
};
2121
}
2222

palette/examples/random.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55

66
#[cfg(feature = "random")]
77
fn main() {
8-
use palette::{Hsl, Hsv, Hwb, IntoColor, Pixel, RgbHue, Srgb};
8+
use palette::{Hsl, Hsv, Hwb, IntoColor, RgbHue, Srgb};
99

1010
use image::{GenericImage, GenericImageView, RgbImage};
1111
use rand::Rng;
@@ -20,7 +20,7 @@ fn main() {
2020
for x in 0..width {
2121
for y in 0..height {
2222
let random_color: Srgb = Srgb::new(rng.gen(), rng.gen(), rng.gen());
23-
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
23+
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into()));
2424
}
2525
}
2626
}
@@ -31,7 +31,7 @@ fn main() {
3131
for x in 0..width {
3232
for y in 0..height {
3333
let random_color: Srgb = rng.gen();
34-
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
34+
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into()));
3535
}
3636
}
3737
}
@@ -44,7 +44,7 @@ fn main() {
4444
for y in 0..height {
4545
let random_color: Srgb =
4646
Hsv::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()).into_color();
47-
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
47+
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into()));
4848
}
4949
}
5050
}
@@ -55,7 +55,7 @@ fn main() {
5555
for x in 0..width {
5656
for y in 0..height {
5757
let random_color: Srgb = rng.gen::<Hsv>().into_color();
58-
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
58+
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into()));
5959
}
6060
}
6161
}
@@ -68,7 +68,7 @@ fn main() {
6868
for y in 0..height {
6969
let random_color: Srgb =
7070
Hsl::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()).into_color();
71-
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
71+
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into()));
7272
}
7373
}
7474
}
@@ -79,7 +79,7 @@ fn main() {
7979
for x in 0..width {
8080
for y in 0..height {
8181
let random_color: Srgb = rng.gen::<Hsl>().into_color();
82-
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
82+
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into()));
8383
}
8484
}
8585
}
@@ -92,7 +92,7 @@ fn main() {
9292
for y in 0..height {
9393
let random_color: Srgb =
9494
Hwb::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()).into_color();
95-
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
95+
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into()));
9696
}
9797
}
9898
}
@@ -103,7 +103,7 @@ fn main() {
103103
for x in 0..width {
104104
for y in 0..height {
105105
let random_color: Srgb = rng.gen::<Hwb>().into_color();
106-
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
106+
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into()));
107107
}
108108
}
109109
}

palette/examples/readme_examples.rs

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

2828
fn pixels_and_buffers() {
29-
use palette::{Pixel, Srgb};
29+
use palette::{cast, 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 = Srgb::from_raw_slice_mut(my_rgb_image);
35+
let my_rgb_image: &mut [Srgb<u8>] = cast::from_component_slice_mut(my_rgb_image);
3636

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

9393
fn color_operations_2() {
94-
use palette::{blend::Blend, Pixel, Srgb, WithAlpha};
94+
use palette::{blend::Blend, cast, Srgb, WithAlpha};
9595

9696
// The input to this function could be data from image files.
9797
fn alpha_blend_images(image1: &mut [u8], image2: &[u8]) {
9898
// Convert the images into `&mut [Srgb<u8>]` and `&[Srgb<u8>]` without copying.
99-
let image1 = Srgb::from_raw_slice_mut(image1);
100-
let image2 = Srgb::from_raw_slice(image2);
99+
let image1: &mut [Srgb<u8>] = cast::from_component_slice_mut(image1);
100+
let image2: &[Srgb<u8>] = cast::from_component_slice(image2);
101101

102102
for (color1, color2) in image1.iter_mut().zip(image2) {
103103
// Convert the colors to linear floating point format and give them transparency values.
@@ -216,8 +216,6 @@ fn display_colors(filename: &str, displays: &[DisplayType]) {
216216
}
217217

218218
fn display_discrete(mut image: SubImage<&mut RgbImage>, colors: &[palette::Srgb<u8>]) {
219-
use palette::Pixel;
220-
221219
let (width, height) = image.dimensions();
222220
let swatch_size = width as f32 / colors.len() as f32;
223221
for (i, &color) in colors.iter().enumerate() {
@@ -227,7 +225,7 @@ fn display_discrete(mut image: SubImage<&mut RgbImage>, colors: &[palette::Srgb<
227225
let (width, height) = sub_image.dimensions();
228226
for x in 0..width {
229227
for y in 0..height {
230-
sub_image.put_pixel(x, y, image::Rgb(*color.as_raw()));
228+
sub_image.put_pixel(x, y, image::Rgb(color.into()));
231229
}
232230
}
233231
}
@@ -237,16 +235,10 @@ fn display_continuous(
237235
mut image: SubImage<&mut RgbImage>,
238236
color_at: &dyn Fn(f32) -> palette::Srgb<u8>,
239237
) {
240-
use palette::Pixel;
241-
242238
let (width, height) = image.dimensions();
243239
for x in 0..width {
244240
for y in 0..height {
245-
image.put_pixel(
246-
x,
247-
y,
248-
image::Rgb(color_at(x as f32 / width as f32).into_raw()),
249-
);
241+
image.put_pixel(x, y, image::Rgb(color_at(x as f32 / width as f32).into()));
250242
}
251243
}
252244
}

palette/examples/saturate.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use palette::{FromColor, Hsl, IntoColor, Lch, Pixel, Saturate, Srgb};
1+
use palette::{FromColor, Hsl, IntoColor, Lch, Saturate, Srgb};
22

33
use image::{GenericImage, GenericImageView};
44

@@ -17,15 +17,15 @@ fn main() {
1717
let (width, height) = sub_image.dimensions();
1818
for x in 0..width {
1919
for y in 0..height {
20-
let color: Hsl = Srgb::from_raw(&sub_image.get_pixel(x, y).0)
20+
let color: Hsl = Srgb::from(sub_image.get_pixel(x, y).0)
2121
.into_format()
2222
.into_color();
2323

2424
let saturated = color.saturate(0.3);
2525
sub_image.put_pixel(
2626
x,
2727
y,
28-
image::Rgb(Srgb::from_color(saturated).into_format().into_raw()),
28+
image::Rgb(Srgb::from_color(saturated).into_format().into()),
2929
);
3030
}
3131
}
@@ -36,15 +36,15 @@ fn main() {
3636
let (width, height) = sub_image.dimensions();
3737
for x in 0..width {
3838
for y in 0..height {
39-
let color: Lch = Srgb::from_raw(&sub_image.get_pixel(x, y).0)
39+
let color: Lch = Srgb::from(sub_image.get_pixel(x, y).0)
4040
.into_format()
4141
.into_color();
4242

4343
let saturated = color.saturate(0.3);
4444
sub_image.put_pixel(
4545
x,
4646
y,
47-
image::Rgb(Srgb::from_color(saturated).into_format().into_raw()),
47+
image::Rgb(Srgb::from_color(saturated).into_format().into()),
4848
);
4949
}
5050
}

palette/examples/shade.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use palette::{Darken, FromColor, Hsv, IntoColor, Lab, Lighten, LinSrgb, Pixel, Srgb};
1+
use palette::{Darken, FromColor, Hsv, IntoColor, Lab, Lighten, LinSrgb, Srgb};
22

33
use image::{GenericImage, GenericImageView, RgbImage};
44

@@ -13,10 +13,10 @@ fn main() {
1313
for i in 0..11 {
1414
let rgb1 = Srgb::from_linear(rgb.darken(0.1 * i as f32))
1515
.into_format()
16-
.into_raw();
16+
.into();
1717
let rgb2 = Srgb::from_linear(rgb.lighten(0.1 * i as f32))
1818
.into_format()
19-
.into_raw();
19+
.into();
2020

2121
{
2222
let mut sub_image = image.sub_image(i as u32 * 20, 0, 20, 31);
@@ -40,10 +40,10 @@ fn main() {
4040

4141
let lab1 = Srgb::from_linear(lab.darken(0.1 * i as f32).into_color())
4242
.into_format()
43-
.into_raw();
43+
.into();
4444
let lab2 = Srgb::from_linear(lab.lighten(0.1 * i as f32).into_color())
4545
.into_format()
46-
.into_raw();
46+
.into();
4747

4848
{
4949
let mut sub_image = image.sub_image(i as u32 * 20, 65, 20, 31);
@@ -67,10 +67,10 @@ fn main() {
6767

6868
let hsv1 = Srgb::from_linear(hsv.darken(0.1 * i as f32).into_color())
6969
.into_format()
70-
.into_raw();
70+
.into();
7171
let hsv2 = Srgb::from_linear(hsv.lighten(0.1 * i as f32).into_color())
7272
.into_format()
73-
.into_raw();
73+
.into();
7474

7575
{
7676
let mut sub_image = image.sub_image(i as u32 * 20, 130, 20, 31);

0 commit comments

Comments
 (0)