⭐⭐⭐⭐⭐
Ethereal Color
Library Website
Sample Project
Fast, simple, easy library to work with colors: single, palettes, gradients and more. Written in Typescript
Try demo
Visite library web page clicking here
Or, visit a demo project clicking here
Installation
1. Package
Npm
npm i ethereal-color
Yarn
yarn add ethereal-color
2. CDN
Quickstart
1. Package
~/index.html
Ethereal Color Quickstart with Package
~/app.js
// ES6; // CommomJSconst EtherealColor = ; const Color Palette Gradient Converter = EtherealColor; // ...Now it's up to you, enjoy this API
2. CDN
~/index.html
Ethereal Color Quickstart with CDN
Color Types
For all examples below, RGB will be used, but you can also use the Hexadecimal or HSL color format
Color Format | Color Key | Color Object Type | Color String |
---|---|---|---|
RGB | rgb | Rgb : { r: number, g: number, b: number } |
rgb(R, G, B) |
Hexadecimal | hex | Hex : { r: string, g: string, b: string } |
#rrggbb |
HSL | hsl | Hsl : { h: number, s: number, l: number } |
hsl(H, S%, L%) |
Color Function
Default return
If you call the Color function without sending any arguments, white will be returned
const color = ; colorstring; // rgb(255, 255, 255)
1. Color API Type (ColorAPI)
2. Create a Color API
A Color API is used to represent a separate color, a single color within that infinity of colors that exist
// Way 1 - Using default color: rgb(255, 255, 255)const color = ; colorstring; // rgb(255, 255, 255)colorobject; // { r: 255, g: 255, b: 255 } // Way 2 - Using custom color: Red, for exampleconst color = ; colorstring; // rgb(255, 0, 0)colorobject; // { r: 255, g: 0, b: 0 } // Way 3 - Using custom color object: Blue, for exampleconst color = ; colorstring; // rgb(0, 0, 255)colorobject; // { r: 0, g: 0, b: 255 }
3. Change the color of an object at run time
const color = ; colorstring; // rgb(255, 255, 255) color; colorstring; // rgb(150, 150, 150)
4. Generate a random color
// Way 1 - A totally random colorconst color = ;color; colorstring; // rgb(??, ??, ??) // Way 2 - Within the range of a paletteconst palette = ;// Start: rgb(120, 100, 160)// End: rgb(130, 180, 200) const color = ;color; colorstring; // rgb(X, Y, Z)// 120 <= X <= 130// 100 <= Y <= 180// 160 <= Z <= 200
Palette Function
Default return
If you try to create a palette without sending anything as a parameter, a palette will be created using white as the base
// this...const palette = ; // ...is the same thing as this:const palette = ;// ...then:// Start: rgb(215, 215, 215)// End: rgb(255, 255, 255)
1. Palette API Type (PaletteAPI)
2. Create a Palette API
A Palette API is used to reference a range of colors, that is, where a certain range begins, and where it ends
// Way 1 - Using defaut range: 40const color = ; const palette = ;// Start: rgb(110, 110, 110)// End: rgb(190, 190, 190) // Way 2 - Using custom range: 100const color = ; const palette = ;// Start: rgb(50, 50, 50)// End: rgb(250, 250, 250) // Way 3 - Create a custom paletteconst startColor = ;const endColor = ; const palette = ;// Start: rgb(65, 65, 0)// End: rgb(230, 230, 230)
3. Change the value of the palette at run time
const color = ; const palette = ;// Start: rgb(80, 110, 110)// End: rgb(160, 190, 190) const otherColor = ; palette;// Start: rgb(10, 30, 0)// End: rgb(30, 50, 20)
4. Generate a random palette
const palette = ; // default color: white - rgb(255, 255, 255)// Start: rgb(215, 215, 215)// End: rgb(255, 255, 255) // Now...// Optionally you can send the options for the random palettepalette;// Start: rgb(??, ??, ??)// End: rgb(??, ??, ??)
Gradient Function
Default return
If you don't send anything to the Gradient function, it will return a gradient from a white palette
// ...thisconst gradient = ;// [rgb(215, 215, 215)] // ...is the same thing as this:const gradient = ;// ...then:// [rgb(215, 215, 215), ..., rgb(255, 255, 255)]
1. Gradient API Type (GradientAPI)
2. Create a Gradient API
A Gradient object is used to represent a sequence of colors, which sequence consists of a starting color and an ending color
// 1. Init start and end colors:const startColor = ;const endColor = ; // 2. Init palette:const palette = ; // 3. Now, the gradients: // Way 1 - Create a default gradient:const gradient = ; // Precision by default: 5 // Note: All arrays will have the specified size, in this case, by default it will be 5gradient; // ["rgb(255, 0, 0)", ..., rgb(0, 0, 255)]gradient; // [{ r: 255, 0, 0 }, ..., { r: 0, g: 0, b: 255 }]gradient; // [ColorAPI, ..., ColorAPI] // Way 2 - Create with custom precisionconst gradient = ; // Custom precision: 20 // Note: All arrays will have the specified size, in this case, this size will be 20gradient; // ["rgb(255, 0, 0)", ..., rgb(0, 0, 255)]gradient; // [{ r: 255, 0, 0 }, ..., { r: 0, g: 0, b: 255 }]gradient; // [ColorAPI, ..., ColorAPI]
3. Change the value of the Gradient at run time
// Init a gradient API with default precision 5...const gradient = ; // ...thengradient; // ["rgb(215, 215, 215)", ..., "rgb(255, 255, 255)"] // Now, create a simple palette...const palette = ); // ...and use set() to change gradient value (optionally, you can set other options too)gradient;gradient; // ["rgb(100, 100, 100)", ..., "rgb(200, 200, 200)"]
4. Generate a random gradient
const gradient = ; // Optionally you can send the options for the random gradientgradient; gradient; // ["rgb(??, ??, ??)", ..., "rgb(??, ??, ??)"]
Converter Function
Note: keep this table in mind when using this function:
Create a Converter API
The Convert object is a separate object, that is, it works as if it were a static class and can help you convert different color formats without having to create another object. The library uses this object under the hood to work with the different color formats.
// New converter objectconst converter = ;
How to use
hexToRgb()
1. Description: Function that receives an object in the "hex" format and returns an object in the "rgb" format
Type: (color: Hex) => Rgb
const output = converter;// output = { r: 255, g: 255, b: 255 }
hslToRgb()
2. Description: Function that receives an object in the "hsl" format and returns an object in the "rgb" format
Type: (color: Hsl) => Rgb
const output = converter;// output = { r: 255, g: 255, b: 255 }
rgbToHex()
3. Description: Function that receives an object in the "rgb" format and returns an object in the "hex" format
Type: (color: Rgb) => Hex
const output = converter;// output = { r: "ff", g: "ff", b: "ff" }
rgbToHsl()
4. Description: Function that receives an object in the "hex" format and returns an object in the "rgb" format
Type: (color: Rgb) => Hsl
const output = converter;// output = { h: 0, s: 0, l: 100 }
Some frequently asked questions
1. Can I work using different color formats?
Yes, it is totally possible, although there may be some minor incompatibilities, for example: every hex color can be represented in the RGB, but not every RGB color can be represented in the hex format
const color = ; colorstring; // #968ca0colorstring; // hsl(270, 10%, 59%) color; color; // rgb(255, 255, 255)color; // hsl(0, 0%, 100%)
Open Source
Thanks for all, @davidcetinkaya
Copyright © 2020-present, Laks Castro.
Ethereal Color is MIT licensed 💖