pastel

repository·master·Indexed 27 days ago

https://github.com/sharkdp/pastel

A command-line tool to generate, analyze, convert, and manipulate colors. It supports color generation, gradients, spectrum printing, and format conversion between spaces like RGB, HSL, Lab, and OKLCH. Features include visually distinct color generation, interactive color picking, and the ability to print colorized text using ANSI escape sequences with support for 24-bit true color.

Tokens
10.9K
Snippets
26
Records
88
Agent score
89%

What's inside pastel

  1. Install pastel

    master

    You can install pastel on various platforms using the following commands:

    Debian-based systems

    Install via apt (testing/unstable) or download the .deb from the release page:

    # Via apt
    sudo apt update
    sudo apt install pastel
    
    # Via dpkg
    wget "https://github.com/sharkdp/pastel/releases/download/v0.12.0/pastel_0.12.0_amd64.deb"
    sudo dpkg -i pastel_0.12.0_amd64.deb

    Arch Linux

    sudo pacman -S pastel

    Nix

    nix-env --install pastel

    MacOS

    brew install pastel

    Windows

    Using Scoop:

    scoop install pastel

    Using Winget:

    winget install sharkdp.pastel

    Snap

    sudo snap install pastel

    NetBSD

    pkgin install pastel

    From Source (Cargo)

    Requires Rust 1.83 or higher:

    cargo install pastel

    Or from the repository:

    git clone https://github.com/sharkdp/pastel
    cargo install --path ./pastel
  2. Print colorized text in shell scripts

    master

    Use the paint command to output colorized text to the terminal. This is useful for creating styled logs or status messages.

    Basic usage with background and foreground:

    # Set a background color
    bg="hotpink"
    # Get the foreground color that works with that background
    fg="$(pastel textcolor "$bg")"
    
    pastel paint "$fg" --on "$bg" "well readable text"

    Common patterns for status messages:

    # Error message with black text on red background
    pastel paint -n black --on red --bold "   ERROR!   "
    echo " A serious error"
    
    # Warning message with black text on yellow background
    pastel paint -n black --on yellow --bold "  WARNING!  "
    echo " A warning message"
    
    # Info message with underline
    echo -n " Informational message with a "
    echo -n "highlighted" | pastel paint -n default --underline
    echo " word"
    pastel paint -n black --on red --bold "   ERROR!   "
  3. Test terminal 24-bit true color support with colorcheck

    master

    Use the colorcheck command to verify if your terminal emulator supports 24-bit true color mode (16,777,216 colors). If the terminal does not support true color, pastel will fallback to 8-bit approximations (256 colors).

    To verify the output visually, ensure the color panels appear consistent. To verify the output precisely, you can use a colorpicker (via pastel pick) to check that the three colors in the 24-bit panels match these values (from left to right):

    • #492732, rgb(73, 39, 50)
    • #10331e, rgb(16, 51, 30)
    • #1d365a, rgb(29, 54, 90)
    pastel colorcheck
  4. Use pastel commands and subcommands

    master

    pastel provides several subcommands for color manipulation.

    • To see a complete list of commands, run: pastel
    • To get help for a specific subcommand (e.g., mix), run: pastel mix -h or pastel help mix.

    Common subcommands mentioned include:

    • saturate
    • mix
    • paint
    • lighten
    • format
    • color
    • pick
    • distinct
    • list
    • textcolor
    pastel
  5. Convert color formats and analyze colors

    master

    Use pastel to transform colors between different color spaces or to inspect them.

    Convert format: Convert a color (e.g., hex) to another format (e.g., HSL):

    pastel format hsl ff8000

    Name a color: Find the name of a specific color:

    pastel format name 44cc11

    Analyze/Show colors: Display colors in the terminal:

    pastel color "rgb(255,50,127)"
    pastel color 556270 4ecdc4 c7f484 ff6b6b c44d58
    pastel format hsl ff8000
  6. Generate distinct colors and lists

    master

    pastel can automate color palette generation and list retrieval.

    Generate N visually distinct colors:

    pastel distinct 8

    List all X11 / CSS color names:

    pastel list

    Pick a color from the screen:

    pastel pick
    pastel distinct 8
  7. Use PASTEL_COLOR_MODE environment variable

    master

    You can manually override the detected color mode by setting the PASTEL_COLOR_MODE environment variable. Supported values are:

    • 24bit or truecolor for TrueColor support.
    • 8bit for 8-bit ANSI support.
    • off to disable colors.
  8. Specify colors in various formats

    master

    pastel accepts colors in many formats, which can be passed as positional arguments or read from standard input (STDIN).

    Supported Formats:

    • Color names: lightslategray, orchid, orange
    • Hex strings: '#778899', 778899
    • Short hex: 789
    • RGB: 'rgb(119, 136, 153)', '119,136,153'
    • HSL: 'hsl(210, 14.3%, 53.3%)

    Usage Examples:

    Passing as positional arguments:

    pastel lighten 0.2 orchid orange lawngreen

    Reading from STDIN:

    printf "%s\n" orchid orange lawngreen | pastel lighten 0.2

    Mixing a color from STDIN with a color from the command line:

    pastel color red | pastel mix - blue
  9. Troubleshoot 24-bit color support

    master

    If pastel warns that your terminal does not appear to support 24-bit colors, you can verify your terminal's capabilities by running the colorcheck subcommand.

    To resolve the warning:

    1. Run pastel colorcheck to test support.
    2. If supported, set PASTEL_COLOR_MODE=24bit to force the mode.
    3. If not supported, set PASTEL_COLOR_MODE=8bit to use 8-bit colors and silence the warning.
    pastel colorcheck
  10. Use the 'pick' keyword for interactive color selection

    master

    When you pass pick as a color argument, pastel invokes an external color picker configured in your settings.

    If pick is provided as a positional argument, pastel will print a color spectrum to the terminal before opening the picker. Subsequent color arguments in the same command will not trigger the spectrum print again.

  11. Configure Simulated Annealing for color optimization

    master

    The SimulatedAnnealing struct allows for fine-grained control over the process of finding distinct colors. You can initialize it with a set of initial colors and specific SimulationParameters.

    SimulationParameters fields:

    • initial_temperature: Starting temperature for the annealing process.
    • cooling_rate: The rate at which temperature decreases.
    • num_iterations: Total number of optimization steps.
    • opt_target: Whether to optimize for the OptimizationTarget::Mean (average distance) or OptimizationTarget::Min (minimum distance between any two colors).
    • opt_mode: Whether to use OptimizationMode::Global (randomly generating new colors) or OptimizationMode::Local (making small adjustments to existing colors).
    • distance_metric: The DistanceMetric used for calculations.
    • num_fixed_colors: The number of colors at the start of the array that cannot be modified.
    let params = SimulationParameters {
        initial_temperature: 3.0,
        cooling_rate: 0.95,
        num_iterations: 100_000,
        opt_target: OptimizationTarget::Mean,
        opt_mode: OptimizationMode::Global,
        distance_metric: DistanceMetric::CIE76,
        num_fixed_colors: 1,
    };
    
    let mut annealing = SimulatedAnnealing::new(&initial_colors, params);
    let result = annealing.run(&mut |stats| {
        // handle statistics
    });