Enum ansiterm::Colour

source ·
pub enum Colour {
Show 19 variants Black, Red, Green, Yellow, Blue, Purple, Cyan, White, DarkGray, BrightRed, BrightGreen, BrightYellow, BrightBlue, BrightPurple, BrightCyan, BrightGray, Fixed(u8), RGB(u8, u8, u8), Default,
}
Expand description

A colour is one specific type of ANSI escape code, and can refer to either the foreground or background colour.

These use the standard numeric sequences. See http://invisible-island.net/xterm/ctlseqs/ctlseqs.html

Variants§

§

Black

Colour #0 (foreground code 30, background code 40).

This is not necessarily the background colour, and using it as one may render the text hard to read on terminals with dark backgrounds.

§

Red

Colour #1 (foreground code 31, background code 41).

§

Green

Colour #2 (foreground code 32, background code 42).

§

Yellow

Colour #3 (foreground code 33, background code 43).

§

Blue

Colour #4 (foreground code 34, background code 44).

§

Purple

Colour #5 (foreground code 35, background code 45).

§

Cyan

Colour #6 (foreground code 36, background code 46).

§

White

Colour #7 (foreground code 37, background code 47).

As above, this is not necessarily the foreground colour, and may be hard to read on terminals with light backgrounds.

§

DarkGray

Colour #8 (foreground code 30, background code 40).

This is not necessarily the background colour, and using it as one may render the text hard to read on terminals with dark backgrounds.

§

BrightRed

Colour #9 (foreground code 91, background code 101).

§

BrightGreen

Colour #10 (foreground code 92, background code 102).

§

BrightYellow

Colour #11 (foreground code 93, background code 103).

§

BrightBlue

Colour #12 (foreground code 94, background code 104).

§

BrightPurple

Colour #13 (foreground code 95, background code 105).

§

BrightCyan

Colour #14 (foreground code 96, background code 106).

§

BrightGray

Colour #15 (foreground code 97, background code 107).

As above, this is not necessarily the foreground colour, and may be hard to read on terminals with light backgrounds.

§

Fixed(u8)

A colour number from 0 to 255, for use in 256-colour terminal environments.

  • Colours 0 to 7 are the Black to White variants respectively. These colours can usually be changed in the terminal emulator.
  • Colours 8 to 15 are brighter versions of the eight colours above. These can also usually be changed in the terminal emulator, or it could be configured to use the original colours and show the text in bold instead. It varies depending on the program.
  • Colours 16 to 231 contain several palettes of bright colours, arranged in six squares measuring six by six each.
  • Colours 232 to 255 are shades of grey from black to white.

It might make more sense to look at a colour chart.

§

RGB(u8, u8, u8)

A 24-bit RGB color, as specified by ISO-8613-3.

§

Default

Default colour (foreground code 39, background code 49).

Implementations§

source§

impl Colour

source

pub fn prefix(self) -> Prefix

The prefix bytes for this colour as a Style. These are the bytes that tell the terminal to use a different colour or font style.

See also Style::prefix.

Examples
use ansiterm::Colour::Green;

assert_eq!("\x1b[0m",
           Green.suffix().to_string());
source

pub fn infix(self, next: Colour) -> Infix

The infix bytes between this colour and next colour. These are the bytes that tell the terminal to use the next colour, or to do nothing if the two colours are equal.

See also Style::infix.

Examples
use ansiterm::Colour::{Red, Yellow};

assert_eq!("\x1b[33m",
           Red.infix(Yellow).to_string());
source

pub fn suffix(self) -> Suffix

The suffix for this colour as a Style. These are the bytes that tell the terminal to reset back to its normal colour and font style.

See also Style::suffix.

Examples
use ansiterm::Colour::Purple;

assert_eq!("\x1b[0m",
           Purple.suffix().to_string());
source§

impl Colour

source

pub fn normal(self) -> Style

Returns a Style with the foreground colour set to this colour.

Examples
use ansiterm::Colour;

let style = Colour::Red.normal();
println!("{}", style.paint("hi"));
source

pub fn bold(self) -> Style

Returns a Style with the foreground colour set to this colour and the bold property set.

Examples
use ansiterm::Colour;

let style = Colour::Green.bold();
println!("{}", style.paint("hey"));
source

pub fn dimmed(self) -> Style

Returns a Style with the foreground colour set to this colour and the dimmed property set.

Examples
use ansiterm::Colour;

let style = Colour::Yellow.dimmed();
println!("{}", style.paint("sup"));
source

pub fn italic(self) -> Style

Returns a Style with the foreground colour set to this colour and the italic property set.

Examples
use ansiterm::Colour;

let style = Colour::Blue.italic();
println!("{}", style.paint("greetings"));
source

pub fn underline(self) -> Style

Returns a Style with the foreground colour set to this colour and the underline property set.

Examples
use ansiterm::Colour;

let style = Colour::Purple.underline();
println!("{}", style.paint("salutations"));

Returns a Style with the foreground colour set to this colour and the blink property set.

Examples
use ansiterm::Colour;

let style = Colour::Cyan.blink();
println!("{}", style.paint("wazzup"));
source

pub fn reverse(self) -> Style

Returns a Style with the foreground colour set to this colour and the reverse property set.

Examples
use ansiterm::Colour;

let style = Colour::Black.reverse();
println!("{}", style.paint("aloha"));
source

pub fn hidden(self) -> Style

Returns a Style with the foreground colour set to this colour and the hidden property set.

Examples
use ansiterm::Colour;

let style = Colour::White.hidden();
println!("{}", style.paint("ahoy"));
source

pub fn strikethrough(self) -> Style

Returns a Style with the foreground colour set to this colour and the strikethrough property set.

Examples
use ansiterm::Colour;

let style = Colour::Fixed(244).strikethrough();
println!("{}", style.paint("yo"));
source

pub fn on(self, background: Colour) -> Style

Returns a Style with the foreground colour set to this colour and the background colour property set to the given colour.

Examples
use ansiterm::Colour;

let style = Colour::RGB(31, 31, 31).on(Colour::White);
println!("{}", style.paint("eyyyy"));
source

pub fn into_index(self) -> Result<u8, (u8, u8, u8)>

Returns index in 256-colour ANSI palette or red, green and blue components of the colour.

Variants Black through White are treated as indexes 0 through 7. Variant Fixed returns the index stored in it. Lastly, RGB variant is returned as a three-element tuple.

source§

impl Colour

source

pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>( self, input: I ) -> ANSIGenericString<'a, S>where I: Into<Cow<'a, S>>, <S as ToOwned>::Owned: Debug,

Paints the given text with this colour, returning an ANSI string. This is a short-cut so you don’t have to use Blue.normal() just to get blue text.

use ansiterm::Colour::Blue;
println!("{}", Blue.paint("da ba dee"));

Trait Implementations§

source§

impl Clone for Colour

source§

fn clone(&self) -> Colour

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Colour

source§

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

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

impl From<Colour> for Style

source§

fn from(colour: Colour) -> Style

You can turn a Colour into a Style with the foreground colour set with the From trait.

use ansiterm::{Style, Colour};
let green_foreground = Style::default().fg(Colour::Green);
assert_eq!(green_foreground, Colour::Green.normal());
assert_eq!(green_foreground, Colour::Green.into());
assert_eq!(green_foreground, Style::from(Colour::Green));
source§

impl PartialEq<Colour> for Colour

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Colour

source§

impl StructuralPartialEq for Colour

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.