Color Alchemy with Less: Creating Color Schemes and Palettes

Share this article

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!

Frequently Asked Questions about Color Alchemy with LESS

What is Color Alchemy with LESS and how does it work?

Color Alchemy with LESS is a method of creating color schemes and palettes using LESS, a dynamic preprocessor style sheet language. It allows you to define variables, mixins, functions, and many other techniques that allow you to make CSS that is more maintainable, themable, and extendable. With LESS, you can create a base color and then use functions to lighten, darken, saturate, desaturate, spin, and mix colors, creating a harmonious color scheme for your website or application.

How can I create a color scheme using LESS?

Creating a color scheme using LESS involves defining a base color and then using LESS functions to create variations of that color. For example, you can use the lighten and darken functions to create lighter and darker shades of the base color. You can also use the saturate and desaturate functions to adjust the intensity of the color, and the spin function to create complementary colors. By combining these functions, you can create a wide range of color schemes.

What are the benefits of using LESS for color alchemy?

LESS offers several benefits for color alchemy. It allows you to create color schemes that are consistent and harmonious, which can enhance the visual appeal of your website or application. It also makes it easy to adjust your color scheme, as you can simply change the base color and the rest of the colors will automatically update. Additionally, LESS allows you to create color schemes that are scalable and reusable, which can save you time and effort in the long run.

How does LESS compare to other CSS preprocessors for color alchemy?

LESS is similar to other CSS preprocessors like SASS in terms of its ability to create color schemes. However, LESS has a simpler syntax and is easier to learn, making it a good choice for beginners. It also has a robust set of functions for manipulating colors, which can give you more control over your color scheme.

Can I use Color Alchemy with LESS for print design?

While LESS is primarily used for web design, you can also use it for print design. You can create a color scheme using LESS and then export it as a CSS file, which can be used in print design software. However, keep in mind that colors may appear differently on screen and in print due to differences in color spaces.

How can I learn more about Color Alchemy with LESS?

There are many resources available online to learn more about Color Alchemy with LESS. You can start by reading the official LESS documentation, which provides a comprehensive overview of the language and its features. There are also many tutorials and guides available that can walk you through the process of creating a color scheme with LESS.

Can I use Color Alchemy with LESS for mobile app design?

Yes, you can use Color Alchemy with LESS for mobile app design. LESS allows you to create a consistent color scheme that can be used across different platforms, including mobile apps. This can help ensure a consistent user experience across all platforms.

What tools do I need to use LESS for color alchemy?

To use LESS for color alchemy, you will need a text editor to write your LESS code and a LESS compiler to compile your LESS code into CSS. There are many free and paid text editors and LESS compilers available, so you can choose the ones that best fit your needs and preferences.

Can I use Color Alchemy with LESS if I’m color blind?

Yes, you can use Color Alchemy with LESS even if you’re color blind. LESS allows you to create color schemes based on mathematical functions, so you don’t need to rely on your perception of color. However, you may want to get feedback from others to ensure your color scheme is visually appealing and accessible to all users.

How can I troubleshoot issues with my LESS color scheme?

If you’re having issues with your LESS color scheme, there are several steps you can take. First, check your LESS code for errors. If your code is correct, try adjusting your base color or the parameters of your functions. If you’re still having issues, consider seeking help from the LESS community or a professional web designer.

Ivaylo GerchevIvaylo Gerchev
View Author

I am a web developer/designer from Bulgaria. My favorite web technologies include SVG, HTML, CSS, Tailwind, JavaScript, Node, Vue, and React. When I'm not programming the Web, I love to program my own reality ;)

AdvancedCSSLess MixinsLouisL
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week