|
1 | 1 | //! Matplotlib colors. |
2 | 2 | //! |
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 |
4 | 15 |
|
5 | 16 | // Unlike Matplotlib colors, we want the colors to be statically |
6 | 17 | // checked as much as possible to avoid a plot to stop the whole |
@@ -37,6 +48,20 @@ impl Color for [f64; 4] { |
37 | 48 | } |
38 | 49 | } |
39 | 50 |
|
| 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 | + |
40 | 65 | /// For any color `c`, one can change its alpha value by using |
41 | 66 | /// `(c, alpha)` where `alpha` will be clamped to \[0., 1.\]. |
42 | 67 | impl<C: Color> Color for (C, f64) { |
@@ -134,6 +159,7 @@ impl Color for Tab { |
134 | 159 | } |
135 | 160 | } |
136 | 161 |
|
| 162 | +/// The [CSS4 colors](https://matplotlib.org/stable/gallery/color/named_colors.html#css-colors). |
137 | 163 | #[derive(Clone, Copy, Debug)] |
138 | 164 | pub enum CSS4 { |
139 | 165 | AcidGreen, |
|
0 commit comments