<?xml version="1.0" encoding="utf-8" standalone="yes"?><?xml-stylesheet href="/pretty-feed-v3.xsl" type="text/xsl"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Snippets by Pranshu Gaba</title><link>https://pranshugaba.com/tags/snippets/</link><description/><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>pranshu@pranshugaba.com (Pranshu Gaba)</managingEditor><webMaster>pranshu@pranshugaba.com (Pranshu Gaba)</webMaster><copyright>This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.</copyright><lastBuildDate>Sat, 23 May 2026 16:56:00 +0530</lastBuildDate><image><url>https://pranshugaba.com/tags/snippets/icon.png</url></image><atom:link href="https://pranshugaba.com/tags/snippets/index.xml" rel="self" type="application/rss+xml"/><item><title>Hugo Colour Shortcode</title><link>https://pranshugaba.com/dev/hugo-colour-shortcode/</link><pubDate>Wed, 21 Jul 2021 17:30:00 +0530</pubDate><guid>https://pranshugaba.com/dev/hugo-colour-shortcode/</guid><description>
<![CDATA[<p>I was writing a <a href="https://pranshugaba.com/dev/moving-back-to-hugo/">post about my new Hugo website</a>, and I wanted to describe the colours used on this site. I wrote the names of the colours, but names do not convey the colour very well. Can you imagine what the colour #714825 looks like? What about rgb(0, 100, 255)?</p>
<p>I thought it would be great if the reader could also see a preview of the colours. I could add images of the colours that I am describing, but that is a lot of work and not very flexible.  This is what led me to write this Hugo shortcode. I followed the <a href="https://gohugo.io/templates/shortcode-templates/" target="_blank" rel="noopener">Hugo docs on shortcodes</a> for reference.</p>
<h2 id="usage">Usage</h2>
<p>The following code in markdown
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">This is a colour shortcode {{&lt; colour &#34;#ff0000&#34; &gt;}}.</span></span></code></pre></div></p>
<p>renders as</p>
<p>This is a colour shortcode 
<code style="background-color: #ff0000;"> 
<span style="color: #ff0000;filter:  grayscale(1) invert(1) contrast(100);">#ff0000</span></code>.</p>
<p>This works with all valid CSS colour syntaxes, not just hexcodes. Here are some examples:</p>
<ul>
<li>
<code style="background-color: #714285;"> 
<span style="color: #714285;filter:  grayscale(1) invert(1) contrast(100);">#714285</span></code></li>
<li>
<code style="background-color: rgb(0, 100, 255);"> 
<span style="color: rgb(0, 100, 255);filter:  grayscale(1) invert(1) contrast(100);">rgb(0, 100, 255)</span></code></li>
<li>
<code style="background-color: hsl(37.1, 100%, 91.8%);"> 
<span style="color: hsl(37.1, 100%, 91.8%);filter:  grayscale(1) invert(1) contrast(100);">hsl(37.1, 100%, 91.8%)</span></code></li>
<li>
<code style="background-color: aquamarine;"> 
<span style="color: aquamarine;filter:  grayscale(1) invert(1) contrast(100);">aquamarine</span></code></li>
</ul>
<h2 id="shortcode">Shortcode</h2>
<p>I created a file named <code>colour.html</code> in <code>layouts/shortcodes</code>, and added the following code in it. We can now access this code in any markdown file using the <code>colour</code> shortcode.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="p">{{</span><span class="w"> </span><span class="err">$</span><span class="nx">colour</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">.</span><span class="nx">Get</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="nx">safeCSS</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nx">code</span><span class="w"> </span><span class="nx">style</span><span class="p">=</span><span class="s">&#34;background-color: {{ $colour }};&#34;</span><span class="p">&gt;</span><span class="w"> 
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="p">&lt;</span><span class="nx">span</span><span class="w"> </span><span class="nx">style</span><span class="p">=</span><span class="s">&#34;
</span></span></span><span class="line"><span class="cl"><span class="s">      color: {{ $colour }};
</span></span></span><span class="line"><span class="cl"><span class="s">      filter:  grayscale(1) invert(1) contrast(100);
</span></span></span><span class="line"><span class="cl"><span class="s">      &#34;</span><span class="p">&gt;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="p">{{</span><span class="w"> </span><span class="err">$</span><span class="nx">colour</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="p">&lt;</span><span class="o">/</span><span class="nx">span</span><span class="p">&gt;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">&lt;</span><span class="o">/</span><span class="nx">code</span><span class="p">&gt;</span><span class="w">
</span></span></span></code></pre></div><p>The shortcode takes in an argument: <code>$colour</code>.  We use this parameter to set the text in the <code>code</code> tag, and we set both the text colour and the background colour to <code>$colour</code>. We then invert the colour of the text and increase the contrast to make the name of the colour legible. The end result is that the text is white if colour is bright, and black if it is dark.</p>



<aside class="admonition note">
    <div class="admonition__title">
        Note
    </div>
	<div class="admonition__content">
        <p>While the shortcode syntax is specific to Hugo, most templating languages have a way to pass in variables. It should be possible to adapt this code for the templating language that you use.</p>

    </div>
</aside>
<h2 id="limitations">Limitations</h2>
<p>This works well in most cases, but it has some limitations.</p>
<ul>
<li>
<p>When the luminosity of the colour is close to 50% and the saturation is low, neither black nor white text has enough contrast with the chosen colour, and the text is not as legible. Example: 
<code style="background-color: hsl(180, 10%,  50%);"> 
<span style="color: hsl(180, 10%,  50%);filter:  grayscale(1) invert(1) contrast(100);">hsl(180, 10%,  50%)</span></code></p>
</li>
<li>
<p>The text is not legible when the colour is transparent. This is because the text is transparent too, and does not have enough contrast with the background. Example: 
<code style="background-color: rgba(120, 0, 240, 0.3);"> 
<span style="color: rgba(120, 0, 240, 0.3);filter:  grayscale(1) invert(1) contrast(100);">rgba(120, 0, 240, 0.3)</span></code></p>
</li>
</ul>
<h2 id="alternate-style">Alternate style</h2>
<p>Here&rsquo;s another style of previewing colours.</p>
<ul>
<li>
<code>
<span
    style="display: inline-block;
    width: 1.6ch;
    height: 1.6ch;
    border-radius: 0.1rem;
    background-color: #714285;
    margin-right:5px;"
    ></span>#714285</code>
</li>
<li>
<code>
<span
    style="display: inline-block;
    width: 1.6ch;
    height: 1.6ch;
    border-radius: 0.1rem;
    background-color: rgb(0, 100, 255);
    margin-right:5px;"
    ></span>rgb(0, 100, 255)</code>
</li>
<li>
<code>
<span
    style="display: inline-block;
    width: 1.6ch;
    height: 1.6ch;
    border-radius: 0.1rem;
    background-color: hsl(37.1, 100%, 91.8%);
    margin-right:5px;"
    ></span>hsl(37.1, 100%, 91.8%)</code>
</li>
<li>
<code>
<span
    style="display: inline-block;
    width: 1.6ch;
    height: 1.6ch;
    border-radius: 0.1rem;
    background-color: aquamarine;
    margin-right:5px;"
    ></span>aquamarine</code>
</li>
</ul>
<p>The text is always legible, but the colour preview is smaller.</p>
<p>Here&rsquo;s the shortcode for this style:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="p">{{</span><span class="w"> </span><span class="err">$</span><span class="nx">colour</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">.</span><span class="nx">Get</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="nx">safeCSS</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nx">code</span><span class="p">&gt;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nx">span</span><span class="w"> </span><span class="nx">style</span><span class="p">=</span><span class="s">&#34;display: inline-block;
</span></span></span><span class="line"><span class="cl"><span class="s">    width: 1.6ch;
</span></span></span><span class="line"><span class="cl"><span class="s">    height: 1.6ch;
</span></span></span><span class="line"><span class="cl"><span class="s">    border-radius: 0.1rem;
</span></span></span><span class="line"><span class="cl"><span class="s">    background-color: {{ $colour }};
</span></span></span><span class="line"><span class="cl"><span class="s">    margin-right:5px;&#34;</span><span class="p">&gt;&lt;</span><span class="o">/</span><span class="nx">span</span><span class="p">&gt;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="p">{{</span><span class="w"> </span><span class="err">$</span><span class="nx">colour</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">&lt;</span><span class="o">/</span><span class="nx">code</span><span class="p">&gt;</span><span class="w">
</span></span></span></code></pre></div><hr>
<p><a href="https://pranshugaba.com/contact/">Let me know</a> if you liked this shortcode, and which style you liked more. Feel free to use this code in your site and share your implementation with me. If you have any suggestions to improve this code, share those with me too.</p>
]]></description></item></channel></rss>