Releases: EDM115/palex
v1.0.0 : Official release
palex
A javascript package to help you create and manage color palettes
Did you ever wanted to create a color palette for your website or your app but you didn't knew how to do it ? Did you had the idea to create a whole palette from a single color but thought it was too hard ? Or maybe you were wondering how to make a color palette colorblind friendly ?
Well today is your lucky day because palex
is here to help you with all of that !
Note
palex
was created when I was working on @data-fair/app-charts and I needed to create color palettes for the charts. During this time I created several functions to help me with that, and some didn't make it to the final version, so I decided to release palex
as a standalone library to help anyone struggling with color palettes !
Installation
You can install palex
using your favorite package manager, here are some examples :
npm install palex
yarn add palex
Find the package on : NPM | jsDelivr
Usage
You can instanciate palex
the CommonJS way :
const palex = require('palex')
console.log(palex.palex('#FFB86C', 'color', 10, cbf = true, golden = true))
Or the ESM way (used in the following documentation) :
import { palex } from 'palex'
console.log(palex('#FFB86C', 'color', 10, cbf = true, golden = true))
Important
If you're using the ESM way, you'll need to either use the .mjs
extension or have a "type": "module"
in your package.json
file.
Documentation
The following documentation will explain how to use palex
and its functions. It tries to be as comprehensive as possible, but if you have any question, feel free to open an issue ! Pull requests are also welcome if you want to add a feature or fix a bug !
palex(input, type, numColors = 10, cbf = false, golden = false, grey = false)
The main entrypoint of palex
. It generates a color palette based on a given input and type.
input
: The input color(s). It can be a palette string (brewer), a color(s) string (hex, rgb, or named color) or an array containing any of these.type
: The type of the input. It can bebrewer
,hues
,complementary
,color
orgreyscale
.numColors
: The number of colors to generate. It defaults to 10.cbf
: Iftrue
, the palette will be colorblind friendly. It defaults tofalse
. Have no effect if the type isbrewer
orgreyscale
.golden
: Iftrue
, the palette will be based on the golden ratio. It defaults tofalse
. Not recommended to use along withcbf
.grey
: Iftrue
, a greyscale will be added to the generated palette if the number of colors is less than numColors. It defaults tofalse
.
Returns an array of colors in hexadecimal format.
import { palex } from 'palex'
console.log(palex('#FFB86C', 'color', 10, cbf = true, golden = true))
// ["#df9220", "#2520df", "#dfb220", "#df3720", "#c6df20", "#df20a7", "#dfde20", "#2c20df", "#ccdf20", "#205fdf"]
sanitizeInput(input)
Did you ever wanted to know how every function in palex
can accept such a wide range of inputs ? Well, it's because of sanitizeInput
! I wanted to create something easy to use so you don't have to worry about changing the input to fit the function.
input
: The input to sanitize. It can be a palette string (brewer), a color(s) string (hex, rgb, or named color) or an array containing any of these.
Returns the sanitized input.
import { sanitizeInput } from 'palex'
console.log(sanitizeInput('#FFB86C'))
// "#ffb86c"
console.log(sanitizeInput('Set3'))
// "Set3"
console.log(sanitizeInput('rgb(255, 184, 108)'))
// "#ffb86c"
console.log(sanitizeInput('ff0, #abc, FFB86C, , ,, ,,, rgb(100, 200, 81)'))
// ["#ffff00", "#aabbcc", "#ffb86c", "#64c851"]
generatePaletteFromBrewer(input, numColors)
Generates a color palette from a brewer palette string.
Tip
You can find strings to use here : https://loading.io/color/feature/
All strings in the Diverging section are valid, all from Qualitative except HCL, and most from Gradient too :)
input
: The brewer palette string.numColors
: The number of colors to generate. If not provided, it defaults to 2 and will return the 2 base colors of the palette.
Returns an array of colors in hexadecimal format.
import { generatePaletteFromBrewer } from 'palex'
console.log(generatePaletteFromBrewer('Set3', 10))
// ["#8dd3c7", "#ffe3b1", "#e19ec9", "#aaa1df", "#ffa778", "#d4d766", "#f1d1e1", "#caa8ca", "#dbd29f", "#ffed6f"]
getGoldenColor(color)
Returns a color based on the golden ratio from a given color. The idea behind it stems from the very good PleaseJS library.
color
: The color to base the new color on.
Returns a color in hexadecimal format.
import { getGoldenColor } from 'palex'
console.log(getGoldenColor('#FFB86C'))
// "#df8320"
generateGreyscale(start, end, steps)
Generates a greyscale palette from a start point to an end point with a given number of steps.
start
: The start point of the greyscale, from 0 to 255.end
: The end point of the greyscale, from 0 to 255.steps
: The number of steps to generate.
Returns an array of colors in hexadecimal format.
import { generateGreyscale } from 'palex'
console.log(generateGreyscale(0, 10, 5))
// generates 11 colors, from black at 0 to white at 5, and values above steps and below end are white
// ["#000000", "#333333", "#666666", "#999999", "#cccccc", "#ffffff", "#ffffff", "#ffffff", "#ffffff", "#ffffff", "#ffffff"]
console.log(generateGreyscale(6, 10, 6))
// generates 5 colors. they are all pure white
// ["#ffffff", "#ffffff", "#ffffff", "#ffffff", "#ffffff", "#ffffff"]
console.log(generateGreyscale(1, 5, 10))
// generates 5 colors. since steps is bigger than end - start, we will have a greyscale that avoids black and white
// ["#1a1a1a", "#333333", "#4d4d4d", "#666666", "#808080"]
generateHues(palette, numColors, cbf = false)
Generates a hue palette from a given color palette and a number of colors. It can also make the palette colorblind friendly.
palette
: The palette to base the hue on.numColors
: The number of colors to generate.cbf
: Iftrue
, the palette will be colorblind friendly. It defaults tofalse
.
Returns an array of colors in hexadecimal format.
import { generateHues } from 'palex'
console.log(generateHues(['#BD93F9', '#F1FA8C', '#6272A4'], 10))
// ["#bd93f9", "#f1fa8c", "#6272a4", "#fdecff", "#faffc8", "#7997db", "#fff3ff", "#a6bcf5", "#cee3ff"]
import { generatePaletteFromBrewer } from 'palex'
console.log(generateHues(generatePaletteFromBrewer('Set3', 10), 10))
// ["#8dd3c7", "#ffe3b1", "#e19ec9", "#aaa1df", "#ffa778", "#d4d766", "#f1d1e1", "#caa8ca", "#dbd29f", "#ffed6f"]
generateHuesFromColor(color, numColors, cbf = false)
Generates a color palette hue from a given color and a number of colors. It can also make the palette colorblind friendly.
color
: The color to base the hue on.numColors
: The number of colors to generate.cbf
: Iftrue
, the palette will be colorblind friendly. It defaults tofalse
.
Returns an array of colors in hexadecimal format.
import { generateHuesFromColor } from 'palex'
console.log(generateHuesFromColor('#FFB86C', 10))
// ["#ffb86c", "#ffc56e", "#ffd794", "#ffe9b7", "#fffadb", "#fff3ff", "#fff3ff", "#fff3ff", "#fff3ff", "#fff3ff"]
generateComplementary(palette, numColors, cbf = false)
Generates a complementary palette from a given color palette and a number of colors. It can also make the palette colorblind friendly.
palette
: The palette to base the complementary on.numColors
: The number of colors to generate.cbf
: Iftrue
, the palette will be colorblind friendly. It defaults tofalse
.
Returns an array of colors in hexadecimal format.
import { generateComplementary } from 'palex'
console.log(generateComplementary(['#BD93F9', '#F1FA8C', '#6272A4'], 10))
// ["#bd93f9", "#f1fa8c", "#6272a4", "#cff993", "#958cfa", "#a49462", "#f093f9", "#bafa8c", "#7362a4", "#939cf9"]
import { generatePaletteFromBrewer } from 'palex'
console.log(generateComplementary(generatePaletteFromBrewer('Set3', 10), 10))
// ["#8dd3c7", "#ffe3b1", "#e19ec9", "#aaa1df", "#ffa778", "#d4d766", "#f1d1e1", "#caa8ca", "#dbd29f", "#ffed6f"]
generatePaletteFromColor(color, numColors, cbf = false)
Generates a color palette from a given color and a number of colors. Starts by generating a complementary color, then generates a number of analogous colors. If the number of colors is not reached, it generates a number of triadic colors. It can also make the palette colorblind friend...