title: Xlib RGB hex strings: have I understood "scaling in bits"?
body: <p>The xlib docs present two string formats for specifying RGB colours:</p>
  <blockquote>
  <h3>RGB Device String Specification</h3>
  <p>An RGB Device specification is
  identified by the prefix “rgb:” and conforms to the following syntax:</p>
  <pre><code>rgb:&lt;red&gt;/&lt;green&gt;/&lt;blue&gt;
  
      &lt;red&gt;, &lt;green&gt;, &lt;blue&gt; := h | hh | hhh | hhhh
      h := single hexadecimal digits (case insignificant)
  </code></pre>
  <p>Note that <code>h</code> indicates the value scaled in 4 bits, <code>hh</code> the value scaled in 8 bits,
  <code>hhh</code> the value scaled in 12 bits, and <code>hhhh</code> the value scaled in 16 bits,
  respectively.</p>
  <p>Typical examples are the strings “rgb:ea/75/52” and “rgb:ccc/320/320”,
  but mixed numbers of hexadecimal digit strings (“rgb:ff/a5/0” and
  “rgb:ccc/32/0”) are also allowed.</p>
  <p>For backward compatibility, an older syntax for RGB Device is
  supported, but its continued use is not encouraged. The syntax is an
  initial sharp sign character followed by a numeric specification, in
  one of the following formats:</p>
  <pre><code>#RGB          (4 bits each)
  #RRGGBB       (8 bits each)
  #RRRGGGBBB    (12 bits each)
  #RRRRGGGGBBBB (16 bits each)
  </code></pre>
  <p>The R, G, and B represent single hexadecimal digits. When fewer than 16 bits each are specified, they
  represent the most significant bits of the value (unlike the “rgb:”
  syntax, in which values are scaled). For example, the string “#3a7” is
  the same as “#3000a0007000”.</p>
  </blockquote>
  <p><a href="https://www.x.org/releases/X11R7.7/doc/libX11/libX11/libX11.html#RGB_Device_String_Specification" rel="nofollow noreferrer">https://www.x.org/releases/X11R7.7/doc/libX11/libX11/libX11.html#RGB_Device_String_Specification</a></p>
  <p>So there are strings like either <code>rgb:c7f1/c7f1/c7f1</code> or <code>#c7f1c7f1c7f1</code></p>
  <p>The part that confuses me is:</p>
  <blockquote>
  <p>When fewer than 16 bits each are specified, they represent the most significant bits of the value (unlike the “rgb:” syntax, in which values are scaled)</p>
  </blockquote>
  <p>Let's say we have 4-bit colour strings, i.e. one hexadecimal digit per component.</p>
  <p><code>C</code> is <code>12</code> in decimal, out of <code>16</code>.</p>
  <p>If we want to scale it up to 8-bits, I guess you have <code>12 / 16 * 256 = 192</code></p>
  <p><code>192</code> in hexadecimal is <code>C0</code></p>
  <p>So this seems to give exactly the same result as &quot;the most significant bits of the value&quot; i.e. padding with <code>0</code> to the right-hand side.</p>
  <p>Since the docs make a distinction between the two formats I wondered if I have misunderstood what they mean by &quot;scaling in bits&quot; for the <code>rgb:</code> format?</p>