Useful color tools for your next JavaScript project
Tags: javascript, webdev, npm, tooling
Published: 2022-12-08 09:38:42



## First game with Akali
First of all, I would like to point out that this post is the first publication of my life, so please don't be too critical. However, I would be happy with any reaction in the comment section. Even a simple `+1` or `-1` if you don't like to comment.

## Introduction
Anyway, I'm working on an Open Source project, and the question arose, what if I split the project into modules, to allow the modules to be used independently in other projects? So, I didn't hesitate and created a pretty package containing only the functions related to the colors.

I want to present this package to you, might you can use it in your next project. But, instead of copying the documentation here, which you can read on [GitHub](https://github.com/nextcss/color-tools) or [NPM](https://www.npmjs.com/package/@nextcss/color-tools), I'd like to point out its real-world usage.

## The Basics
First, let's look at the basics.

### Convert colors
It was clear that converting colors is always a sudden problem, and we spend unnecessary time on Google searches.

Currently, there are three functions available for this purpose, which support every 3-digit, 6-digit, and 8-digit hexadecimal color.

```js
hex2rgb(), rgb2hex(), hsl2hex()
```

At the moment, I've been able to solve all the cases with this kit as a Full Stack Developer.

### Random colors
Sometimes it may be necessary to generate a random color. And if we quickly create a short code, we're in for a surprise. What useless and ugly colors come out? Right? 

```js
randomHex(), randomRgb(), randomHsl()
```

<figure>
![Random colors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wq372e9ga3g7eqghy2ps.png)
<figcaption>Unsorted colors with default parameters</figcaption>
</figure>

As in all cases HSL colors are generated under the hood, it is possible to specify `saturation` and `lightness` parameters to the generators. You'll always get the colors you've always imagined.

## The Big Boys
And now, the stronger features.

### Brightness
How many times have you encountered the difficulty of deciding whether to use white or black text color based on the background color?

```js
brightness()
```

<figure>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7fo862gjythxm3ddr4b.png)
<figcaption>The brightness of some colors</figcaption>
</figure>

You can forget the problem forever because here's a tool that tells you whether a color is dark or light. Bang! One less problem.

### Tone Map
Creating color variations (`lighter`, `light`, `dark`, `darker`, etc.) for a brand is also a task that comes up in most projects. It is not as easy as it seems and a lot of unnecessary time is spent on it.

```js
toneMap()
```

<figure>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3h18sr3lpb9xd8m2c5li.png)
<figcaption>Range of 19 colors from a single color.</figcaption>
</figure>

This feature solves that problem and gives 18 variations, 9 darker and 9 lighter, so you never have to worry about palettes again.

### Color Shift
If you don't need complete palettes, just a simple color shift, well, use this feature for that purpose.

```js
colorShift()
```
<figure>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0xzzt0cbt0gmlt4hqmh.png)
<figcaption>Shift color by 20% in both directions</figcaption>
</figure>

Under the hood, a full RGB offset is applied, so you can be sure that the resulting color will match the original perfectly.

### Colorify
And finally, a super tool. If you've developed an application where you've managed user accounts with profile pictures, you already know that most users don't upload profile pictures.

In these cases, it is now almost standard practice to display the user's monogram in a colored circle as a profile picture.

```js
colorify()
```
<figure>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc4ao9lbjhdp8fptpxn9.png)
<figcaption>Colors generated from strings</figcaption>
</figure>

As there are HSL colors under the hood, it is possible to set the `saturation` and `lightness` parameters for the generators.

## Summary
As you have seen, a pretty useful package has been put together, so feel free to use it in any of your projects!

Related links:
- [GitHub → nextcss/color-tools](https://github.com/nextcss/color-tools)
- [NPM → @nextcss/color-tools](https://www.npmjs.com/package/@nextcss/color-tools)

## Final words
Thank you for your interest, I hope you enjoyed my article. I look forward to receiving your feedback in the comment section!

<small>
If you found this project interesting, please consider supporting my open source work by [sponsoring me](https://github.com/sponsors/toviszsolt) / [give the repo a star](https://github.com/nextcss/color-tools) / [follow the nextcss project](https://github.com/nextcss).
</small>