zng/
color.rs

1//! Color and gradient types, functions, properties and macros.
2//!
3//! # Colors
4//!
5//! The example demonstrates multiple ways of declaring or selecting a color, all blue in this case.
6//!  
7//! ```
8//! use zng::prelude::*;
9//!
10//! fn sample(color: impl IntoVar<color::Rgba>) -> UiNode {
11//!     Wgt! {
12//!         layout::size = (100, 40);
13//!         widget::background_color = color;
14//!     }
15//! }
16//!
17//! # fn example() {
18//! # let _ =
19//! Window! {
20//!     child = Stack!(
21//!         top_to_bottom,
22//!         5,
23//!         ui_vec![
24//!             sample(hex!(#00F)),
25//!             sample(rgb(0, 0, 255)),
26//!             sample(rgb(0.0, 0.0, 1.0)),
27//!             sample(colors::BLUE),
28//!             sample(hsv(240.deg(), 100.pct(), 100.pct())),
29//!             sample(hsl(240.deg(), 100.pct(), 50.pct())),
30//!         ]
31//!     );
32//! }
33//! # ; }
34//! ```
35//!
36//! The [`Rgba`] type also provides methods for basic color manipulation and mixing.
37//!
38//! ```rust,no_fmt
39//! # use zng::prelude::*;
40//! # fn sample(_: impl IntoVar<color::Rgba>) -> UiNode {
41//! # widget::node::UiNode::nil()
42//! # }
43//! # let _ = ui_vec![
44//! sample(colors::GREEN.darken(50.pct())),
45//! sample(colors::GREEN),
46//! sample(colors::GREEN.lighten(50.pct())),
47//! sample(colors::GREEN.desaturate(50.pct())),
48//! sample(colors::GREEN.with_alpha(50.pct()).mix_normal(colors::BLUE)),
49//! # ];
50//! ```
51//!
52//! Color mixing methods apply the color over the parameter, that is `foreground.mix_normal(background)`.
53//!
54//! # Color Filters
55//!
56//! The [`filter`] module provides implementation of pixel filter graphical effects that you may be
57//! familiar with from CSS.
58//!
59//! ```
60//! use zng::prelude::*;
61//!
62//! # fn example() {
63//! # let _ =
64//! Window! {
65//!     clear_color = colors::BLACK.transparent();
66//!     color::filter::opacity = 50.pct();
67//!     child = Text!("translucent window");
68//! }
69//! # ; }
70//! ```
71//!
72//! The example above applies [`filter::opacity`] on the window, making it translucent in view-process
73//! implementations that support transparent windows.
74//!
75//! [`filter::opacity`]: fn@filter::opacity
76//!
77//! # Gradients
78//!
79//! The [`gradient`] module provides implementation of linear, radial and conic gradients. Usually the
80//! gradient nodes are wrapped in some other property like [`widget::background_conic`], but they can be used directly.
81//!
82//! [`widget::background_conic`]: fn@crate::widget::background_conic
83//!
84//! ```
85//! use zng::prelude::*;
86//!
87//! # fn example() {
88//! # let _ =
89//! Window! {
90//!     widget::background = color::gradient::conic_gradient(
91//!         50.pct(),
92//!         45.deg(),
93//!         color::gradient::stops![colors::GREEN, (colors::RED, 30.pct()), colors::BLUE],
94//!     );
95//!     // OR
96//!     widget::background_conic = {
97//!         center: 50.pct(),
98//!         angle: 45.deg(),
99//!         stops: color::gradient::stops![colors::GREEN, (colors::RED, 30.pct()), colors::BLUE],
100//!     };
101//! }
102//! # ; }
103//! ```
104//!
105//! See [`gradient::stops!`] for the macro syntax.
106//!
107//! # Full API
108//!
109//! See [`zng_color`], [`zng_wgt_filter`] and [`zng_wgt_fill`] for the full API.
110
111pub use zng_color::{
112    COLOR_SCHEME_VAR, ColorScheme, Hsla, Hsva, LerpSpace, LightDark, LightDarkVarExt, MixAdjust, MixBlendMode, PreMulRgba, Rgba, colors,
113    hex, hsl, hsla, hsla_linear_sampler, hsla_sampler, hsv, hsva, lerp_space, light_dark, rgb, rgba, rgba_sampler, web_colors,
114    with_lerp_space,
115};
116
117pub use zng_wgt::{accent_color, base_color, color_scheme};
118
119pub use zng_wgt_fill::node::flood;
120
121/// Color filter types and properties.
122#[cfg(feature = "color_filter")]
123pub mod filter {
124    pub use zng_color::filter::{ColorMatrix, Filter, RenderFilter};
125
126    pub use zng_wgt_filter::{
127        backdrop_blur, backdrop_brightness, backdrop_color_matrix, backdrop_contrast, backdrop_filter, backdrop_grayscale,
128        backdrop_hue_rotate, backdrop_invert, backdrop_saturate, backdrop_sepia, blur, brightness, child_filter, child_mix_blend,
129        child_opacity, color_matrix, contrast, drop_shadow, filter, grayscale, hue_rotate, invert_color, mix_blend, opacity, saturate,
130        sepia,
131    };
132}
133
134/// Color gradient types and nodes.
135pub mod gradient {
136    pub use zng_color::gradient::{
137        ColorStop, ExtendMode, GradientRadius, GradientRadiusBase, GradientStop, GradientStops, LinearGradientAxis, RenderExtendMode,
138        RenderGradientStop, stops,
139    };
140
141    pub use zng_wgt_fill::node::{
142        ConicGradient, GradientBuilder, LinearGradient, RadialGradient, TiledConicGradient, TiledLinearGradient, TiledRadialGradient,
143        conic_gradient, gradient, linear_gradient, radial_gradient,
144    };
145}