Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c15f1b5

Browse files
authored
Merge pull request #1888 from iced-rs/web-colors
Introduce `web-colors` feature flag to enable "sRGB linear" blending
2 parents b353767 + b5fc0f4 commit c15f1b5

17 files changed

Lines changed: 121 additions & 34 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ smol = ["iced_futures/smol"]
3737
palette = ["iced_core/palette"]
3838
# Enables querying system information
3939
system = ["iced_winit/system"]
40+
# Enables broken "sRGB linear" blending to reproduce color management of the Web
41+
web-colors = ["iced_renderer/web-colors"]
4042
# Enables the advanced module
4143
advanced = []
4244

examples/geometry/src/main.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mod rainbow {
44
use iced_graphics::primitive::{ColoredVertex2D, Primitive};
55

6+
use iced::advanced::graphics::color;
67
use iced::advanced::layout::{self, Layout};
78
use iced::advanced::renderer;
89
use iced::advanced::widget::{self, Widget};
@@ -84,39 +85,39 @@ mod rainbow {
8485
vertices: vec![
8586
ColoredVertex2D {
8687
position: posn_center,
87-
color: [1.0, 1.0, 1.0, 1.0],
88+
color: color::pack([1.0, 1.0, 1.0, 1.0]),
8889
},
8990
ColoredVertex2D {
9091
position: posn_tl,
91-
color: color_r,
92+
color: color::pack(color_r),
9293
},
9394
ColoredVertex2D {
9495
position: posn_t,
95-
color: color_o,
96+
color: color::pack(color_o),
9697
},
9798
ColoredVertex2D {
9899
position: posn_tr,
99-
color: color_y,
100+
color: color::pack(color_y),
100101
},
101102
ColoredVertex2D {
102103
position: posn_r,
103-
color: color_g,
104+
color: color::pack(color_g),
104105
},
105106
ColoredVertex2D {
106107
position: posn_br,
107-
color: color_gb,
108+
color: color::pack(color_gb),
108109
},
109110
ColoredVertex2D {
110111
position: posn_b,
111-
color: color_b,
112+
color: color::pack(color_b),
112113
},
113114
ColoredVertex2D {
114115
position: posn_bl,
115-
color: color_i,
116+
color: color::pack(color_i),
116117
},
117118
ColoredVertex2D {
118119
position: posn_l,
119-
color: color_v,
120+
color: color::pack(color_v),
120121
},
121122
],
122123
indices: vec![

graphics/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ categories = ["gui"]
1414
geometry = ["lyon_path"]
1515
opengl = []
1616
image = ["dep:image", "kamadak-exif"]
17+
web-colors = []
1718

1819
[dependencies]
1920
glam = "0.24"

graphics/src/color.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! Manage colors for shaders.
2+
use crate::core::Color;
3+
4+
use bytemuck::{Pod, Zeroable};
5+
6+
/// A color packed as 4 floats representing RGBA channels.
7+
#[derive(Debug, Clone, Copy, PartialEq, Zeroable, Pod)]
8+
#[repr(C)]
9+
pub struct Packed([f32; 4]);
10+
11+
impl Packed {
12+
/// Returns the internal components of the [`Packed`] color.
13+
pub fn components(self) -> [f32; 4] {
14+
self.0
15+
}
16+
}
17+
18+
/// A flag that indicates whether the renderer should perform gamma correction.
19+
pub const GAMMA_CORRECTION: bool = internal::GAMMA_CORRECTION;
20+
21+
/// Packs a [`Color`].
22+
pub fn pack(color: impl Into<Color>) -> Packed {
23+
Packed(internal::pack(color.into()))
24+
}
25+
26+
#[cfg(not(feature = "web-colors"))]
27+
mod internal {
28+
use crate::core::Color;
29+
30+
pub const GAMMA_CORRECTION: bool = true;
31+
32+
pub fn pack(color: Color) -> [f32; 4] {
33+
color.into_linear()
34+
}
35+
}
36+
37+
#[cfg(feature = "web-colors")]
38+
mod internal {
39+
use crate::core::Color;
40+
41+
pub const GAMMA_CORRECTION: bool = false;
42+
43+
pub fn pack(color: Color) -> [f32; 4] {
44+
[color.r, color.g, color.b, color.a]
45+
}
46+
}

graphics/src/gradient.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
//! For a gradient that you can use as a background variant for a widget, see [`Gradient`].
44
//!
55
//! [`Gradient`]: crate::core::Gradient;
6+
use crate::color;
67
use crate::core::gradient::ColorStop;
78
use crate::core::{self, Color, Point, Rectangle};
9+
810
use std::cmp::Ordering;
911

1012
#[derive(Debug, Clone, PartialEq)]
@@ -101,7 +103,8 @@ impl Linear {
101103

102104
for (index, stop) in self.stops.iter().enumerate() {
103105
let [r, g, b, a] =
104-
stop.map_or(Color::default(), |s| s.color).into_linear();
106+
color::pack(stop.map_or(Color::default(), |s| s.color))
107+
.components();
105108

106109
data[index * 4] = r;
107110
data[(index * 4) + 1] = g;
@@ -133,7 +136,8 @@ pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed {
133136

134137
for (index, stop) in linear.stops.iter().enumerate() {
135138
let [r, g, b, a] =
136-
stop.map_or(Color::default(), |s| s.color).into_linear();
139+
color::pack(stop.map_or(Color::default(), |s| s.color))
140+
.components();
137141

138142
data[index * 4] = r;
139143
data[(index * 4) + 1] = g;

graphics/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod transformation;
2727
mod viewport;
2828

2929
pub mod backend;
30+
pub mod color;
3031
pub mod compositor;
3132
pub mod damage;
3233
pub mod gradient;

graphics/src/primitive.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Draw using different graphical primitives.
2+
use crate::color;
23
use crate::core::alignment;
34
use crate::core::image;
45
use crate::core::svg;
@@ -248,7 +249,7 @@ pub struct ColoredVertex2D {
248249
pub position: [f32; 2],
249250

250251
/// The color of the vertex in __linear__ RGBA.
251-
pub color: [f32; 4],
252+
pub color: color::Packed,
252253
}
253254

254255
/// A vertex which contains 2D position & packed gradient data.

renderer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ image = ["iced_tiny_skia/image", "iced_wgpu?/image"]
99
svg = ["iced_tiny_skia/svg", "iced_wgpu?/svg"]
1010
geometry = ["iced_graphics/geometry", "iced_tiny_skia/geometry", "iced_wgpu?/geometry"]
1111
tracing = ["iced_wgpu?/tracing"]
12+
web-colors = ["iced_wgpu?/web-colors"]
1213

1314
[dependencies]
1415
raw-window-handle = "0.5"

wgpu/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ repository = "https://github.com/iced-rs/iced"
1111
geometry = ["iced_graphics/geometry", "lyon"]
1212
image = ["iced_graphics/image"]
1313
svg = ["resvg"]
14+
web-colors = ["iced_graphics/web-colors"]
1415

1516
[dependencies]
1617
wgpu = "0.16"
@@ -44,7 +45,7 @@ path = "../graphics"
4445
[dependencies.glyphon]
4546
version = "0.2"
4647
git = "https://github.com/hecrj/glyphon.git"
47-
rev = "cf7fe9df00499b868a6a94fa5fdb0a4ca368c9f9"
48+
rev = "26f92369da3704988e3e27f0b35e705c6b2de203"
4849

4950
[dependencies.glam]
5051
version = "0.24"

wgpu/src/backend.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::core;
22
use crate::core::{Color, Font, Point, Size};
33
use crate::graphics::backend;
4+
use crate::graphics::color;
45
use crate::graphics::{Primitive, Transformation, Viewport};
56
use crate::quad;
67
use crate::text;
@@ -239,7 +240,7 @@ impl Backend {
239240
load: match clear_color {
240241
Some(background_color) => wgpu::LoadOp::Clear({
241242
let [r, g, b, a] =
242-
background_color.into_linear();
243+
color::pack(background_color).components();
243244

244245
wgpu::Color {
245246
r: f64::from(r),

0 commit comments

Comments
 (0)