Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade palette dependency #1875

Merged
merged 4 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ log = "0.4.17"
twox-hash = { version = "1.5", default-features = false }

[dependencies.palette]
version = "0.6"
version = "0.7"
optional = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = "0.1"

[dev-dependencies]
approx = "0.5"
40 changes: 19 additions & 21 deletions core/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,31 +183,31 @@ macro_rules! color {
}

#[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 {
fn from(srgba: Srgba) -> Self {
Color::new(srgba.red, srgba.green, srgba.blue, srgba.alpha)
fn from(rgba: Srgba) -> Self {
Color::new(rgba.red, rgba.green, rgba.blue, rgba.alpha)
}
}

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

#[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 {
fn from(srgb: Srgb) -> Self {
Color::new(srgb.red, srgb.green, srgb.blue, 1.0)
fn from(rgb: Srgb) -> Self {
Color::new(rgb.red, rgb.green, rgb.blue, 1.0)
}
}

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

#[test]
fn srgba_traits() {
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::Srgba type
let s: Srgba = c.into();
let r: Color = s.into();
assert_eq!(c, r);
}

#[test]
fn color_manipulation() {
use approx::assert_relative_eq;

let c1 = Color::from_rgb(0.5, 0.4, 0.3);
let c2 = Color::from_rgb(0.2, 0.5, 0.3);

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

// Take the lighter of each of the RGB components
// Take the lighter of each of the sRGB components
let lighter = l1.lighten(l2);

// Convert back to our Color
let r: Color = Srgba::from_linear(lighter).into();
assert_eq!(
r,
Color {
r: 0.5,
g: 0.5,
b: 0.3,
a: 1.0
}
);
let result: Color = Srgba::from_linear(lighter).into();

assert_relative_eq!(result.r, 0.5);
assert_relative_eq!(result.g, 0.5);
assert_relative_eq!(result.b, 0.3);
assert_relative_eq!(result.a, 1.0);
}
}
2 changes: 1 addition & 1 deletion examples/checkbox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Application for Example {
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
(
Self::default(),
font::load(include_bytes!("../fonts/icons.ttf").as_ref())
font::load(include_bytes!("../fonts/icons.ttf").as_slice())
.map(Message::FontLoaded),
)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/color_palette/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ publish = false

[dependencies]
iced = { path = "../..", features = ["canvas", "palette"] }
palette = "0.6.0"
palette = "0.7.0"
50 changes: 27 additions & 23 deletions examples/color_palette/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use iced::{
alignment, Alignment, Color, Element, Length, Point, Rectangle, Renderer,
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::ops::RangeInclusive;

Expand Down Expand Up @@ -49,12 +51,12 @@ impl Sandbox for ColorPalette {

fn update(&mut self, message: Message) {
let srgb = match message {
Message::RgbColorChanged(rgb) => palette::Srgb::from(rgb),
Message::HslColorChanged(hsl) => palette::Srgb::from_color(hsl),
Message::HsvColorChanged(hsv) => palette::Srgb::from_color(hsv),
Message::HwbColorChanged(hwb) => palette::Srgb::from_color(hwb),
Message::LabColorChanged(lab) => palette::Srgb::from_color(lab),
Message::LchColorChanged(lch) => palette::Srgb::from_color(lch),
Message::RgbColorChanged(rgb) => Rgb::from(rgb),
Message::HslColorChanged(hsl) => Rgb::from_color(hsl),
Message::HsvColorChanged(hsv) => Rgb::from_color(hsv),
Message::HwbColorChanged(hwb) => Rgb::from_color(hwb),
Message::LabColorChanged(lab) => Rgb::from_color(lab),
Message::LchColorChanged(lch) => Rgb::from_color(lch),
};

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

let srgb = palette::Srgb::from(base);
let srgb = Rgb::from(base);
let hsl = palette::Hsl::from_color(srgb);
let hsv = palette::Hsv::from_color(srgb);
let hwb = palette::Hwb::from_color(srgb);
Expand Down Expand Up @@ -95,12 +97,10 @@ struct Theme {

impl Theme {
pub fn new(base: impl Into<Color>) -> Theme {
use palette::{Hue, Shade};

let base = base.into();

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

let lower = [
hsl.shift_hue(-135.0).lighten(0.075),
Expand All @@ -119,12 +119,12 @@ impl Theme {
Theme {
lower: lower
.iter()
.map(|&color| Srgb::from_color(color).into())
.map(|&color| Rgb::from_color(color).into())
.collect(),
base,
higher: higher
.iter()
.map(|&color| Srgb::from_color(color).into())
.map(|&color| Rgb::from_color(color).into())
.collect(),
canvas_cache: canvas::Cache::default(),
}
Expand Down Expand Up @@ -209,14 +209,14 @@ impl Theme {

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() {
let pct = (i as f32 + 1.0) / (self.len() as f32 + 1.0);
let graded = Hsl {
lightness: 1.0 - pct,
..hsl
};
let color: Color = Srgb::from_color(graded).into();
let color: Color = Rgb::from_color(graded).into();

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

fn components(&self) -> [f32; 3] {
[
self.hue.to_positive_degrees(),
self.hue.into_positive_degrees(),
self.saturation,
self.lightness,
]
Expand All @@ -361,7 +361,7 @@ impl ColorSpace for palette::Hsl {
fn to_string(&self) -> String {
format!(
"hsl({:.1}, {:.1}%, {:.1}%)",
self.hue.to_positive_degrees(),
self.hue.into_positive_degrees(),
100.0 * self.saturation,
100.0 * self.lightness
)
Expand All @@ -378,13 +378,17 @@ impl ColorSpace for palette::Hsv {
}

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 {
format!(
"hsv({:.1}, {:.1}%, {:.1}%)",
self.hue.to_positive_degrees(),
self.hue.into_positive_degrees(),
100.0 * self.saturation,
100.0 * self.value
)
Expand All @@ -406,7 +410,7 @@ impl ColorSpace for palette::Hwb {

fn components(&self) -> [f32; 3] {
[
self.hue.to_positive_degrees(),
self.hue.into_positive_degrees(),
self.whiteness,
self.blackness,
]
Expand All @@ -415,7 +419,7 @@ impl ColorSpace for palette::Hwb {
fn to_string(&self) -> String {
format!(
"hwb({:.1}, {:.1}%, {:.1}%)",
self.hue.to_positive_degrees(),
self.hue.into_positive_degrees(),
100.0 * self.whiteness,
100.0 * self.blackness
)
Expand Down Expand Up @@ -450,15 +454,15 @@ impl ColorSpace for palette::Lch {
}

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 {
format!(
"Lch({:.1}, {:.1}, {:.1})",
self.l,
self.chroma,
self.hue.to_positive_degrees()
self.hue.into_positive_degrees()
)
}
}
2 changes: 1 addition & 1 deletion style/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ path = "../core"
features = ["palette"]

[dependencies.palette]
version = "0.6"
version = "0.7"

[dependencies.once_cell]
version = "1.15"
22 changes: 12 additions & 10 deletions style/src/theme/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use iced_core::Color;

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.
#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -298,11 +300,11 @@ fn deviate(color: Color, amount: f32) -> Color {
}

fn mix(a: Color, b: Color, factor: f32) -> Color {
let a_lin = Srgb::from(a).into_linear();
let b_lin = Srgb::from(b).into_linear();
let a_lin = Rgb::from(a).into_linear();
let b_lin = Rgb::from(b).into_linear();

let mixed = a_lin.mix(&b_lin, factor);
Srgb::from_linear(mixed).into()
let mixed = a_lin.mix(b_lin, factor);
Rgb::from_linear(mixed).into()
}

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

fn is_readable(a: Color, b: Color) -> bool {
let a_srgb = Srgb::from(a);
let b_srgb = Srgb::from(b);
let a_srgb = Rgb::from(a);
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 {
Hsl::from_color(Srgb::from(color))
Hsl::from_color(Rgb::from(color))
}

fn from_hsl(hsl: Hsl) -> Color {
Srgb::from_color(hsl).into()
Rgb::from_color(hsl).into()
}