What? β’ Why? β’ How? β’ TypeScript β’ Guide and examples β’ Help β’ Contribute
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:
npm install --save @nauverse/color-to-hsla
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.
Feel free to test and explore and if later on you need more guidance, read the whole guide and ask in the GitHub repo.
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.
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 |
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.
Currently, the package is distributed via NPM.
npm install --save @nauverse/color-to-hsla
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.
This library provides its own type definitions. "It just works", no need to install anything from @types
.
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:
import { colorToHSLA } from "@nauverse/color-to-hsla";
const myHSLAColor = colorToHSLA("#f00"); // { h: 0, s: 1, l: 0.5, a: 1 }
import { colorToHSLA } from "@nauverse/color-to-hsla";
const myHSLAColor = colorToHSLA("#ff0000"); // { h: 0, s: 1, l: 0.5, a: 1 }
import { colorToHSLA } from "@nauverse/color-to-hsla";
const myHSLAColor = colorToHSLA("rgb(255, 0, 0)"); // { h: 0, s: 1, l: 0.5, a: 1 }
import { colorToHSLA } from "@nauverse/color-to-hsla";
const myHSLAColor = colorToHSLA("rgb(100%, 0%, 0%)"); // { h: 0, s: 1, l: 0.5, a: 1 }
import { colorToHSLA } from "@nauverse/color-to-hsla";
const myHSLAColor = colorToHSLA("rgba(255, 0, 0, 1)"); // { h: 0, s: 1, l: 0.5, a: 1 }
import { colorToHSLA } from "@nauverse/color-to-hsla";
const myHSLAColor = colorToHSLA("rgba(100%, 0%, 0%, 1)"); // { h: 0, s: 1, l: 0.5, a: 1 }
import { colorToHSLA } from "@nauverse/color-to-hsla";
const myHSLAColor = colorToHSLA("hsl(120, 50%, 50%)"); // { h: 120, s: 0.5, l: 0.5, a: 1 }
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 }
import { colorToHSLA } from "@nauverse/color-to-hsla";
const myHSLAColor = colorToHSLA("transparent"); // { h: 0, s: 0, l: 0, a: 0 }
import { colorToHSLA } from "@nauverse/color-to-hsla";
const myHSLAColor = colorToHSLA("hihhoihoho"); // { h: 0, s: 0, l: 0, a: 0 }
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)"
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.
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 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!
Thanks goes to these wonderful people (emoji key):
Al | Naucode π π» π π§ |
This project follows the all-contributors specification. Contributions of any kind welcome!