colorprofile
Version, currently main branch2 versions
- 0.1.1latestAug 31, 2026
- main branchFeb 23, 2026
github.com/dsisnero/colorprofile
Detect terminal colorprofiles and degrade accordingly
Installation
# Add this to your shard.yml
dependencies:
colorprofile:
github: dsisnero/colorprofile
branch: mainmain is a branch, not a release, so this tracks it rather than pinning a version.
Then run:
shards installshard.yml
- Crystal
>= 1.19.1- License
- MIT
- Author
- Dominic Sisneros
Dependencies
Runtime Dependencies
Development Dependencies
- ameba~> 1.6github: crystal-ameba/amebadev
README
This README is the one indexed from the repository at its latest ref, not from the tag for this version.
colorprofile
A simple, powerful—and at times magical—library for detecting terminal color profiles and performing color (and CSI) degradation.
This is a Crystal port of github.com/charmbracelet/colorprofile.
The original Go source code is available in the vendor/ directory as a git
submodule.
Installation
-
Add the dependency to your
shard.yml:dependencies: colorprofile: github: dsisnero/colorprofile -
Run
shards install
Usage
Detecting the terminal's color profile
Detecting the terminal's color profile is easy.
require "colorprofile"
# Detect the color profile.
profile = Colorprofile.detect(STDOUT, ENV.to_h)
# Comment on the profile.
case profile
when Colorprofile::Profile::TrueColor
puts "You know, your colors are quite fancy."
when Colorprofile::Profile::ANSI256
puts "You know, your colors are quite 1990s fancy."
when Colorprofile::Profile::ANSI
puts "You know, your colors are quite normcore."
when Colorprofile::Profile::ASCII
puts "You know, your colors are quite ancient."
when Colorprofile::Profile::NoTTY
puts "You know, your colors are quite naughty!"
end
Downsampling colors
When necessary, colors can be downsampled to a given profile.
profile = Colorprofile.detect(STDOUT, ENV.to_h)
color = Color::RGBA.new(0x6b, 0x50, 0xff, 0xff) # #6b50ff
# Downsample to the detected profile, when necessary.
converted_color = profile.convert(color)
# Or manually convert to a given profile.
ansi256_color = Colorprofile::Profile::ANSI256.convert(color)
ansi_color = Colorprofile::Profile::ANSI.convert(color)
no_color = Colorprofile::Profile::ASCII.convert(color)
no_ansi = Colorprofile::Profile::NoTTY.convert(color)
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.
my_fancy_ansi = "\e[38;2;107;80;255mCute \e[1;3mpuppy!!\e[m"
# Automatically downsample for the terminal at stdout.
writer = Colorprofile::Writer.new(STDOUT, ENV.to_h)
writer.write(my_fancy_ansi.to_slice)
# Downsample to 4-bit ANSI.
writer.profile = Colorprofile::Profile::ANSI
writer.write(my_fancy_ansi.to_slice)
# Ascii-fy, no colors.
writer.profile = Colorprofile::Profile::ASCII
writer.write(my_fancy_ansi.to_slice)
# Strip ANSI altogether.
writer.profile = Colorprofile::Profile::NoTTY
writer.write(my_fancy_ansi.to_slice) # not as fancy
Development
Run make help to see available targets:
make install- Install dependenciesmake test- Run testsmake format- Format codemake lint- Run linter (ameba)
Contributing
- Fork it (https://github.com/dsisnero/colorprofile/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Attribution
This project is a Crystal port of the Go library charmbracelet/colorprofile. All credit for the original design and implementation goes to the Charmbracelet team.
License
MIT License - see LICENSE for details.
Contributors
- Dominic Sisneros - creator and maintainer of the Crystal port
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This branch
- Branch
main- Seen
- Feb 23, 2026
- Crystal
>= 1.19.1- Indexed
- yes
Dependents
Repository
github.com/dsisnero/colorprofile
Metadata
- Created
- Aug 19, 2026
- Updated
- Sep 17, 2026
- Synced
- Sep 17, 2026
- Versions
- 2