Upgrade palette dependency

This commit is contained in:
Clark Moody 2023-05-25 13:59:58 -05:00 committed by Héctor Ramón Jiménez
parent c61a4cc21c
commit 2d21d0900e
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
6 changed files with 63 additions and 57 deletions

View file

@ -14,7 +14,7 @@ log = "0.4.17"
twox-hash = { version = "1.5", default-features = false } twox-hash = { version = "1.5", default-features = false }
[dependencies.palette] [dependencies.palette]
version = "0.6" version = "0.7"
optional = true optional = true
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]

View file

@ -1,5 +1,5 @@
#[cfg(feature = "palette")] #[cfg(feature = "palette")]
use palette::rgb::{Srgb, Srgba}; use palette::rgb::{Rgb, Rgba};
/// A color in the sRGB color space. /// A color in the sRGB color space.
#[derive(Debug, Clone, Copy, PartialEq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Default)]
@ -183,34 +183,34 @@ macro_rules! color {
} }
#[cfg(feature = "palette")] #[cfg(feature = "palette")]
/// Converts from palette's `Srgba` type to a [`Color`]. /// Converts from palette's `Rgba` type to a [`Color`].
impl From<Srgba> for Color { impl From<Rgba> for Color {
fn from(srgba: Srgba) -> Self { fn from(rgba: Rgba) -> Self {
Color::new(srgba.red, srgba.green, srgba.blue, srgba.alpha) Color::new(rgba.red, rgba.green, rgba.blue, rgba.alpha)
} }
} }
#[cfg(feature = "palette")] #[cfg(feature = "palette")]
/// Converts from [`Color`] to palette's `Srgba` type. /// Converts from [`Color`] to palette's `Rgba` type.
impl From<Color> for Srgba { impl From<Color> for Rgba {
fn from(c: Color) -> Self { fn from(c: Color) -> Self {
Srgba::new(c.r, c.g, c.b, c.a) Rgba::new(c.r, c.g, c.b, c.a)
} }
} }
#[cfg(feature = "palette")] #[cfg(feature = "palette")]
/// Converts from palette's `Srgb` type to a [`Color`]. /// Converts from palette's `Rgb` type to a [`Color`].
impl From<Srgb> for Color { impl From<Rgb> for Color {
fn from(srgb: Srgb) -> Self { fn from(rgb: Rgb) -> Self {
Color::new(srgb.red, srgb.green, srgb.blue, 1.0) Color::new(rgb.red, rgb.green, rgb.blue, 1.0)
} }
} }
#[cfg(feature = "palette")] #[cfg(feature = "palette")]
/// Converts from [`Color`] to palette's `Srgb` type. /// Converts from [`Color`] to palette's `Rgb` type.
impl From<Color> for Srgb { impl From<Color> for Rgb {
fn from(c: Color) -> Self { fn from(c: Color) -> Self {
Srgb::new(c.r, c.g, c.b) Rgb::new(c.r, c.g, c.b)
} }
} }
@ -218,13 +218,13 @@ impl From<Color> for Srgb {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use palette::Blend; use palette::blend::Blend;
#[test] #[test]
fn srgba_traits() { fn srgba_traits() {
let c = Color::from_rgb(0.5, 0.4, 0.3); let c = Color::from_rgb(0.5, 0.4, 0.3);
// Round-trip conversion to the palette:Srgba type // Round-trip conversion to the palette:Rgba type
let s: Srgba = c.into(); let s: Rgba = c.into();
let r: Color = s.into(); let r: Color = s.into();
assert_eq!(c, r); assert_eq!(c, r);
} }
@ -235,14 +235,14 @@ mod tests {
let c2 = Color::from_rgb(0.2, 0.5, 0.3); let c2 = Color::from_rgb(0.2, 0.5, 0.3);
// Convert to linear color for manipulation // Convert to linear color for manipulation
let l1 = Srgba::from(c1).into_linear(); let l1 = Rgba::from(c1).into_linear();
let l2 = Srgba::from(c2).into_linear(); let l2 = Rgba::from(c2).into_linear();
// Take the lighter of each of the RGB components // Take the lighter of each of the RGB components
let lighter = l1.lighten(l2); let lighter = l1.lighten(l2);
// Convert back to our Color // Convert back to our Color
let r: Color = Srgba::from_linear(lighter).into(); let r: Color = Rgba::from_linear(lighter).into();
assert_eq!( assert_eq!(
r, r,
Color { Color {

View file

@ -7,4 +7,4 @@ publish = false
[dependencies] [dependencies]
iced = { path = "../..", features = ["canvas", "palette"] } iced = { path = "../..", features = ["canvas", "palette"] }
palette = "0.6.0" palette = "0.7.0"

View file

@ -4,7 +4,9 @@ use iced::{
alignment, Alignment, Color, Element, Length, Point, Rectangle, Renderer, alignment, Alignment, Color, Element, Length, Point, Rectangle, Renderer,
Sandbox, Settings, Size, Vector, Sandbox, Settings, Size, Vector,
}; };
use palette::{self, convert::FromColor, Hsl, Srgb}; use palette::{
self, convert::FromColor, rgb::Rgb, Darken, Hsl, Lighten, ShiftHue,
};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
@ -49,12 +51,12 @@ impl Sandbox for ColorPalette {
fn update(&mut self, message: Message) { fn update(&mut self, message: Message) {
let srgb = match message { let srgb = match message {
Message::RgbColorChanged(rgb) => palette::Srgb::from(rgb), Message::RgbColorChanged(rgb) => Rgb::from(rgb),
Message::HslColorChanged(hsl) => palette::Srgb::from_color(hsl), Message::HslColorChanged(hsl) => Rgb::from_color(hsl),
Message::HsvColorChanged(hsv) => palette::Srgb::from_color(hsv), Message::HsvColorChanged(hsv) => Rgb::from_color(hsv),
Message::HwbColorChanged(hwb) => palette::Srgb::from_color(hwb), Message::HwbColorChanged(hwb) => Rgb::from_color(hwb),
Message::LabColorChanged(lab) => palette::Srgb::from_color(lab), Message::LabColorChanged(lab) => Rgb::from_color(lab),
Message::LchColorChanged(lch) => palette::Srgb::from_color(lch), Message::LchColorChanged(lch) => Rgb::from_color(lch),
}; };
self.theme = Theme::new(srgb); self.theme = Theme::new(srgb);
@ -63,7 +65,7 @@ impl Sandbox for ColorPalette {
fn view(&self) -> Element<Message> { fn view(&self) -> Element<Message> {
let base = self.theme.base; let base = self.theme.base;
let srgb = palette::Srgb::from(base); let srgb = Rgb::from(base);
let hsl = palette::Hsl::from_color(srgb); let hsl = palette::Hsl::from_color(srgb);
let hsv = palette::Hsv::from_color(srgb); let hsv = palette::Hsv::from_color(srgb);
let hwb = palette::Hwb::from_color(srgb); let hwb = palette::Hwb::from_color(srgb);
@ -95,12 +97,10 @@ struct Theme {
impl Theme { impl Theme {
pub fn new(base: impl Into<Color>) -> Theme { pub fn new(base: impl Into<Color>) -> Theme {
use palette::{Hue, Shade};
let base = base.into(); let base = base.into();
// Convert to HSL color for manipulation // Convert to HSL color for manipulation
let hsl = Hsl::from_color(Srgb::from(base)); let hsl = Hsl::from_color(Rgb::from(base));
let lower = [ let lower = [
hsl.shift_hue(-135.0).lighten(0.075), hsl.shift_hue(-135.0).lighten(0.075),
@ -119,12 +119,12 @@ impl Theme {
Theme { Theme {
lower: lower lower: lower
.iter() .iter()
.map(|&color| Srgb::from_color(color).into()) .map(|&color| Rgb::from_color(color).into())
.collect(), .collect(),
base, base,
higher: higher higher: higher
.iter() .iter()
.map(|&color| Srgb::from_color(color).into()) .map(|&color| Rgb::from_color(color).into())
.collect(), .collect(),
canvas_cache: canvas::Cache::default(), canvas_cache: canvas::Cache::default(),
} }
@ -209,14 +209,14 @@ impl Theme {
text.vertical_alignment = alignment::Vertical::Bottom; text.vertical_alignment = alignment::Vertical::Bottom;
let hsl = Hsl::from_color(Srgb::from(self.base)); let hsl = Hsl::from_color(Rgb::from(self.base));
for i in 0..self.len() { for i in 0..self.len() {
let pct = (i as f32 + 1.0) / (self.len() as f32 + 1.0); let pct = (i as f32 + 1.0) / (self.len() as f32 + 1.0);
let graded = Hsl { let graded = Hsl {
lightness: 1.0 - pct, lightness: 1.0 - pct,
..hsl ..hsl
}; };
let color: Color = Srgb::from_color(graded).into(); let color: Color = Rgb::from_color(graded).into();
let anchor = Point { let anchor = Point {
x: (i as f32) * box_size.width, x: (i as f32) * box_size.width,
@ -352,7 +352,7 @@ impl ColorSpace for palette::Hsl {
fn components(&self) -> [f32; 3] { fn components(&self) -> [f32; 3] {
[ [
self.hue.to_positive_degrees(), self.hue.into_positive_degrees(),
self.saturation, self.saturation,
self.lightness, self.lightness,
] ]
@ -361,7 +361,7 @@ impl ColorSpace for palette::Hsl {
fn to_string(&self) -> String { fn to_string(&self) -> String {
format!( format!(
"hsl({:.1}, {:.1}%, {:.1}%)", "hsl({:.1}, {:.1}%, {:.1}%)",
self.hue.to_positive_degrees(), self.hue.into_positive_degrees(),
100.0 * self.saturation, 100.0 * self.saturation,
100.0 * self.lightness 100.0 * self.lightness
) )
@ -378,13 +378,17 @@ impl ColorSpace for palette::Hsv {
} }
fn components(&self) -> [f32; 3] { fn components(&self) -> [f32; 3] {
[self.hue.to_positive_degrees(), self.saturation, self.value] [
self.hue.into_positive_degrees(),
self.saturation,
self.value,
]
} }
fn to_string(&self) -> String { fn to_string(&self) -> String {
format!( format!(
"hsv({:.1}, {:.1}%, {:.1}%)", "hsv({:.1}, {:.1}%, {:.1}%)",
self.hue.to_positive_degrees(), self.hue.into_positive_degrees(),
100.0 * self.saturation, 100.0 * self.saturation,
100.0 * self.value 100.0 * self.value
) )
@ -406,7 +410,7 @@ impl ColorSpace for palette::Hwb {
fn components(&self) -> [f32; 3] { fn components(&self) -> [f32; 3] {
[ [
self.hue.to_positive_degrees(), self.hue.into_positive_degrees(),
self.whiteness, self.whiteness,
self.blackness, self.blackness,
] ]
@ -415,7 +419,7 @@ impl ColorSpace for palette::Hwb {
fn to_string(&self) -> String { fn to_string(&self) -> String {
format!( format!(
"hwb({:.1}, {:.1}%, {:.1}%)", "hwb({:.1}, {:.1}%, {:.1}%)",
self.hue.to_positive_degrees(), self.hue.into_positive_degrees(),
100.0 * self.whiteness, 100.0 * self.whiteness,
100.0 * self.blackness 100.0 * self.blackness
) )
@ -450,7 +454,7 @@ impl ColorSpace for palette::Lch {
} }
fn components(&self) -> [f32; 3] { fn components(&self) -> [f32; 3] {
[self.l, self.chroma, self.hue.to_positive_degrees()] [self.l, self.chroma, self.hue.into_positive_degrees()]
} }
fn to_string(&self) -> String { fn to_string(&self) -> String {
@ -458,7 +462,7 @@ impl ColorSpace for palette::Lch {
"Lch({:.1}, {:.1}, {:.1})", "Lch({:.1}, {:.1}, {:.1})",
self.l, self.l,
self.chroma, self.chroma,
self.hue.to_positive_degrees() self.hue.into_positive_degrees()
) )
} }
} }

View file

@ -16,7 +16,7 @@ path = "../core"
features = ["palette"] features = ["palette"]
[dependencies.palette] [dependencies.palette]
version = "0.6" version = "0.7"
[dependencies.once_cell] [dependencies.once_cell]
version = "1.15" version = "1.15"

View file

@ -2,7 +2,9 @@
use iced_core::Color; use iced_core::Color;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use palette::{FromColor, Hsl, Mix, RelativeContrast, Srgb}; use palette::color_difference::Wcag21RelativeContrast;
use palette::rgb::Rgb;
use palette::{FromColor, Hsl, Mix};
/// A color palette. /// A color palette.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
@ -298,11 +300,11 @@ fn deviate(color: Color, amount: f32) -> Color {
} }
fn mix(a: Color, b: Color, factor: f32) -> Color { fn mix(a: Color, b: Color, factor: f32) -> Color {
let a_lin = Srgb::from(a).into_linear(); let a_lin = Rgb::from(a).into_linear();
let b_lin = Srgb::from(b).into_linear(); let b_lin = Rgb::from(b).into_linear();
let mixed = a_lin.mix(&b_lin, factor); let mixed = a_lin.mix(b_lin, factor);
Srgb::from_linear(mixed).into() Rgb::from_linear(mixed).into()
} }
fn readable(background: Color, text: Color) -> Color { fn readable(background: Color, text: Color) -> Color {
@ -320,16 +322,16 @@ fn is_dark(color: Color) -> bool {
} }
fn is_readable(a: Color, b: Color) -> bool { fn is_readable(a: Color, b: Color) -> bool {
let a_srgb = Srgb::from(a); let a_srgb = Rgb::from(a);
let b_srgb = Srgb::from(b); let b_srgb = Rgb::from(b);
a_srgb.has_enhanced_contrast_text(&b_srgb) a_srgb.has_enhanced_contrast_text(b_srgb)
} }
fn to_hsl(color: Color) -> Hsl { fn to_hsl(color: Color) -> Hsl {
Hsl::from_color(Srgb::from(color)) Hsl::from_color(Rgb::from(color))
} }
fn from_hsl(hsl: Hsl) -> Color { fn from_hsl(hsl: Hsl) -> Color {
Srgb::from_color(hsl).into() Rgb::from_color(hsl).into()
} }