Color

Struct Color 

Source
pub struct Color { /* private fields */ }
Expand description

Color.

§Constructors from color representations

FromConstructor
RGBColor::rgb
RGBAColor::rgba
Named ColorColor::named
Hexadecimal LiteralColor::hex_literal
Hexadecimal StringColor::hex_str

§Default constructor

Color::default constructs a color with RGB values of 0, 0, 0 (black) and an alpha value of 1.0 (opaque).

§Other methods

MethodDescription
Color::alphaSet the opacity/transparency of the color.
Color::to_plotly_rgbaConvert the color to an Rgba from the plotly crate.

Implementations§

Source§

impl Color

Source

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

Construct a color from RGB (red, green, blue) values.

§Arguments
  • r - Red component.
  • g - Green component.
  • b - Blue component.
§Returns

Color.

Source

pub fn rgba(r: u8, g: u8, b: u8, a: f64) -> Color

Construct a color from RGBA (red, green, blue, alpha) values.

§Arguments
  • r - Red component.
  • g - Green component.
  • b - Blue component.
  • a - Alpha (opacity) between 0 (transparent) and 1 (opaque). Inputs outside the range [0, 1] are clamped.
§Returns

Color.

Source

pub fn named(color: NamedColor) -> Color

Construct a color from a named color.

§Arguments
  • color - Named color.
§Returns.

Color.

Examples found in repository?
examples/general_2d_plot.rs (line 8)
4fn main() {
5    // Define the traces.
6    let trace_1 = Trace::new_2d([1.0, 2.0, 3.0], [1.0, 2.0, 3.0])
7        .name("y = x")
8        .line_color(Color::named(NamedColor::Red))
9        .line_width(2.0)
10        .line_style(LineStyle::Dash);
11    let trace_2 = Trace::new_2d([1.0, 2.0, 3.0], [1.0, 4.0, 9.0])
12        .name("y = x^2")
13        .line_color(Color::named(NamedColor::Blue))
14        .line_width(2.0)
15        .line_style(LineStyle::Dot);
16
17    // Figure formatting.
18    let format: Format = FormatBuilder::default()
19        .title("y vs. x")
20        .x_label("x")
21        .y_label("y")
22        .width(800)
23        .height(600)
24        .build()
25        .unwrap();
26
27    // Create the figure.
28    let fig = Figure::new(vec![trace_1, trace_2], format);
29
30    // Save the figure so it can be displayed right below this example.
31    fig.save_inline_html(Path::new("book/src/figures/general_2d_plot.html"));
32
33    // Alternatively, you can show the figure in a web browser.
34    // fig.show();
35}
More examples
Hide additional examples
examples/general_3d_plot.rs (line 8)
4fn main() {
5    // Define the traces.
6    let trace_1 = Trace::new_3d([1.0, 2.0, 5.0], [1.0, 2.0, 3.0], [1.0, 2.0, 4.0])
7        .name("Trace 1")
8        .line_color(Color::named(NamedColor::Red))
9        .line_width(2.0)
10        .line_style(LineStyle::Dash);
11    let trace_2 = Trace::new_3d([1.0, 2.0, 5.0], [3.0, 2.0, 1.0], [1.0, 2.0, 4.0])
12        .name("Trace 2")
13        .line_color(Color::named(NamedColor::Blue))
14        .line_width(2.0)
15        .line_style(LineStyle::Dot);
16
17    // Figure formatting.
18    let format: Format = FormatBuilder::default()
19        .title("z vs. x and y")
20        .x_label("x")
21        .y_label("y")
22        .z_label("z")
23        .width(800)
24        .height(600)
25        .build()
26        .unwrap();
27
28    // Create the figure.
29    let fig = Figure::new(vec![trace_1, trace_2], format);
30
31    // Save the figure so it can be displayed right below this example.
32    fig.save_inline_html(Path::new("book/src/figures/general_3d_plot.html"));
33
34    // Alternatively, you can show the figure in a web browser.
35    // fig.show();
36}
Source

pub fn hex_literal(color: u32) -> Color

Construct a color from a hexadecimal literal.

§Arguments
  • color - Hexadecimal literal representing a color.
§Returns

Color.

§Note

If color is outside the valid RGB range (e.g. color > 0xFFFFFF), then the default color is returned.

§References
Source

pub fn hex_str(color: &str) -> Color

Construct a color from a hexadecimal string.

§Arguments
  • color - Hexadecimal string representing a color.
§Returns

Color.

§Note

If color is outside the valid RGB range (e.g. `color > “#FFFFFF”), then the default color is returned.

Source

pub fn alpha(self, a: f64) -> Color

Set the opacity/transparency of the color.

§Arguments
  • a - Alpha (opacity) between 0 (transparent) and 1 (opaque). Inputs outside the range [0, 1] are clamped.
§Returns

Color with the opacity set.

Source

pub fn to_plotly_rgba(&self) -> Rgba

Convert the color to an Rgba from the plotly crate.

§Returns

This color as an Rgba from the plotly crate.

Trait Implementations§

Source§

impl Debug for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Color

Source§

fn default() -> Self

Default constructor.

§Returns

Default-constructed color (black).

§Example
use plotting::{Color, NamedColor};

let color = Color::default();

assert_eq!(color, Color::named(NamedColor::Black))
Source§

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnwindSafe for Color

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.