Colormath.js - a color conversion and color manipulation library written in typescript for Node.js, Deno and Browser

Overview

Colormath.js 🎨

Colormath.js is a color conversion and color manipulation library written in typescript for Node.js, Deno and Browser.

const colors = require('colormath.js');

console.log(colors.rgb.toHex([255, 255, 255])); // '#ffffff'
console.log(colors.hex.toRgb('fff')); // [255, 255, 255]

Installation

Using colormath.js through Node.js

const colormath = require('colormath.js');

Using colormath.js through Browser

">
<script src="https://cdn.skypack.dev/colormath.js@latest?min">script>

Using colormath.js through Deno

import * as colormath from 'https://deno.land/x/colormath/mod.ts';

Examples

Manipulation Methods

Hex and Rgb are only supported for manipulation methods.

// Invert a color
colors.invert([114, 152, 218]).rgb;                   // [141, 103, 37]
colors.invert('#7298da').hex;                         // #8d6725

// Rotate the hue of the color.
colors.hue([114, 152, 218], 45).rgb;                  // [154.1, 114, 218]

// Returns the complement of a color.
colors.complement([114, 152, 218]).rgb;               // [218, 804.1, 114]

// Saturates a color.
colors.saturate([114, 152, 218], 20).rgb;             // [0, 79.6, 218]

// Desaturates a color.
colors.desaturate([114, 152, 218], 20).rgb;           // [218, 114, 114]

// Grayscales a color.
colors.grayscale([114, 152, 218]).rgb;                // [114, 152, 218]

// Mix colors
colors.mixColor([114, 152, 218], [255, 255, 255]).rgb // [184.5, 203.5, 236.5]

// Lighens a color.
colors.lighten([114, 152, 218], 20).rgb;              // [165, 203, 255]

// Darkens a color.
colors.darken([114, 152, 218], 20).rgb;               // [63, 101, 167]

Conversion Methods

The color models or formats supported by the library are hex, rgb, hsv, hsl, hwb, cmyk, xyz, lab, lch, ansi16, ansi256, gray and apple.

// Converts rgb to hex
colors.rgb.toHex([255, 255, 255]);                   // '#ffffff'

// Converts hex to rgb
colors.hex.toRgb('fff');                             // [255, 255, 255]

// Converts hsl to rgb
colors.hsl.toRgb([218, 58, 65]);                     // [114, 151.9, 217.5]

// Converts grayscale to rgb
colors.gray.toRgb(100)                               // [255, 255, 255]

Supported Conversions

These are supported color conversions from A to B suported by the library.

Only conversions which are used by people are added to the library and if you feel some of the unavailable color conversions are useful then you can create an issue or create a pr if you are able to add it yourself.

⬇ A \ B ➑ rgb hex hsv hsl hwb lab lch xyz cmyk ansi16 ansi256 gray apple
rgb βœ” βœ” βœ” βœ” βœ” ❌ βœ” βœ” βœ” βœ” βœ” βœ”
hex βœ” ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
hsv βœ” βœ” βœ” ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
hsl βœ” βœ” βœ” ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
hwb ❌ βœ” ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
lab βœ” ❌ ❌ ❌ ❌ βœ” βœ” ❌ ❌ ❌ ❌ ❌
lch ❌ ❌ ❌ ❌ ❌ βœ” ❌ ❌ ❌ ❌ ❌ ❌
xyz βœ” ❌ ❌ ❌ ❌ βœ” ❌ ❌ ❌ ❌ ❌ ❌
cmyk βœ” βœ” ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
ansi16 βœ” ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
ansi256 βœ” ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
gray βœ” βœ” βœ” βœ” βœ” βœ” ❌ ❌ βœ” ❌ ❌ ❌
apple βœ” ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  • βœ” means the color conversion is supported.
  • ❌ means the color conversion is not supported.

Sub conversions

You can still perform sub conversions. For example, conversions like rgb to lch does not exist you can still perform conversions like rgb to lab to lch.

const colors = require('colormath.js');
const rgbToLch = (rgb) => colors.lab.toLch(colors.rgb.toLab(rgb));

console.log(rgbToLch([255, 255, 255])); 
// Output: [ 100.00000386666655, 0.00001795054880958058, 158.19859051364818 ]

Things to be added

  • Support for alpha and string conversions.
  • Find a better way to do ColorResult.
  • Extracting colors from images.
  • More color manipulation methods.
  • Support for color spaces other than hex and rgb for method functions.

Help

If any doubts, bugs, reports regarding the module or want to add a new conversion or a new color model you can create an issue in github.

You might also like...

Create your own complete Web color system fast and easy!

