Skip to content

Commit 46b2596

Browse files
committed
Improve color handling and documentation
1 parent d6d9d8c commit 46b2596

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/colors/mod.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
//! Matplotlib colors.
22
//!
3-
//! https://matplotlib.org/stable/gallery/color/named_colors.html
3+
//! This module defines a [`Color`] trait and implements it for `[r,
4+
//! g, b]`, `[r, g, b, a]` (where `r`, `g`, `b`, `a` are `f64` numbers
5+
//! clamped to the interval \[0,1\], or `u8` numbers) and `f64`
6+
//! (clamped to \[0,1\], for gray-scale). If you would like to use
7+
//! HTML colors, statically checked, you can for example use the crate
8+
//! [color-hex][]. For any color `c`, `(c, a)` is another color that
9+
//! sets (or overrides) the alpha component to `a`. Moreover, it also
10+
//! defines all [matplotlib color tables][colors], [`Base`], [`Tab`],
11+
//! [`CSS4`], and [`Xkcd`].
12+
//!
13+
//! [colors]: https://matplotlib.org/stable/gallery/color/named_colors.html
14+
//! [color-hex]: https://crates.io/crates/color-hex
415
516
// Unlike Matplotlib colors, we want the colors to be statically
617
// checked as much as possible to avoid a plot to stop the whole
@@ -37,6 +48,20 @@ impl Color for [f64; 4] {
3748
}
3849
}
3950

51+
impl Color for [u8; 3] {
52+
fn rgba(&self) -> [f64; 4] {
53+
[self[0] as f64 / 255., self[1] as f64 / 255.,
54+
self[2] as f64 / 255., 1.]
55+
}
56+
}
57+
58+
impl Color for [u8; 4] {
59+
fn rgba(&self) -> [f64; 4] {
60+
[self[0] as f64 / 255., self[1] as f64 / 255.,
61+
self[2] as f64 / 255., self[3] as f64 / 255.]
62+
}
63+
}
64+
4065
/// For any color `c`, one can change its alpha value by using
4166
/// `(c, alpha)` where `alpha` will be clamped to \[0., 1.\].
4267
impl<C: Color> Color for (C, f64) {
@@ -134,6 +159,7 @@ impl Color for Tab {
134159
}
135160
}
136161

162+
/// The [CSS4 colors](https://matplotlib.org/stable/gallery/color/named_colors.html#css-colors).
137163
#[derive(Clone, Copy, Debug)]
138164
pub enum CSS4 {
139165
AcidGreen,

0 commit comments

Comments
 (0)