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.
- Color
Palette ColorPaletteis an object which stores a color palette based on aname, a set ofRGB Colorsand, optionally, a mapping ofcolor nameswhose key is aColorand the value is aString.- Context
Contextis an object containing state for template rendering. It can be serialized and deserialized to load from disk.- Conversion
ToF32 Error - Contains information of errors which occur while converting RGB band values from
u8tof32. - Conversion
ToU8 Error - Contains information of errors which occur while converting RGB band values from
f32tou8. - Float
Metadata - FloatMetadata contains, as the name indicates, metadata about float values.
- Parse
Error - Specialized error containing the error message and, optionally, the input string which underwent parsing and failed, as well as optional context regarding the error.
- Prefix
Container - Renderable
Color RenderableColorcontains aColorand 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
[0mwithout any ansi escape prefix - RgbTriple
- RgbTriple is an intermediary container which holds each of the
three bands of an RGB color as
u8values. - 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.
- Terminal
Info - Holds all terminal info obtained via
Terminal::info() - Value
- Value is a container of float values used within
crate::Colorfor 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 - RGBParse
Error - Represents a failure to parse a
Colorfrom strings. - Terminal
Info Error - Holds error details which may occur while querying terminal colors via
Terminal::background_colororTerminal::foreground_color. - Wrap
- Represents the intent of rendering a
LayerorColorbefore, after or around the text being colored.
Constants§
- BLACK
Colorwhich holds the absolute black RGB color.- WHITE
Colorwhich holds the absolute white RGB color.
Statics§
Traits§
- Ansi
Renderable - The
AnsiRenderabletrait allowPrefix-awareimplementors that implement theToAnsiEscSuffixtrait in order to produce a full ANSI sequence. - ToAnsi
EscSuffix - The
ToAnsiEscSuffixtrait 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 aPrefixin order to render a whole sequence.