<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mihael Tomić</title>
    <description>The latest articles on DEV Community by Mihael Tomić (@tomic_mihael).</description>
    <link>https://dev.to/tomic_mihael</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F247737%2F3a7fad16-1827-4a4e-8495-72514a0d08a3.jpg</url>
      <title>DEV Community: Mihael Tomić</title>
      <link>https://dev.to/tomic_mihael</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tomic_mihael"/>
    <language>en</language>
    <item>
      <title>Supercharge your colors with the PostCSS color() function</title>
      <dc:creator>Mihael Tomić</dc:creator>
      <pubDate>Fri, 25 Oct 2019 09:17:20 +0000</pubDate>
      <link>https://dev.to/monosoftware/supercharge-your-colors-with-the-postcss-color-function-3be8</link>
      <guid>https://dev.to/monosoftware/supercharge-your-colors-with-the-postcss-color-function-3be8</guid>
      <description>&lt;p&gt;Colors provide an atmosphere crucial to our design. They communicate design intentions and send a message to the people. &lt;/p&gt;

&lt;p&gt;In this article, we will take a look how using plugins for &lt;a href="https://github.com/postcss/postcss" rel="noopener noreferrer"&gt;PostCSS&lt;/a&gt; can help us in defining a color theme for our website and how to use colors across our project.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the color function?
&lt;/h2&gt;

&lt;p&gt;CSS color function is a &lt;a href="https://drafts.csswg.org/css-color/#modifying-colors" rel="noopener noreferrer"&gt;W3C draft&lt;/a&gt; proposed by &lt;a href="https://github.com/tabatkins" rel="noopener noreferrer"&gt;Tab Atkins&lt;/a&gt; which will help us to modify base color with the assistance of color-adjusters like hue, saturation, lightness, tint, opacity, etc. If you are familiar with a graphic software like Photoshop, Affinity Photo or any similar software and its adjustments tools, this will sound familiar.&lt;/p&gt;

&lt;p&gt;In our case we will use a tool called &lt;a href="https://github.com/postcss/postcss-color-function" rel="noopener noreferrer"&gt;postcss-color-function&lt;/a&gt; - &lt;em&gt;a plugin to transform &lt;a href="http://dev.w3.org/csswg/css-color/#modifying-colors" rel="noopener noreferrer"&gt;W3C CSS color function&lt;/a&gt; to more compatible CSS.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;What is so wonderful about this? After browsers start to fully support specification and implement it, we can kick out the plugins and start using browser native CSS parsing for color function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining our color theme
&lt;/h2&gt;

&lt;p&gt;To create a functional color theme, we have to define the right colors for our project. The initial setup is important since it will help us create a starting point for simple color usage across all the (Post)CSS files.&lt;/p&gt;

&lt;p&gt;We will use a color naming tool from &lt;a href="http://www.color-blindness.com/color-name-hue/" rel="noopener noreferrer"&gt;Colblindor&lt;/a&gt; to assign a name to our hex colors. For this demo, we will use color palette created from the Star Wars™ The Last Jedi poster.&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-886335965744082944-986" src="https://platform.twitter.com/embed/Tweet.html?id=886335965744082944"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-886335965744082944-986');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=886335965744082944&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkxidk4t3m38i01ahiaru.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkxidk4t3m38i01ahiaru.png" alt="Color scheme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we will define variables* using the &lt;a href="https://github.com/postcss/postcss-simple-vars" rel="noopener noreferrer"&gt;postcss-simple-vars&lt;/a&gt; plugin which gives us the ability to write and use JS-like variables we're familiar from &lt;code&gt;Sass&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** Brand colors **/
$cinnabar: #DF3D2F;
$seal-brown: #080202;

/** Theme colors **/
$color-primary: $cinnabar;
$color-secondary: $seal-brown;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're finished with our setup. From now on - we will use only theme colors in our project.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;You can also use CSS variables which are W3C CR and not fully supported by all browsers. In our workflow we use PostCSS, and I'll use it in the following examples.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical use cases
&lt;/h2&gt;

&lt;p&gt;There's a lot of stuff we can do with color functions, and I'll cover some of them in following examples. I won't include all the adjusters; you can find them in documentation and explore in detail by yourself. &lt;/p&gt;

