Watch
1
0
Fork
You've already forked golang-github-charmbracelet-colorprofile
0
No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Daniel Baumann a4f32b5447
Releasing fastforward version 0.4.3-1~ffwd13+u1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2026-07-14 19:20:39 +02:00
.github Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
debian Releasing fastforward version 0.4.3-1~ffwd13+u1. 2026-07-14 19:20:39 +02:00
examples Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
.golangci.yml Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
.goreleaser.yml Adding upstream version 0.3.2. 2026-07-14 19:19:23 +02:00
doc.go Adding upstream version 0.3.2. 2026-07-14 19:19:23 +02:00
env.go Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
env_other.go Adding upstream version 0.3.2. 2026-07-14 19:19:23 +02:00
env_test.go Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
env_windows.go Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
go.mod Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
go.sum Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
LICENSE Adding upstream version 0.3.2. 2026-07-14 19:19:23 +02:00
profile.go Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
profile_test.go Adding upstream version 0.3.2. 2026-07-14 19:19:23 +02:00
README.md Adding upstream version 0.3.2. 2026-07-14 19:19:23 +02:00
writer.go Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00
writer_test.go Merging upstream version 0.4.3. 2026-07-14 19:19:58 +02:00

Colorprofile

Latest Release GoDoc Build Status

A simple, powerful—and at times magical—package for detecting terminal color profiles and performing color (and CSI) degradation.

Detecting the terminals color profile

Detecting the terminals color profile is easy.

import "github.com/charmbracelet/colorprofile"

// Detect the color profile. If youre planning on writing to stderr you'd want
// to use os.Stderr instead.
p := colorprofile.Detect(os.Stdout, os.Environ())

// Comment on the profile.
fmt.Printf("You know, your colors are quite %s.", func() string {
    switch p {
    case colorprofile.TrueColor:
        return "fancy"
    case colorprofile.ANSI256:
        return "1990s fancy"
    case colorprofile.ANSI:
        return "normcore"
    case colorprofile.Ascii:
        return "ancient"
    case colorprofile.NoTTY:
        return "naughty!"
    }
    return "...IDK" // this should never happen
}())

Downsampling colors

When necessary, colors can be downsampled to a given profile, or manually downsampled to a specific profile.

p := colorprofile.Detect(os.Stdout, os.Environ())
c := color.RGBA{0x6b, 0x50, 0xff, 0xff} // #6b50ff

// Downsample to the detected profile, when necessary.
convertedColor := p.Convert(c)

// Or manually convert to a given profile.
ansi256Color := colorprofile.ANSI256.Convert(c)
ansiColor := colorprofile.ANSI.Convert(c)
noColor := colorprofile.Ascii.Convert(c)
noANSI := colorprofile.NoTTY.Convert(c)

Automatic downsampling with a Writer

You can also magically downsample colors in ANSI output, when necessary. If output is not a TTY ANSI will be dropped entirely.

myFancyANSI := "\x1b[38;2;107;80;255mCute \x1b[1;3mpuppy!!\x1b[m"

// Automatically downsample for the terminal at stdout.
w := colorprofile.NewWriter(os.Stdout, os.Environ())
fmt.Fprintf(w, myFancyANSI)

// Downsample to 4-bit ANSI.
w.Profile = colorprofile.ANSI
fmt.Fprintf(w, myFancyANSI)

// Ascii-fy, no colors.
w.Profile = colorprofile.Ascii
fmt.Fprintf(w, myFancyANSI)

// Strip ANSI altogether.
w.Profile = colorprofile.NoTTY
fmt.Fprintf(w, myFancyANSI) // not as fancy

Contributing

See contributing.

Feedback

Wed love to hear your thoughts on this project. Feel free to drop us a note!

License

MIT


Part of Charm.

The Charm logo

Charm热爱开源 • Charm loves open source • نحنُ نحب المصادر المفتوحة