Skip to content

πŸ§ͺ A tiny typesafe library to convert any color in any format to HSLA.

License

Notifications You must be signed in to change notification settings

TheNaubit/color-to-hsla

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ§ͺ color-to-hsla

color-to-hsla

A tiny typesafe library to convert any color in any format to HSLA.

Build Status npm version minzipped size

What? β€’ Why? β€’ How? β€’ TypeScript β€’ Guide and examples β€’ Help β€’ Contribute

tl;dr

If you just want to try and you don't want to read this guide right now (although you should in the future if you decide to use the library), you can start quickly by:

1. Installing the dependency:

npm install --save @nauverse/color-to-hsla

2. Checking this example of use:

import { colorToHSLA, hslaToString } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("#ff0000"); // { h: 0, s: 1, l: 0.5, a: 1 }
const myHSLAString = hslaToString(myHSLAColor); // "hsla(0, 100%, 50%, 1)"

If you want to see more examples, jump to here.

3. You are done! πŸͺ„

Feel free to test and explore and if later on you need more guidance, read the whole guide and ask in the GitHub repo.

What?

color-to-hsla is heavily inspired in the awesome color-to-hsla and parts of the code where adapted from it. It is a tiny JavaScript library that makes converting from any color format to the HSLA format really easy.

It works with hsl, rgb with numbers and percentages, rgba with numbers and percentages, hex3 colors, hex6 colors and even default CSS colors like aliceblue. This tool can be really useful to standarize colors to a single format.

Features

v1
0️⃣ β€Ž Zero dependencies
πŸ‘Œ <3kB minified and gzipped
πŸ”’ Reliable. Even when you pass really messed up colors (mixed case) or even invalid ones, the library does not crash, it always return something
✍️ Support for hsl, hsla, rgb (with numbers and percentages), rgba (with numbers and percentages), hex3, hex6 and even CSS default colors
πŸ›Ÿ Full of tests
βœ… Production ready

Why?

Sometimes you need to handle colors and you need to suport several formats but you do not want to be detecting/guessing all the time how to work with those colors.

An usual good way to handle that is to convert every color to a single format. This tool helps you with that, so you do not have to handle that tricky logic every time you need that functionality.

How?

Install

Currently, the package is distributed via NPM.

npm install --save @nauverse/color-to-hsla

Usage with Node

Node 18 and above are officially supported, though you may have luck using it with an earlier Node version.

The package comes with CJS and ESM modules.

TypeScript

This library provides its own type definitions. "It just works", no need to install anything from @types.

Guide and examples

A good contribution for this repo would be a more detailed guide about how to use it.

The most important function that this package offers is colorToHsla. Let's see some examples:

HEX3 color to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("#f00"); // { h: 0, s: 1, l: 0.5, a: 1 }

HEX6 color to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("#ff0000"); // { h: 0, s: 1, l: 0.5, a: 1 }

RGB color with numbers to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("rgb(255, 0, 0)"); // { h: 0, s: 1, l: 0.5, a: 1 }

RGB color with percentages to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("rgb(100%, 0%, 0%)"); // { h: 0, s: 1, l: 0.5, a: 1 }

RGBA color with numbers to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("rgba(255, 0, 0, 1)"); // { h: 0, s: 1, l: 0.5, a: 1 }

RGBA color with percentages to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("rgba(100%, 0%, 0%, 1)"); // { h: 0, s: 1, l: 0.5, a: 1 }

HSL color to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("hsl(120, 50%, 50%)"); // { h: 120, s: 0.5, l: 0.5, a: 1 }

HSLA color to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("hsla(120, 50%, 50%, 1)"); // { h: 120, s: 0.5, l: 0.5, a: 1 }

Transparent color to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("transparent"); // { h: 0, s: 0, l: 0, a: 0 }

Invalid color to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("hihhoihoho"); // { h: 0, s: 0, l: 0, a: 0 }

CSS default color to HSLA object

import { colorToHSLA } from "@nauverse/color-to-hsla";

const myHSLAColor = colorToHSLA("azure"); // { h: 180, s: 1, l: 0.97, a: 1 }

There is another function, hslaToString, which allows you to convert any HSLA object to a valid CSS string:

import { hslaToString } from "@nauverse/color-to-hsla";

const myHSLAString = hslaToString({
      h: 120,
      s: 0.5,
      l: 0.5,
      a: 0.1
    }); // "hsla(120, 50%, 50%, 0.1)"
import { hslaToString } from "@nauverse/color-to-hsla";

const myHSLAString = hslaToString({
      h: 120,
      s: 0.5,
      l: 0.5,
      a: 1
    }); // "hsla(120, 50%, 50%, 1)"

Help

Thank you for using color-to-hsla!

If you need any help using this library, feel free to create a GitHub issue, and ask your questions. I'll try to answer as quickly as possible.

Contribute

Contributions of any kind (pull requests, bug reports, feature requests, documentation, design) are more than welcome! If you like this project and want to help, but feel like you are stuck, feel free to contact the maintainers.

Building from source

Building the project should be quick and easy. If it isn't, it's the maintainer's fault. Please report any problems with building in a GitHub issue.

You need to have a reasonably recent version of node.js to build color-to-hsla. Tested on node version 18.0.0 and npm version 8.6.0.

First, clone the git repository:

git clone [email protected]:TheNaubit/color-to-hsla.git

Then switch to the newly created color-to-hsla directory and install the dependencies:

cd color-to-hsla
npm install

You can then run the unit tests to verify that everything works correctly:

npm run test:run

And finally, build the library:

npm run build

The output will appear in the dist directory.

Happy hacking!

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):

Al | Naucode
Al | Naucode

πŸ› πŸ’» πŸ“– 🚧 ⚠️ πŸš‡

This project follows the all-contributors specification. Contributions of any kind welcome!