← All libraries

CandyPalette

CandyPalette

Terminal color profile detection + ANSI conversion

port of charmbracelet/colorprofile colorterminaloutput

Detect the terminal's color capability (ANSI / ANSI256 / TrueColor), convert between formats, and write StandardColors compatible with the Charm ecosystem.

Install

composer require sugarcraft/candy-palette

Quickstart

use SugarCraft\Palette\Palette;

$profile = Palette::detect();
echo $profile->type();   // "truecolor" | "256" | "ansi"

$hex = Palette::fromHex('#ff6b6b')->toProfile($profile)->ansi();
$rgb = Palette::fromHex('#ff6b6b')->rgb();  // [255, 107, 107]

What's in the box

Profile detectionAuto-detect TrueColor / ANSI256 / ANSI via environment + terminfo.
Format conversionHex ↔ RGB ↔ ANSI ↔ ANSI256. Convert any color to the terminal's capability.
StandardColors16 ANSI standard colors with hex equivalents.
ProfileWriterOutput ANSI escape sequences to set the terminal profile.

Source & demos

Try the quickstart →

API

ClassMethodDescription
Palettedetect()Detect terminal color profile
PalettefromHex(hex)Create color from hex
PalettefromRgb(r, g, b)Create color from RGB
PalettetoProfile(profile)Convert to profile
Paletteansi(), ansi256(), truecolor()Output as ANSI format
StandardColorsblack(), red(), green()...16 ANSI standard colors
ProfileWriterwrite(profile)Output terminal profile

Demos.

VHS-recorded GIFs of every example shipped with the library. Regenerated automatically on every push that touches the source.

Profile detection

Profile detection

Resolved Ascii / ANSI / ANSI256 / TrueColor profile.
Standard colours

Standard colours

The 16 ANSI standard colours rendered as a swatch.
Conversion

Conversion

TrueColor → ANSI256 → ANSI degradation steps.
Degrade

Degrade

Same hex degraded for each output profile.