A challenge that any developer or designer will face is which color should be used on a site—a nice shade of red, or how about sea blue, for example? It doesn't matter whether they are responsible for choosing the shade to use, or if they have to pick the right RGB or HEX color to use.
Irrespective of where responsibilities lie, we still have to choose a color, and there is a good chance we won't be choosing one that comes from the default 256-color palette, but one that is likely to be a lighter or darker shade, or perhaps a mix of two colors:

Anyone used to working with SASS will already be aware of functions such as lighten(), darken() or saturate()—the great thing about PostCSS is that we can replicate similar functionality for those who want to move away from the dependency of SASS.
To see how easy it is to use, we're going to combine the power of two plugins for PostCSS—postcss-color-palette (available at https://github.com/zaim/postcss-color-palette), and postcss-color-mix (from https://github.com/iamstarkov/postcss-color-mix). The former allows us to choose one or more colors from any of three palettes, while postcss-color-mix will mix specific colors to make a new color. There are reasons for using these plugins, which will become clear; for now, let's get stuck in and watch these plugins in action.
In this exercise, we're going to take a look at mixing colors; postcss-color-palette allows us to choose multiple colors by name (and not by number!), then converts them to HEX equivalent values. We can then either create gradient-type effects, or simply mix the colors together (using postcss-color-mix) to produce a new color.
Let's make a start:
Tutorial24 folder from the code download that accompanies this book; save the folder to the root of our project area.Tutorial24 folder, copy the package.json and gulpfile.js files to the root of our project area.style – pre-compile.css file from the same folder and drop this into the src folder in our project area. Rename it as style.css.

gulp, then press Enter—PostCSS will go away and compile the style sheet, and drop the compiled results into the dest folder.dest folder (which will be the uncompressed and minified style sheets, along with a source map file) to the css folder within the Tutorial24 folder.index.html at the root of our Tutorial24 folder; if all is well, we should see our mixed colors, as shown in this screenshot:
Okay, the colors I've chosen clearly aren't going to win any style awards any time soon, but they help serve a purpose: it is very easy to use proper color names, if preferred, while still allowing PostCSS to compile them into valid HEX values. That aside, let's take a moment to consider the code we've used in this demo—it does raise a few key points, which we should cover, when using these plugins.
The demo we've created follows similar principles to most other demos we've built so far; we begin with declaring variables to store instances of our plugins, thus:
var palette = require('postcss-color-palette');
var colormix = require('postcss-color-mix')The magic then happens in this task, within our gulp file:
gulp.task('palette', function () {
return gulp.src('src/*.css')
.pipe(postcss([ autoprefixer, palette({ palette: 'mrmrs' }), colormix() ]))
.pipe(gulp.dest('dest/'));
});Notice that we've specified a palette to use, the mrmrs option is the default, but we can equally use material or flatui as alternatives. All three reference the webcolors plugin from https://github.com/zaim/webcolors/; this package could be expanded to include other palettes if desired.
With the links to our two plugins in place, and the task set up, we can then begin to specify rules within our style sheet, which will use the plugins. We've created three, and all three use the postcss-color-palette to determine what the HEX value should be for each color; the third and final mixes the two colors together once HEX values have been assigned:
#box0 { background: linear-gradient(aqua, blue 50%, purple); }
#box1 { background: linear-gradient(to right, orange, red, yellow); }
#box2 { background: mix(teal, navy, 80%); }Getting the mix of the color right for the third rule isn't easy, the key to a successful mix is to avoid using colors that are in the same spectrum; the closer they are, the less impact the mix will have!
If you want a quick way to gauge how well colors have mixed, then try http://jackiebalzer.com/color—this demo has a mix() option in it, which will compile them in the browser and avoid the need to run the compilation process manually.
We've covered some of the plugins that are likely to be more popular; there are more available via the PostCSS.parts directory, which may be of interest:
colorguard: Helps maintain a consistent color palettepostcss-ase-colors: Replaces color names with values read from an ASE palette file; this is perfect if you happen to be a user of Adobe PhotoShop, InDesign, or Illustratorpostcss-shades-of-gray: Helps keep grayscale colors consistent to a gray palettepostcss-color-pantone: Transforms Pantone color to RGB.In the meantime, let's move on: we've explored using palettes to select our colors, before mixing them to create new ones. This is just scratching the surface of what is possible; how about creating different shades of colors, using functions such as darken(), tint() or lightness()? Such functions already exist in most preprocessors, such as SASS; let's explore how we can achieve the same results using PostCSS plugins.