Skip to main content

Crate couleur_rs

Crate couleur_rs 

Source
Expand description

couleur-rs: parse, apply contrast algorithms to ANSI RGB colors and print text powered by a simple template language

§Parsing Colors

let dark_pink = "#C32454".parse::<Color>()?;
let darkest_pink = "#831C5D".parse::<Color>()?;
let light_pink = "#FCA790".parse::<Color>()?;
let lightest_pink = "#FDCBB0".parse::<Color>()?;

§Applying Contrast

let dark_pink = "#C32454".parse::<Color>()?;
let darkest_pink = "#831C5D".parse::<Color>()?;
let light_pink = "#FCA790".parse::<Color>()?;
let lightest_pink = "#FDCBB0".parse::<Color>()?;

assert_eq!(
    dark_pink.to_triple(),
    (Value::from_u8(0xC3)?, Value::from_u8(0x24)?, Value::from_u8(0x54)?)
);

assert_eq!(lightest_pink.get_adobe_complementary().to_hex_string(), "#B0E2FD");
assert_eq!(lightest_pink.get_accessible_contrast().to_hex_string(), "#000000");
assert_eq!(lightest_pink.get_binary_contrast().to_hex_string(), "#000000");
assert_eq!(lightest_pink.get_msb_invert_contrast().to_hex_string(), "#7D4B30");

assert_eq!(darkest_pink.get_adobe_complementary().to_hex_string(), "#1C8342");
assert_eq!(darkest_pink.get_accessible_contrast().to_hex_string(), "#000000");
assert_eq!(darkest_pink.get_binary_contrast().to_hex_string(), "#FFFFFF");
assert_eq!(darkest_pink.get_msb_invert_contrast().to_hex_string(), "#039CDD");

§Template Language

parse::<&str, ContextError>(
    "{color:#1EBC73}Hello{color:#1EBC73%contrast:web} World",
);
Ok(Node::Array(vec![
    Node::Color("#1EBC73".parse::<couleur_rs::Color>().unwrap()),
    Node::Text("Hello".to_string()),
    Node::RenderableColor(
        RenderableColor::new("#1EBC73".parse::<couleur_rs::Color>().unwrap())
            .with_contrast(Contrast::Web),
    ),
    Node::Text(" World".to_string()),
]))

IMPORTANT

This library is currently a work-in-progress and provides the feature flags logging and tracing as debugging tools. All the tracing and logging annotations and calls will be removed before couleur-rs reaches version 1.0.0

Until then, the API likely to undergo changes in design and usability.

Structs§

Color
Represents an RGB color, providing methods to obtain color information and render the color in ANSI terminals supporting true-color.
ColorPalette
ColorPalette is an object which stores a color palette based on a name, a set of RGB Colors and, optionally, a mapping of color names whose key is a Color and the value is a String.
Context
Context is an object containing state for template rendering. It can be serialized and deserialized to load from disk.
ConversionToF32Error
Contains information of errors which occur while converting RGB band values from u8 to f32.
ConversionToU8Error
Contains information of errors which occur while converting RGB band values from f32 to u8.
FloatMetadata
FloatMetadata contains, as the name indicates, metadata about float values.
ParseError
Specialized error containing the error message and, optionally, the input string which underwent parsing and failed, as well as optional context regarding the error.
PrefixContainer
RenderableColor
RenderableColor contains a Color and optional properties that dictate how the color may be rendered as ANSI string and what contrast algorithm, if any, should be applied to a color before rendering.
Reset
Represents the ansi code [0m without any ansi escape prefix
RgbTriple
RgbTriple is an intermediary container which holds each of the three bands of an RGB color as u8 values.
Terminal
Terminal is an abstraction to retrive information regarding the background and foreground colors of the terminal at runtime along with information such as whether the background is dark or light and the binary luminance of both.
TerminalInfo
Holds all terminal info obtained via Terminal::info()
Value
Value is a container of float values used within crate::Color for each RGB band (red, green blue).

Enums§

Contrast
Set of contrast algorithms applicable to Color
Error
Main error used across the crate
Layer
Represents the concept of “background” and “foreground” colors in a terminal
Prefix
Represents an ANSI Escape character which prefixes ANSI sequences rendered by elements of this crate such as RenderableColor
RGBParseError
Represents a failure to parse a Color from strings.
TerminalInfoError
Holds error details which may occur while querying terminal colors via Terminal::background_color or Terminal::foreground_color.
Wrap
Represents the intent of rendering a Layer or Color before, after or around the text being colored.

Constants§

BLACK
Color which holds the absolute black RGB color.
WHITE
Color which holds the absolute white RGB color.

Statics§

PREFIX
TERMINAL

Traits§

AnsiRenderable
The AnsiRenderable trait allow Prefix-aware implementors that implement the ToAnsiEscSuffix trait in order to produce a full ANSI sequence.
ToAnsiEscSuffix
The ToAnsiEscSuffix trait provides a way for elements of this crate to produce a string which represents one element of an ANSI escape sequence that needs to be prefixed with a Prefix in order to render a whole sequence.

Functions§

get_log_path
get_runtime_prefix
local_data_dir
set_runtime_prefix
setup_logging
setup_tracing

Type Aliases§

Result
U8Triple
U8Triple is a type alias which represents a tuple containing the three RGB bands as u8