Skip to content

Commit fcb1b45

Browse files
authored
Merge pull request #1875 from clarkmoody/palette-0.7
Upgrade `palette` dependency
2 parents c61a4cc + cf2c8f2 commit fcb1b45

7 files changed

Lines changed: 65 additions & 58 deletions

File tree

core/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ log = "0.4.17"
1414
twox-hash = { version = "1.5", default-features = false }
1515

1616
[dependencies.palette]
17-
version = "0.6"
17+
version = "0.7"
1818
optional = true
1919

2020
[target.'cfg(target_arch = "wasm32")'.dependencies]
2121
instant = "0.1"
22+
23+
[dev-dependencies]
24+
approx = "0.5"

core/src/color.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,31 +183,31 @@ macro_rules! color {
183183
}
184184

185185
#[cfg(feature = "palette")]
186-
/// Converts from palette's `Srgba` type to a [`Color`].
186+
/// Converts from palette's `Rgba` type to a [`Color`].
187187
impl From<Srgba> for Color {
188-
fn from(srgba: Srgba) -> Self {
189-
Color::new(srgba.red, srgba.green, srgba.blue, srgba.alpha)
188+
fn from(rgba: Srgba) -> Self {
189+
Color::new(rgba.red, rgba.green, rgba.blue, rgba.alpha)
190190
}
191191
}
192192

193193
#[cfg(feature = "palette")]
194-
/// Converts from [`Color`] to palette's `Srgba` type.
194+
/// Converts from [`Color`] to palette's `Rgba` type.
195195
impl From<Color> for Srgba {
196196
fn from(c: Color) -> Self {
197197
Srgba::new(c.r, c.g, c.b, c.a)
198198
}
199199
}
200200

201201
#[cfg(feature = "palette")]
202-
/// Converts from palette's `Srgb` type to a [`Color`].
202+
/// Converts from palette's `Rgb` type to a [`Color`].
203203
impl From<Srgb> for Color {
204-
fn from(srgb: Srgb) -> Self {
205-
Color::new(srgb.red, srgb.green, srgb.blue, 1.0)
204+
fn from(rgb: Srgb) -> Self {
205+
Color::new(rgb.red, rgb.green, rgb.blue, 1.0)
206206
}
207207
}
208208

209209
#[cfg(feature = "palette")]
210-
/// Converts from [`Color`] to palette's `Srgb` type.
210+
/// Converts from [`Color`] to palette's `Rgb` type.
211211
impl From<Color> for Srgb {
212212
fn from(c: Color) -> Self {
213213
Srgb::new(c.r, c.g, c.b)
@@ -218,39 +218,37 @@ impl From<Color> for Srgb {
218218
#[cfg(test)]
219219
mod tests {
220220
use super::*;
221-
use palette::Blend;
221+
use palette::blend::Blend;
222222

223223
#[test]
224224
fn srgba_traits() {
225225
let c = Color::from_rgb(0.5, 0.4, 0.3);
226-
// Round-trip conversion to the palette:Srgba type
226+
// Round-trip conversion to the palette::Srgba type
227227
let s: Srgba = c.into();
228228
let r: Color = s.into();
229229
assert_eq!(c, r);
230230
}
231231

232232
#[test]
233233
fn color_manipulation() {
234+
use approx::assert_relative_eq;
235+
234236
let c1 = Color::from_rgb(0.5, 0.4, 0.3);
235237
let c2 = Color::from_rgb(0.2, 0.5, 0.3);
236238

237239
// Convert to linear color for manipulation
238240
let l1 = Srgba::from(c1).into_linear();
239241
let l2 = Srgba::from(c2).into_linear();
240242

241-
// Take the lighter of each of the RGB components
243+
// Take the lighter of each of the sRGB components
242244
let lighter = l1.lighten(l2);
243245

244246
// Convert back to our Color
245-
let r: Color = Srgba::from_linear(lighter).into();
246-
assert_eq!(
247-
r,
248-
Color {
249-
r: 0.5,
250-
g: 0.5,
251-
b: 0.3,
252-
a: 1.0
253-
}
254-
);
247+
let result: Color = Srgba::from_linear(lighter).into();
248+
249+
assert_relative_eq!(result.r, 0.5);
250+
assert_relative_eq!(result.g, 0.5);
251+
assert_relative_eq!(result.b, 0.3);
252+
assert_relative_eq!(result.a, 1.0);
255253
}
256254
}

examples/checkbox/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Application for Example {
3131
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
3232
(
3333
Self::default(),
34-
font::load(include_bytes!("../fonts/icons.ttf").as_ref())
34+
font::load(include_bytes!("../fonts/icons.ttf").as_slice())
3535
.map(Message::FontLoaded),
3636
)
3737
}

examples/color_palette/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ publish = false
77

88
[dependencies]
99
iced = { path = "../..", features = ["canvas", "palette"] }
10-
palette = "0.6.0"
10+
palette = "0.7.0"

examples/color_palette/src/main.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use iced::{
44
alignment, Alignment, Color, Element, Length, Point, Rectangle, Renderer,
55
Sandbox, Settings, Size, Vector,
66
};
7-
use palette::{self, convert::FromColor, Hsl, Srgb};
7+
use palette::{
8+
self, convert::FromColor, rgb::Rgb, Darken, Hsl, Lighten, ShiftHue,
9+
};
810
use std::marker::PhantomData;
911
use std::ops::RangeInclusive;
1012

@@ -49,12 +51,12 @@ impl Sandbox for ColorPalette {
4951

5052
fn update(&mut self, message: Message) {
5153
let srgb = match message {
52-
Message::RgbColorChanged(rgb) => palette::Srgb::from(rgb),
53-
Message::HslColorChanged(hsl) => palette::Srgb::from_color(hsl),
54-
Message::HsvColorChanged(hsv) => palette::Srgb::from_color(hsv),
55-
Message::HwbColorChanged(hwb) => palette::Srgb::from_color(hwb),
56-
Message::LabColorChanged(lab) => palette::Srgb::from_color(lab),
57-
Message::LchColorChanged(lch) => palette::Srgb::from_color(lch),
54+
Message::RgbColorChanged(rgb) => Rgb::from(rgb),
55+
Message::HslColorChanged(hsl) => Rgb::from_color(hsl),
56+
Message::HsvColorChanged(hsv) => Rgb::from_color(hsv),
57+
Message::HwbColorChanged(hwb) => Rgb::from_color(hwb),
58+
Message::LabColorChanged(lab) => Rgb::from_color(lab),
59+
Message::LchColorChanged(lch) => Rgb::from_color(lch),
5860
};
5961

6062
self.theme = Theme::new(srgb);
@@ -63,7 +65,7 @@ impl Sandbox for ColorPalette {
6365
fn view(&self) -> Element<Message> {
6466
let base = self.theme.base;
6567

66-
let srgb = palette::Srgb::from(base);
68+
let srgb = Rgb::from(base);
6769
let hsl = palette::Hsl::from_color(srgb);
6870
let hsv = palette::Hsv::from_color(srgb);
6971
let hwb = palette::Hwb::from_color(srgb);
@@ -95,12 +97,10 @@ struct Theme {
9597

9698
impl Theme {
9799
pub fn new(base: impl Into<Color>) -> Theme {
98-
use palette::{Hue, Shade};
99-
100100
let base = base.into();
101101

102102
// Convert to HSL color for manipulation
103-
let hsl = Hsl::from_color(Srgb::from(base));
103+
let hsl = Hsl::from_color(Rgb::from(base));
104104

105105
let lower = [
106106
hsl.shift_hue(-135.0).lighten(0.075),
@@ -119,12 +119,12 @@ impl Theme {
119119
Theme {
120120
lower: lower
121121
.iter()
122-
.map(|&color| Srgb::from_color(color).into())
122+
.map(|&color| Rgb::from_color(color).into())
123123
.collect(),
124124
base,
125125
higher: higher
126126
.iter()
127-
.map(|&color| Srgb::from_color(color).into())
127+
.map(|&color| Rgb::from_color(color).into())
128128
.collect(),
129129
canvas_cache: canvas::Cache::default(),
130130
}
@@ -209,14 +209,14 @@ impl Theme {
209209

210210
text.vertical_alignment = alignment::Vertical::Bottom;
211211

212-
let hsl = Hsl::from_color(Srgb::from(self.base));
212+
let hsl = Hsl::from_color(Rgb::from(self.base));
213213
for i in 0..self.len() {
214214
let pct = (i as f32 + 1.0) / (self.len() as f32 + 1.0);
215215
let graded = Hsl {
216216
lightness: 1.0 - pct,
217217
..hsl
218218
};
219-
let color: Color = Srgb::from_color(graded).into();
219+
let color: Color = Rgb::from_color(graded).into();
220220

221221
let anchor = Point {
222222
x: (i as f32) * box_size.width,
@@ -352,7 +352,7 @@ impl ColorSpace for palette::Hsl {
352352

353353
fn components(&self) -> [f32; 3] {
354354
[
355-
self.hue.to_positive_degrees(),
355+
self.hue.into_positive_degrees(),
356356
self.saturation,
357357
self.lightness,
358358
]
@@ -361,7 +361,7 @@ impl ColorSpace for palette::Hsl {
361361
fn to_string(&self) -> String {
362362
format!(
363363
"hsl({:.1}, {:.1}%, {:.1}%)",
364-
self.hue.to_positive_degrees(),
364+
self.hue.into_positive_degrees(),
365365
100.0 * self.saturation,
366366
100.0 * self.lightness
367367
)
@@ -378,13 +378,17 @@ impl ColorSpace for palette::Hsv {
378378
}
379379

380380
fn components(&self) -> [f32; 3] {
381-
[self.hue.to_positive_degrees(), self.saturation, self.value]
381+
[
382+
self.hue.into_positive_degrees(),
383+
self.saturation,
384+
self.value,
385+
]
382386
}
383387

384388
fn to_string(&self) -> String {
385389
format!(
386390
"hsv({:.1}, {:.1}%, {:.1}%)",
387-
self.hue.to_positive_degrees(),
391+
self.hue.into_positive_degrees(),
388392
100.0 * self.saturation,
389393
100.0 * self.value
390394
)
@@ -406,7 +410,7 @@ impl ColorSpace for palette::Hwb {
406410

407411
fn components(&self) -> [f32; 3] {
408412
[
409-
self.hue.to_positive_degrees(),
413+
self.hue.into_positive_degrees(),
410414
self.whiteness,
411415
self.blackness,
412416
]
@@ -415,7 +419,7 @@ impl ColorSpace for palette::Hwb {
415419
fn to_string(&self) -> String {
416420
format!(
417421
"hwb({:.1}, {:.1}%, {:.1}%)",
418-
self.hue.to_positive_degrees(),
422+
self.hue.into_positive_degrees(),
419423
100.0 * self.whiteness,
420424
100.0 * self.blackness
421425
)
@@ -450,15 +454,15 @@ impl ColorSpace for palette::Lch {
450454
}
451455

452456
fn components(&self) -> [f32; 3] {
453-
[self.l, self.chroma, self.hue.to_positive_degrees()]
457+
[self.l, self.chroma, self.hue.into_positive_degrees()]
454458
}
455459

456460
fn to_string(&self) -> String {
457461
format!(
458462
"Lch({:.1}, {:.1}, {:.1})",
459463
self.l,
460464
self.chroma,
461-
self.hue.to_positive_degrees()
465+
self.hue.into_positive_degrees()
462466
)
463467
}
464468
}

style/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ path = "../core"
1616
features = ["palette"]
1717

1818
[dependencies.palette]
19-
version = "0.6"
19+
version = "0.7"
2020

2121
[dependencies.once_cell]
2222
version = "1.15"

style/src/theme/palette.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
use iced_core::Color;
33

44
use once_cell::sync::Lazy;
5-
use palette::{FromColor, Hsl, Mix, RelativeContrast, Srgb};
5+
use palette::color_difference::Wcag21RelativeContrast;
6+
use palette::rgb::Rgb;
7+
use palette::{FromColor, Hsl, Mix};
68

79
/// A color palette.
810
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -298,11 +300,11 @@ fn deviate(color: Color, amount: f32) -> Color {
298300
}
299301

300302
fn mix(a: Color, b: Color, factor: f32) -> Color {
301-
let a_lin = Srgb::from(a).into_linear();
302-
let b_lin = Srgb::from(b).into_linear();
303+
let a_lin = Rgb::from(a).into_linear();
304+
let b_lin = Rgb::from(b).into_linear();
303305

304-
let mixed = a_lin.mix(&b_lin, factor);
305-
Srgb::from_linear(mixed).into()
306+
let mixed = a_lin.mix(b_lin, factor);
307+
Rgb::from_linear(mixed).into()
306308
}
307309

308310
fn readable(background: Color, text: Color) -> Color {
@@ -320,16 +322,16 @@ fn is_dark(color: Color) -> bool {
320322
}
321323

322324
fn is_readable(a: Color, b: Color) -> bool {
323-
let a_srgb = Srgb::from(a);
324-
let b_srgb = Srgb::from(b);
325+
let a_srgb = Rgb::from(a);
326+
let b_srgb = Rgb::from(b);
325327

326-
a_srgb.has_enhanced_contrast_text(&b_srgb)
328+
a_srgb.has_enhanced_contrast_text(b_srgb)
327329
}
328330

329331
fn to_hsl(color: Color) -> Hsl {
330-
Hsl::from_color(Srgb::from(color))
332+
Hsl::from_color(Rgb::from(color))
331333
}
332334

333335
fn from_hsl(hsl: Hsl) -> Color {
334-
Srgb::from_color(hsl).into()
336+
Rgb::from_color(hsl).into()
335337
}

0 commit comments

Comments
 (0)