&lt;p&gt;The color function accepts many color spaces - you can use RGB, HSL/hwb, browser colors and HEX values and manipulate them.&lt;/p&gt;

&lt;p&gt;With the right combination of PostCSS plugins (for nesting, variables and color function) we can quickly create consistent and easily maintainable pseudo states for buttons in our project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Button pseudo state&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our HTML code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button type="submit"&amp;gt;Join me!&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;combined with our PostCSS...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button {
  ...
  box-shadow: 1px 1px 3px color(black a(0.2));
  background-color: $color-primary;

  &amp;amp;:hover,
  &amp;amp;:active {
    background-color: color($color-primary lightness(+5%));
    box-shadow: 0px 3px 10px color(black a(0.4));
  }

  &amp;amp;:focus {
    background-color: color(black blackness(+10%));
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...gives us a great level of control over the &lt;code&gt;button&lt;/code&gt; element. We can change our base colors as many times as we want without thinking about what will happen with the pseudo states.&lt;/p&gt;

&lt;p&gt;As you can see from compiled CSS, all values are &lt;em&gt;readable&lt;/em&gt; for all browsers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button {
  box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
  background-color: #DF3D2F;
}

button:hover,
  button:active {
  background-color: rgb(226, 83, 70);
  box-shadow: 0px 3px 10px rgba(0, 0, 0, 0.4);
}

button:focus {
  background-color: rgb(196, 58, 46);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;View functional example on CodePen:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/mihaeltomic/embed/przMrG/?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Using color function with postcss-inline-svg
&lt;/h2&gt;

&lt;p&gt;One of the tools we use in our everyday projects is &lt;a href="https://github.com/TrySound/postcss-inline-svg" rel="noopener noreferrer"&gt;postcss-inline-svg&lt;/a&gt; - "&lt;em&gt;a plugin to inline SVG and customize its styles&lt;/em&gt;". I find the combination of these two plugins handy. Since CodePen does not support inline-svg plugin, I'll try to reproduce it through images and code examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source PostCSS&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.icon--empire {
    background-image: svg-load(../images/icon-delete.svg, fill: color($color-secondary lightness(+30%)));

  &amp;amp;:hover {
      background-image: svg-load(../images/icon-delete.svg, fill=$color-primary);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Compiled CSS&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.icon--empire {
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg fill='%23030303' height='24' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z'/%3E%3Cpath d='M0 0h24v24H0z' fill='none'/%3E%3C/svg%3E");

  &amp;amp;:hover {
      background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg fill='%23DF3D2F' height='24' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z'/%3E%3Cpath d='M0 0h24v24H0z' fill='none'/%3E%3C/svg%3E");

  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostCSS would produce an SVG icon in &lt;code&gt;base64&lt;/code&gt; after compiling. &lt;/p&gt;

&lt;p&gt;&lt;a href="/img/blog/SuperchargeColorFunction/empire-crest.svg" class="article-body-image-wrapper"&gt;&lt;img src="/img/blog/SuperchargeColorFunction/empire-crest.svg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The good&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The good thing about this approach is that you can generate icons easily through CSS and have color control for fill and stroke. All icons are in one place and can be used across as utils.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The bad&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There's no way (as far as I know) to add CSS classes and animate SVG through CSS paths or other elements in SVG. Also, there's no SVG code in our markup.&lt;/p&gt;

&lt;h2&gt;
  
  
  To wrap it up
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;color() function&lt;/strong&gt; is a &lt;strong&gt;powerful tool&lt;/strong&gt; and can be used in many creative ways. After browsers start to support it fully, we will see its full potential. For now, we have tools to help us unlock that potential and use its benefits today.&lt;/p&gt;




&lt;p&gt;This one is an old article I wrote for &lt;a href="https://mono.software/2017/08/08/supercharge-your-colors/" rel="noopener noreferrer"&gt;Mono&lt;/a&gt;, but I feel it also belongs here, on Dev.to community.&lt;/p&gt;

</description>
      <category>postcss</category>
      <category>css</category>
      <category>color</category>
      <category>svg</category>
    </item>
  </channel>
</rss>
