Colors in terminal
Update 13/12/16 — I’ve added more context. Also more information on text styles and resource links at the bottom.
I built a little CLI tool that-color to name a color from a given hex. It also gives a nice color scale from hello-color. I use this tool often when I’m writing styleguide or mocking up components. I don’t have to think about color names, values or open the browser.
I started this project using the modules
ansy-styles. The first time I used
ansi-styles I got undefined trying to ouput the
style.color. I looked into the code and from there I started a quick research around this subject.
Before write this library I didn’t bother to understand how colors works in terminal. Just because this 👇 didn’t look friendly.
printf "\u001b[38;2;230;90;180mhello world\u001b[0m"
It turned out to be more simple than I thought.
\u001Bunicode ANSI escape code. By the way
\eare different ways to write the same thing.
[part of ANSI sequence escape.
38;is the xterm-256 foreground color code. Use
48to change the background color instead.
2;is the color format code for 24-bit RGB Truecolor ANSI support.
mend of the sequence.
\u001b[0mreset all changes for the next escape sequence.
The first parameter after
[ determines the style of the text. This value is off by default. Some of the attributes are:
0 for off,
1 for bold,
2 for underscore and
3 for blink.
Now the code looks like:
Doing a bit of homework and trying to understand how things works under the hood is always self-rewarding.