HTML & CSS
Article

Color Alchemy with Less: Creating Color Schemes and Palettes

By Ivaylo Gerchev

Color is one of the most important elements in any visual design. When properly used, it can have great impact on your web site or application. But knowing color theory solely is not enough to achieve such impact. You need to have the right tool belt to operate easily and successfully with the multitude of colors. Fortunately, Less solves this practical problem by providing plenty of color functions to work with.

In this tutorial, I’ll explore how to use some of these color functions, in conjunction with other Less features, to produce flexible and reusable mixins for color manipulation.

Creating Color Schemes

When attempting to create color schemes with Less, most people take the most obvious approach, which looks like this:

@base-color: #00ff00;
@triad-secondary: spin(@base-color, 120);
@triad-tertiary: spin(@base-color, -120);

This method uses variables and Less’s spin() function to create a color scheme (triadic, in our case). This works fine, but for me it’s not particularly reusable and not flexible enough. Fortunately, the issue can be resolved by using mixins. Let’s see what I mean.

.analog(@color, @variant, @property) {
  @first: spin(@color, 30);
  @second: spin(@color, -30);
  @list: @first, @second;
  @return: extract(@list, @variant);
  @{property}: @return;
}

.triad(@color, @variant, @property) {
  @first: spin(@color, 120);
  @second: spin(@color, -120);
  @list: @first, @second;
  @return: extract(@list, @variant);
  @{property}: @return;
}

.quad(@color, @variant, @property) {
  @first: spin(@color, 90);
  @second: spin(@color, -90);
  @third: spin(@color, 180);
  @list: @first, @second, @third;
  @return: extract(@list, @variant);
  @{property}: @return;
}

The above code creates three types of color schemes. I’ll explain only the last one, because the first two have the same structure and they don’t need individual explanations.

The .quad() mixin takes three parameters. The first one sets the base color for the scheme. The second one tells the mixin which color variant to return. And the third one defines which CSS property to use when Less compiles the code. Inside the mixin’s body, the spin() function creates the three available color variants in a quad scheme, then these variants are put in a list. The extract() function gets the desired variant, defined in the second parameter. And finally, with the help of variable interpolation, the color variant is assigned to the defined CSS property.

We can now put the above code in a separate file called color_schemes.less and use it as follows:

@import "color_schemes.less";

@base-color: #00ff00; 

div {
  width: 200px;
  height: 100px;
  border: thick solid;
  .quad(@base-color, 3, border-color);
  .quad(@base-color, 2, color);
  .quad(@base-color, 1, background-color);
}

Here we import the file with the color schemes, and then we define the base color for our website or application. The last three lines in the div rule set, define the colors for the border-color, color, and background-color properties.

As you can see, the mixin can be used with any property whose expected value is a color. Besides, it’s super easy to see for which property a particular statement is used; just take a look at the end, and boom, we know it. For example, in the last statement you can clearly see that the first color variant of the quad scheme will be used as the value for the background-color property. Pretty cool, huh?

And here is the compiled output:

div {
  /* ... etc... */
  border-color: #ff00ff;
  color: #ff8000;
  background-color: #007fff;
}

See the Pen xwxmeP by SitePoint (@SitePoint) on CodePen.

Generating Color Palettes

In this section, I’ll show you how to create different types of color palettes. For that purpose, I’ll use Less-specific loops and conditional statements (mixin guards). If you’re not familiar with those constructs, you can take a quick look at my previous articles on these topics.

In the first example, I’ll create a mixin that produces a color table. You’ve used a color picker, right? So, you know what I mean.

.color-palette(@color, @swatches, @step, @index: 0) when (@index =< (@swatches - 1)) {

  .swatch-@{index} {
    background-color: spin(@color, (@index * @step));
  }
  .color-palette(@color, @swatches, @step, (@index + 1));
}

.color-palette(#ff0000, 25, 15);

The .color-palette() mixin takes three actual arguments. The first one defines the base color for the palette. The second one defines how many swatches to generate. And the third one sets the spin step needed for the spin() function.

Actually, there is one more argument: @index. But it’s used only internally to make the loop work. As it’s set in the code above, the loop will iterate 25 times through the code, creating 25 CSS classes – one for each swatch. Each class name will be constructed following the .swatch-[number] pattern (for example, .swatch-1).

The color for each swatch is generated by using the value derived from the multiplication of the current index by the spin step. That value is added to the base color’s value for each iteration of the loop. This produces the full color spectrum, beginning and ending with the same color (red, in our case).

Here is the compiled output:

.swatch-0 {
  background-color: #ff0000;
}

.swatch-1 {
  background-color: #ff4000;
}

/* ...etc... */

.swatch-23 {
  background-color: #ff0040;
}
.swatch-24 {
  background-color: #ff0000;
}

See the Pen Generating a Color Palette with Less by SitePoint (@SitePoint) on CodePen.

This mixin can be used to create any kind of color table – with any number of swatches, and with a bigger or smaller spin step. For example, you can generate only four swatches with a spin step of 90 degrees, which will produce swatches for a square color scheme. You have endless possibilities. Just go ahead and do your own experiments.

In the next example, we’ll create a mixin that produces tints and shades of a particular color. According to Wikipedia:

a tint is the mixture of a color with white, which increases lightness, and a shade is the mixture of a color with black, which reduces lightness.

As we’ll see in a minute, tints and shades can be easily created with the help of Less’s lighten() and darken() built-in functions.

.color-palette(@type, @color, @index: 0) when (@index =< 9) {
  & when (@type = 'tints') {
    .swatch-@{index} {
      background-color: lighten(@color, (@index * 10%), relative);
    }
  }
  & when (@type = 'shades') {
    .swatch-@{index} {
      background-color: darken(@color, (@index * 10%), relative);
    }
  }
  .color-palette(@type, @color, (@index + 1));
}

.color-palette('shades', #ff00ff);

This version of the .color-palette() mixin takes two actual arguments – the type of the palette (shades or tints), and the base color. To make possible switching between shades and tints, the & operator is used in conjunction with the when keyword. This means that if we use “shades” as the first parameter, the code with the darken() function will be used.

The background color in both cases is generated by the lighten() or darken() function, respectively, which uses the defined base color and the current index multiplied by ten percent. Pay attention to the relative parameter. It’s important to include it so the adjustment is relative to the current value.

Note: Don’t worry that the two mixins share one and the same name. Thanks to the pattern-matching feature, Less will know which one to use.

Here is the compiled output:

.swatch-0 {
  background-color: #ff00ff;
}
.swatch-1 {
  background-color: #e600e5;
}

/* ...etc... */

.swatch-9 {
  background-color: #190019;
}

See the Pen Generating a Color Swatch with Less by SitePoint (@SitePoint) on CodePen.

Summary

There are many other things you can do with colors and with the great number of color functions provided by Less. But hey, you don’t want a 10,000-word tutorial, right? The given examples are enough to get you started and to give you an overview of the available possibilities. It’s up to you to dive deeper and continue experimenting. Happy Less coding!

  • http://careersreport.com Margaret Lan

    I will show excellent internet job opportunity… three-five hrs of work /a day… Payment at the end of each week… Bonuses…Payment of 6-9 thousand! dollars /a month… Merely several hrs of spare time, desktop or laptop, most basic knowing of web and dependable internet connection is what is needed…Get more information by visiting my page

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in Front-end, once a week, for free.