<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Lucentbeing.com :: Linux</title>
    <link href="http://lucentbeing.com/writing/tags/linux/atom.xml" rel="self" />
    <link href="http://lucentbeing.com" />
    <id>http://lucentbeing.com/writing/tags/linux/atom.xml</id>
    <author>
        <name>P.C. Shyamshankar</name>
        <email>sykora@lucentbeing.com</email>
    </author>
    <updated>2014-04-22T00:00:00Z</updated>
    <entry>
    <title>A Guide to 256 Color Codes</title>
    <link href="http://lucentbeing.com/writing/archives/a-guide-to-256-color-codes/" />
    <id>http://lucentbeing.com/writing/archives/a-guide-to-256-color-codes/</id>
    <published>2014-04-22T00:00:00Z</published>
    <updated>2014-04-22T00:00:00Z</updated>
    <summary type="html"><![CDATA[<article id="entry">
  <header>
    <hgroup>
      <h1><a href="/writing/archives/a-guide-to-256-color-codes/">A Guide to 256 Color Codes</a></h1>
      
    </hgroup>
    <div id="meta">
      <div class="date">Tuesday, April 22, 2014</div>
      <div class="tags"><a href="/writing/tags/command-line/">command-line</a> / <a href="/writing/tags/linux/">linux</a></div>
    </div>
  </header>

  <hr>

  <div id="detail">
    <blockquote>
<p>To kick-start the new site, I’ve resurrected one of the more popular previous entries, albeit touched up, and at a new location. I apologize for all the broken links.</p>
<p>This article was originally published on November 10, 2009.</p>
</blockquote>
<p>Terminal capabilities have some of the sparsest documentation I have ever had the misfortune of having to dig through. Popular terminal applications such as vim, emacs, mutt, etc. seem to make use of every last bit of a terminal’s flexibility, but exactly how they do that always appears to be a mystery.</p>
<p>Among these capabilities are those to display a 256 color pallette.</p>
<p>Most terminals can handle at least 16 colors (8, and a bold version of each), and the ANSI codes necessary to invoke them are already a mess. Getting a terminal to display 256 colors requires character gymnastics that are altogether incomprehensible.</p>
<p>After hours of poring over non-existent documentation stretching back to a time when color actually meant ink, I think I’ve identified a pattern. In this entry, I’ll be giving you a run-down on what a 256 color escape seqence looks like, and how you can use them<a href="#fn1" class="footnoteRef" id="fnref1"><sup>1</sup></a>.</p>
<h3 id="terminal-basics">Terminal Basics</h3>
<p>The primary function of a terminal is to display text. Terminals of yore were <em>serial</em> terminals – text came in as a stream, and the terminal had one pass to do whatever it needed to do. While this limitation probably derived from a lack of resources to store more than a small buffer, most of the terminals we use today are still built in this way, so that’s what we’re stuck with.</p>
<p>Terminals use a pattern of <em>delimiters</em> to mark-up regions of text with attributes, the same way an HTML document uses start and end tags to mark-up text in a document. However terminals usually can’t express attributes as rich as HTML, nor can they afford to include a sufficiently powerful parser to do so. The delimiters for terminal attributes are therefore extremely simple, and are known as <em>escape-codes</em>.</p>
<p>Every escape code is a sequence of characters which tell the terminal to do something special. The most common escape code is one you might be familiar with: <code>\n</code>. This tells the terminal to print a newline character instead of the literal characters <code>\</code> followed by <code>n</code>.</p>
<p>For more complex text attributes such as foreground color, bold-ness, etc, the terminal keeps a small flag to remember which state it’s in. When it sees the turn-on-bold escape code, it sets that flag, and continues to display bold text until it’s given an escape code that tells it otherwise.</p>
<p>For the standard 8-color palette, these codes are fairly well documented. However, finding the codes for the 256 color palette, and furthermore the syntax in which they may be used, has driven me almost to distraction. It would seem that although they’ve managed to increase the number of colors, they haven’t added any more documentation in that direction.</p>
<h3 id="special-effects">Special Effects</h3>
<p>There are a variety of effects that you can apply to text, but not all of them are supported on every terminal/emulator. The following are the most common:</p>
<ul>
<li>Reset (Clear all text attributes, including color, effects, etc.)</li>
<li>Bold</li>
<li>Italic</li>
<li>Underline</li>
<li>Blink</li>
<li>Reverse (Swap foreground and background)</li>
</ul>
<p>Colors are themselves special effects, but they need a different code, depending on whether they are to be applied on the foreground or the background.</p>
<h3 id="anatomy-of-a-color-code">Anatomy of a Color Code</h3>
<p>The general structure of a 256 color code is:</p>
<pre class="text"><code>code       :: ^[[(value)m
value      :: (attributes);(foreground);(background)
attributes :: attribute;attributes
attribute  :: 00|01|03|04|05|07|22|23|24|25|27
foreground :: 38;05;color
background :: 48;05;color
color      :: 000-255</code></pre>
<p>A few observations about the above:</p>
<ul>
<li>The parentheses aren’t really necessary.</li>
<li>Any leading zeroes can be skipped.</li>
<li>That is a literal <code>m</code> in the code.</li>
<li>The <code>^[[</code> is an escape key (U+001B) followed by a literal <code>[</code>.</li>
</ul>
<p>The attribute codes are one of the following:</p>
<table>
<thead>
<tr class="header">
<th align="center">Effect</th>
<th align="center">Code</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="center">Reset</td>
<td align="center">00</td>
</tr>
<tr class="even">
<td align="center">Bold</td>
<td align="center">01</td>
</tr>
<tr class="odd">
<td align="center">Italic</td>
<td align="center">03</td>
</tr>
<tr class="even">
<td align="center">Underline</td>
<td align="center">04</td>
</tr>
<tr class="odd">
<td align="center">Blink</td>
<td align="center">05</td>
</tr>
<tr class="even">
<td align="center">Reverse</td>
<td align="center">07</td>
</tr>
<tr class="odd">
<td align="center">No Bold</td>
<td align="center">22</td>
</tr>
<tr class="even">
<td align="center">No Italic</td>
<td align="center">23</td>
</tr>
<tr class="odd">
<td align="center">No Underline</td>
<td align="center">24</td>
</tr>
<tr class="even">
<td align="center">No Blink</td>
<td align="center">25</td>
</tr>
<tr class="odd">
<td align="center">No Reverse</td>
<td align="center">27</td>
</tr>
</tbody>
</table>
<p>The “No-” variations are codes to turn off the particular effect. They’re enormously useful if you have a lot of effects, you only want to turn off one and you don’t want to use reset followed by re-enabling everything else.</p>
<p>That’s pretty much all you need to start making use of them yourself. However, examples are always nice, so:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># Print text normally, in color 214, which happens to be a nice orange.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[38;05;214mHello, World&quot;</span>

<span class="co"># Make the same text bold.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[(01);(38;05;214)mHello, World&quot;</span>

<span class="co"># Print underlined and italicized text, with normal foreground, and blue</span>
<span class="co"># background.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[(03;04);(48;05;20)mHello, World&quot;</span>

<span class="co"># Bold, blinking purple text.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[(01;05);(38;05;93)mHello, World&quot;</span>

<span class="co"># Simple purple text on yellow background.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[(38;05;93);(48;05;226)mHello, World&quot;</span></code></pre></div>
<h3 id="a-note-on-prompt-usage">A Note on Prompt Usage</h3>
<p>If you want to use these colors and effects in your prompt, you can do so, but you will need to wrap each color code in a <code>%{%}</code> construct. This tells the shell that everything in between should not actually be printed, and terminal space should not be reserved for those characters.</p>
<h3 id="spectrum-a-zsh-abstraction">Spectrum, a ZSH Abstraction</h3>
<p>I’ve abstracted these codes and put them together in a zsh function which I call <code>spectrum</code>. The code for spectrum can be found as part of my <code>~/etc</code>, on <a href="http://github.com/sykora/etc/tree/master/zsh/functions/spectrum/">github</a>.</p>
<p>To use, first either source or autoload-and-run the spectrum script. It will set up three mappings, <code>FX</code>, <code>FG</code> and <code>BG</code>. <code>FX</code> is a mapping from effect names to their corresponding codes, and FG and BG are mappings from color number to codes. All numbers are 0-padded to 3 digits, so if you want light blue, use <code>$FG[000]</code>. The source is quite readable, take a look. Note that you can’t <em>run</em> the script, as this would execute it in a different shell, and the changes wouldn’t stick.</p>
<p>The above examples then, could be replicated as follows:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># Provided spectrum is in your $fpath,</span>
$<span class="kw">&gt;</span> <span class="kw">autoload</span> spectrum <span class="kw">&amp;&amp;</span> <span class="kw">spectrum</span>

<span class="co"># Print text normally, in color 214, which happens to be a nice orange.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FG[214]</span><span class="st">Hello, World&quot;</span>

<span class="co"># Make the same text bold.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FX[bold]$FG[214]</span><span class="st">Hello, World&quot;</span>

<span class="co"># Print underlined and italicized text, with normal foreground, and blue</span>
<span class="co"># background.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FX[italic]$FX[underline]$BG[020]</span><span class="st">Hello, World&quot;</span>

<span class="co"># Bold, blinking purple text.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FX[bold]$FX[blink]$FG[093]</span><span class="st">Hello, World&quot;</span>

<span class="co"># Simple purple text on yellow background.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FG[093]$BG[226]</span><span class="st">Hello, World&quot;</span></code></pre></div>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>Although I might specifically mention my terminal (rxvt-unicode) or my shell (zsh), the information here is largely portable across all such terminals and shells.<a href="#fnref1">↩</a></p></li>
</ol>
</div>
  </div>
  <footer>
  </footer>
</article>
]]></summary>
</entry>

</feed>
