3 releases

Uses new Rust 2024

0.1.2 May 16, 2025
0.1.1 May 16, 2025
0.1.0 May 14, 2025

#90 in Visualization

Download history 97/week @ 2025-05-09 288/week @ 2025-05-16

385 downloads per month

MIT license

230KB
4K SLoC

Pigment

Crates.io Documentation License: MIT

All the colors of the web, by name – case-/space-/snake-insensitive.

Pigment is a Rust library that provides access to hundreds of named colors, with a forgiving lookup system that ignores case, spaces, and other non-alphanumeric characters.

Installation

Add this to your Cargo.toml:

[dependencies]
pigment = "0.1.2"

Features

  • Extensive color database: Hundreds of named colors from Wikipedia
  • Forgiving lookups: Case-insensitive, ignores spaces and special characters
  • Multiple formats: Access colors as hex codes or RGB tuples
  • ANSI terminal support: Built-in support for ANSI color codes
  • Multiple library integrations: Optional integrations with popular color libraries:

Usage

Basic Usage

use pigment::color;

fn main() {
    // Look up a color by name
    let azure = color("Azure").unwrap();

    // Access color properties
    println!("Name: {}", azure.name());     // "Azure"
    println!("Hex: {}", azure.hex());       // "#007FFF"
    println!("RGB: {:?}", azure.rgb());     // (0, 127, 255)

    // Forgiving lookups - these all return the same color
    assert_eq!(color("Azure"), color("azure"));
    assert_eq!(color("Azure"), color("AZURE"));
    assert_eq!(color("Azure"), color("a z u r e"));
    assert_eq!(color("Azure"), color("a-z-u-r-e"));
}

ANSI Terminal Colors

use pigment::color;

fn main() {
    let red = color("Red").unwrap();

    // Print colored text in terminal
    println!("{}This is red text{}",
        red.ansi().fg(),           // Foreground color
        pigment::ansi::Ansi::reset() // Reset formatting
    );

    let blue = color("Blue").unwrap();
    println!("{}Text on blue background{}",
        blue.ansi().bg(),          // Background color
        pigment::ansi::Ansi::reset()
    );
}

Library Integrations

Pigment can integrate with several popular Rust color libraries. Here are some examples:

owo-colors

Enable the owo feature in your Cargo.toml:

[dependencies]
pigment = { version = "0.1.2", features = ["owo"] }
owo-colors = "4"

Then use it like this:

use owo_colors::OwoColorize;
use pigment::color;

fn main() {
    let azure = color("Azure").unwrap();

    // Use with owo-colors
    let owo_color: owo_colors::Rgb = azure.into();

    // Now you can use all owo-colors functionality
    println!("{}", "Azure colored text".color(owo_color));
}

termcolor

Enable the termcolor feature in your Cargo.toml:

[dependencies]
pigment = { version = "0.1.2", features = ["termcolor"] }
termcolor = "1.2"

Then use it like this:

use std::io::Write;
use termcolor::{ColorChoice, ColorSpec, StandardStream, WriteColor};
use pigment::color;

fn main() {
    let azure = color("Azure").unwrap();
    let tc_color: termcolor::Color = azure.into();

    let mut stdout = StandardStream::stdout(ColorChoice::Always);
    stdout.set_color(ColorSpec::new().set_fg(Some(tc_color))).unwrap();
    writeln!(&mut stdout, "Azure colored text").unwrap();
    stdout.reset().unwrap();
}

colored

Enable the colored feature in your Cargo.toml:

[dependencies]
pigment = { version = "0.1.2", features = ["colored"] }
colored = "2"

Then use it like this:

use colored::Colorize;
use pigment::color;

fn main() {
    let azure = color("Azure").unwrap();
    let c_color: colored::Color = azure.into();

    println!("{}", "Azure colored text".color(c_color));
}

Other supported libraries include anstyle, nu-ansi-term, yansi, and crossterm. See the examples directory for more detailed usage examples.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

~2–12MB
~157K SLoC