Create your own complete Web color system fast and easy!

Simpler Color Create your own complete Web color system fast and easy, from as little as one base color! Color is at the heart of every UI design syst

Dec 20, 2022

A CLI utility to calculate/verify accessible magic numbers for a color palette.

A CLI utility to calculate/verify accessible magic numbers for a color palette.

A11y Contrast A CLI utility to calculate/verify accessible magic numbers for a color palette. Read my blog post for some more information. Installatio

Nov 13, 2022

A array with color name - decimal rgbs.

colors-named-decimal A array with color name - decimal rgbs. Based on https://www.w3.org/TR/css-color-4/#colors-named Installation This package is ES

Jul 20, 2022

colors.js - get colors in your node.js console

 colors.js - get colors in your node.js console

colors.js Please check out the roadmap for upcoming features and releases. Please open Issues to provide feedback, and check the develop branch for th

Feb 9, 2022

get colors in your node.js console

colors.js This is a fork of Marak Squires' original colors.js project Please check out the roadmap for upcoming features and releases. Please open Iss

Jan 11, 2022

Node Terminal Colors. Fast!

Tcol npm package to add colors to your terminal Installation # npm npm install tcol # pnpm pnpm install tcol Usage import { c } from "tcol"; console.

May 13, 2022

Get colors in your node.js console

Get colors in your node.js console

@colors/colors ("colors.js") Please check out the roadmap for upcoming features and releases. Please open Issues to provide feedback. get color and st

Dec 30, 2022

This repo contains resources & practice of project and files , while learning to master JS

Resources to learn JavaScript and conquer the world? Tutorials and Books JavaScript For Cats (http://jsforcats.com/) is a dead simply introduction for

Jan 6, 2022

Tints and shades generator in React.

Tints and shades generator in React.

Reactry's shades goals add option to enter hexColor by hand add option to get hexColor from clipboard add option to set one of the shades or tints as

May 26, 2022
Releases(1.2.4)
Owner
TheSudarsanDev
Quality is important than Quantity.
TheSudarsanDev
Fast, small color manipulation and conversion for JavaScript

TinyColor JavaScript color tooling TinyColor is a small, fast library for color manipulation and conversion in JavaScript. It allows many forms of inp

Brian Grinstead 4.5k Jan 1, 2023
Color2k - a color parsing and manipulation lib served in roughly 2kB

color2k a color parsing and manipulation lib served in roughly 2kB or less (2.8kB to be more precise) color2k is a color parsing and manipulation libr

Rico Kahler 506 Dec 31, 2022
Coloris - A lightweight and elegant JavaScript color picker. Written in vanilla ES6, no dependencies. Accessible.

Coloris A lightweight and elegant JavaScript color picker written in vanilla ES6. Convert any text input field into a color field. View demo Features

Momo Bassit 126 Dec 27, 2022
A simple color picker application written in pure JavaScript, for modern browsers.

Color Picker A simple color picker application written in pure JavaScript, for modern browsers. Has support for touch events. Touchy… touchy… Demo and

Taufik Nurrohman 207 Dec 14, 2022
A TypeScript package to calculate WCAG 2.0/3.0 and APCA color contrasts

a11y-color-contrast A simple utility package for working with WCAG 2.2/3.0 color contrasts a11y: Built for checking how readable colors are together S

Sondre Aasemoen 10 Oct 10, 2022
JavaScript Library for creating random pleasing colors and color schemes

#PleaseJS www.checkman.io/please Please.js is a polite companion that wants to help you make your projects beautiful. It uses HSV color space to creat

Jordan Checkman 2.3k Dec 22, 2022
JavaScript library for all kinds of color manipulations

Chroma.js Chroma.js is a tiny small-ish zero-dependency JavaScript library (13.5kB) for all kinds of color conversions and color scales. Usage Initiat

Gregor Aisch 9.2k Jan 4, 2023
A dependency-free image color extraction library

Color mage A dependency-free image color extraction library. The extraction consists of using K-Means algorithm. It has a few utility functions as wel

Gilmar Quinelato 4 Mar 11, 2022
A tool for creating and maintaining cohesive, consistent, and accessible color palettes

Primer Prism Primer Prism is a tool for creating and maintaining cohesive, consistent, and accessible color palettes. ?? primer.style/prism ?? Read th

Primer 517 Jan 3, 2023
A simple Discord bot that will listen for HEX, RGB(a), and HSL(a) colors in a message, and provide a small image of that color.

Pigment For the teams of designers and developers out there - Pigment will listen for messages containing a HEX, RGB(a), or HSL(a) color, and provide

Conrad Crawford 17 Dec 8, 2022