packages

plot::color

On this page

sRGB colours with alpha, CSS-style parsing, and the colour cycles a theme draws series colours from.

Structs

ColorSource

pub struct Color

An sRGB colour with alpha, 8 bits a channel.

nabi
use plot::Color;

let c = try Color::parse("#0072b2");
assert(c == Color::rgb(0, 114, 178));
assert(c.with_alpha(0.5).to_hex() == "#0072b280");
  • pub r: u8§

    Source
  • pub g: u8§

    Source
  • pub b: u8§

    Source
  • pub a: u8§

    Source
  • pub fn rgb(r: u8, g: u8, b: u8) -> Color§

    Source

    An opaque colour.

    nabi
    assert(plot::Color::rgb(1, 2, 3).a == 255);
    
  • pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color§

    Source

    A colour with alpha (255 is opaque).

    nabi
    assert(plot::Color::rgba(1, 2, 3, 0).opacity() == 0.0);
    
  • pub fn unit(r: f64, g: f64, b: f64) -> Color§

    Source

    From channels in [0, 1], rounded to the nearest 8-bit value.

    nabi
    assert(plot::Color::unit(1.0, 0.5, 0.0) == plot::Color::rgb(255, 128, 0));
    
  • pub fn clear() -> Color§

    Source

    Fully transparent black.

    nabi
    assert(plot::Color::clear().a == 0);
    
  • pub fn parse(s: str) throws (PlotError) -> Color§

    Source

    Reads #rgb, #rrggbb, #rrggbbaa, a CSS basic colour name ("red", "gray", ...), "none" or "transparent".

    nabi
    use plot::Color;
    
    assert((try Color::parse("#f00")) == Color::rgb(255, 0, 0));
    assert((try Color::parse("navy")).b == 128);
    assert((try? Color::parse("#12")) is none);
    
  • pub fn to_hex(self) -> str§

    Source

    #rrggbb, or #rrggbbaa when not opaque.

    nabi
    assert(plot::Color::rgb(255, 128, 0).to_hex() == "#ff8000");
    
  • pub fn with_alpha(self, a: f64) -> Color§

    Source

    The same colour with alpha a in [0, 1].

    nabi
    assert(plot::Color::rgb(0, 0, 0).with_alpha(1.0).a == 255);
    
  • pub fn opacity(self) -> f64§

    Source

    Alpha as a fraction in [0, 1].

    nabi
    assert(plot::Color::rgba(0, 0, 0, 255).opacity() == 1.0);
    
  • pub fn mix(self, o: Color, t: f64) -> Color§

    Source

    t of the way from self to o, channel by channel (alpha too).

    nabi
    use plot::Color;
    
    assert(Color::rgb(0, 0, 0).mix(Color::rgb(255, 255, 255), 0.5) == Color::rgb(128, 128, 128));
    
  • pub fn luminance(self) -> f64§

    Source

    Relative luminance (WCAG), 0 for black to 1 for white.

    nabi
    assert(plot::Color::rgb(0, 0, 0).luminance() == 0.0);
    
Trait implementations 1
  • impl Display for Color

Type aliases

ColorSpecSource

pub type ColorSpec = Color | str | none

What a builder takes for a colour: a Color, a string that Color::parse reads or "C0" to "C9" (the theme's cycle), or none for the next colour of the cycle.

nabi
let ax = plot::line([0, 1], [0, 1]).color("C2").line([0, 1], [1, 0]).color(plot::Color::rgb(9, 9, 9));
assert((try ax.svg()).contains("#090909"));

Functions

okabe_itoSource

pub fn okabe_ito() -> List<Color>

The Okabe-Ito palette (Okabe and Ito 2008), safe for the common colour-vision deficiencies; the default cycle. Ordered for contrast on a light background: blue, vermillion, bluish green, orange, sky blue, reddish purple, yellow, black.

nabi
assert(plot::color::okabe_ito()[0].to_hex() == "#0072b2");

tableau10Source

pub fn tableau10() -> List<Color>

Tableau 10 (the 2016 palette), as matplotlib's tab10 successor.

nabi
assert(plot::color::tableau10().len() == 10);