You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
176: Rewrite the conversion traits to work more like From and Into r=Ogeon a=Ogeon
This replaces the old `FromColor` and `IntoColor` traits, as well as the implementations of `From` and `Into` between color types, with new traits that are more flexible. The new `FromColor` and `IntoColor` traits will also clamp the output to prevent out-of-bounds results.
It's a _very_ breaking change. To migrate from the old system, any failing uses `from` and `into` should be replaced with `from_color` and `into_color`. Any custom color that used to implement `IntoColor` should switch to implementing `FromColorUnclamped`.
A new trait, called `Transparency`, has also been added to make it possible to implement `FromColorUnclamped` for `Alpha` without running into conflicting implementations. It makes it possible to convert from any color type that implements `Transparency`, and not just `Alpha<A, T>` to `Alpha<B, T>`, for example.
The semantics of the new traits are a bit different, so it may affect type inference. It may also be necessary to convert using more than one step. This is mostly for the RGB family and Luma, when changing RGB space or standard.
The syntax for the helper attributes were also changed to the more common `#[palette(...)]` pattern. It helped when implementing them and looks a bit nicer, IMO.
Closes#41, fixes#111.
Co-authored-by: Erik Hedvall <erikwhedvall@gmail.com>
Copy file name to clipboardexpand all lines: README.md
+11-12
Original file line number
Diff line number
Diff line change
@@ -59,15 +59,15 @@ Palette provides tools for both color manipulation and conversion between color
59
59
60
60
RGB is probably the most widely known color space, but it's not the only one. You have probably used a color picker with a rainbow wheel and a brightness slider. That may have been an HSV or an HSL color picker, where the color is encoded as hue, saturation and brightness/lightness. There's also a group of color spaces that are designed to be perceptually uniform, meaning that the perceptual change is equal to the numerical change.
61
61
62
-
Selecting the proper color space can have a big impact on how the resulting image looks (as illustrated by some of the programs in `examples`), and Palette makes the conversion between them as easy as a call to `from` or `into`.
62
+
Selecting the proper color space can have a big impact on how the resulting image looks (as illustrated by some of the programs in `examples`), and Palette makes the conversion between them as easy as a call to `from_color` or `into_color`.
63
63
64
64
This example takes an sRGB color, converts it to CIE L\*C\*h°, shifts its hue by 180° and converts it back to RGB:
@@ -78,16 +78,14 @@ This results in the following two colors:
78
78
79
79
Palette comes with a number of color manipulation tools, that are implemented as traits. These includes lighten/darken, saturate/desaturate and hue shift. These traits are only implemented on types where they are meaningful, which means that you can't shift the hue of an RGB color without converting it to a color space where it makes sense.
80
80
81
-
This may seem limiting, but the point is to avoid inconsistent behavior due to arbitrary defaults, such as saturating a gray color to red when there is no available hue information. The abstract `Color` type does still support every operation, for when this is less important.
82
-
83
-
The following example shows how the `Color` type is used to make a lighter and a desaturated version of the original.
81
+
The following example shows how to make a lighter and a desaturated version of the original.
@@ -101,16 +99,16 @@ There is also a linear gradient type which makes it easy to interpolate between
101
99
The following example shows three gradients between the same two endpoints, but the top is in RGB space while the middle and bottom are in HSV space. The bottom gradient is an example of using the color sequence iterator.
102
100
103
101
```Rust
104
-
usepalette::{LinSrgb, Hsv, Gradient};
102
+
usepalette::{FromColor, LinSrgb, Hsv, Gradient};
105
103
106
104
letgrad1=Gradient::new(vec![
107
105
LinSrgb::new(1.0, 0.1, 0.1),
108
106
LinSrgb::new(0.1, 1.0, 1.0)
109
107
]);
110
108
111
109
letgrad2=Gradient::new(vec![
112
-
Hsv::from(LinSrgb::new(1.0, 0.1, 0.1)),
113
-
Hsv::from(LinSrgb::new(0.1, 1.0, 1.0))
110
+
Hsv::from_color(LinSrgb::new(1.0, 0.1, 0.1)),
111
+
Hsv::from_color(LinSrgb::new(0.1, 1.0, 1.0))
114
112
]);
115
113
```
116
114
@@ -125,6 +123,7 @@ Palette supports converting from a raw buffer of data into a color type using th
125
123
Oftentimes, pixel data is stored in a raw buffer such as a `[u8; 3]`. `from_raw` can be used to convert into a Palette color, `into_format` converts from `Srgb<u8>` to `Srgb<f32>`, and finally `into_raw` to convert from a Palette color back to a `[u8;3]`.
126
124
127
125
Here's an example of turning a buffer of `[u8; 3]` into a Palette `Srgb` color and back to a raw buffer.
0 commit comments