I’ve always relied on 2-bit coloring for terminal output, sticking to eight foreground and eight background colors. It’s nice because it conforms to any user’s terminal theme. But there’s a whole world of colors out there (in most modern terminals) and I figured it was about time I started expanding my horizons.
I write most of my command line tools and scripts in Ruby. Call me old fashioned, but it really is a beautiful, readable language with plenty of room for elegant solutions. So this little snippet is in Ruby, but the core of it is simple enough that I can’t imagine it would take much work to port.
Like all terminal color output, it relies on ANSI escape codes. 2-bit escape codes look like \033[0;30;41m
, which would be regular black text on a red background. But you can use RGB values in most modern terminals with an escape code like \033[38;2;RRR;GGG;BBBm
(for a foreground color). So all you have to do is get RGB values for a specific color and insert them into that code.
The following Ruby snippet (gist here) will take a CSS hex color, e.g. #FA380E
, and turn it into an ANSI escape sequence for you. It splits the hex value and uses the #hex
method in Ruby to convert a 2-digit hex into a value between 1 and 256. With it you can run something like print "text to color".color256('#FA380E')
and get some bright red text. The snippet itself is designed to be included in a script. If you run it directly you can test by using ./256color.rb "TEXT TO COLOR" "FG" "BG"
where FG and BG are 3 or 6-digit hex codes. The octothorp (#
) is always optional.
Hope that’s of use to somebody!
By the way, this is incorporated into the latest version of Doing such that you can use %#RRGGBB
instead of a color name to set colors in a template. And use %b#RRGGBB
to set a background color. Customize away!