• Stars
    star
    746
  • Rank 60,823 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Plain color conversion functions in JavaScript

color-convert

Color-convert is a color conversion library for JavaScript and node. It converts all ways between rgb, hsl, hsv, hwb, cmyk, ansi, ansi16, hex strings, and CSS keywords (will round to closest):

var convert = require('color-convert');

convert.rgb.hsl(140, 200, 100);             // [96, 48, 59]
convert.keyword.rgb('blue');                // [0, 0, 255]

var rgbChannels = convert.rgb.channels;     // 3
var cmykChannels = convert.cmyk.channels;   // 4
var ansiChannels = convert.ansi16.channels; // 1

Install

$ npm install color-convert

API

Simply get the property of the from and to conversion that you're looking for.

All functions have a rounded and unrounded variant. By default, return values are rounded. To get the unrounded (raw) results, simply tack on .raw to the function.

All 'from' functions have a hidden property called .channels that indicates the number of channels the function expects (not including alpha).

var convert = require('color-convert');

// Hex to LAB
convert.hex.lab('DEADBF');         // [ 76, 21, -2 ]
convert.hex.lab.raw('DEADBF');     // [ 75.56213190997677, 20.653827952644754, -2.290532499330533 ]

// RGB to CMYK
convert.rgb.cmyk(167, 255, 4);     // [ 35, 0, 98, 0 ]
convert.rgb.cmyk.raw(167, 255, 4); // [ 34.509803921568626, 0, 98.43137254901961, 0 ]

Arrays

All functions that accept multiple arguments also support passing an array.

Note that this does not apply to functions that convert from a color that only requires one value (e.g. keyword, ansi256, hex, etc.)

var convert = require('color-convert');

convert.rgb.hex(123, 45, 67);      // '7B2D43'
convert.rgb.hex([123, 45, 67]);    // '7B2D43'

Routing

Conversions that don't have an explicitly defined conversion (in conversions.js), but can be converted by means of sub-conversions (e.g. XYZ -> RGB -> CMYK), are automatically routed together. This allows just about any color model supported by color-convert to be converted to any other model, so long as a sub-conversion path exists. This is also true for conversions requiring more than one step in between (e.g. LCH -> LAB -> XYZ -> RGB -> Hex).

Keep in mind that extensive conversions may result in a loss of precision, and exist only to be complete. For a list of "direct" (single-step) conversions, see conversions.js.

Color Space Scales

Conversions rely on an agreed upon 'full-scale' value for each of the channels. Listed here are those values for the most common color spaces

rgb

channel full-scale value
r 255
g 255
b 255

hsl

channel full-scale value
h 360
s 100
l 100

hsv

channel full-scale value
h 360
s 100
v 100

hwb

channel full-scale value
h 360
w 100
b 100

cmyk

channel full-scale value
c 100
m 100
y 100
k 100

hex

channel full-scale value
hex 0xffffff

keyword

channel value
name any key from color-name

apple

channel full-scale value
0 65535
1 65535
2 65535

gray

channel full-scale value
g 100

Contribute

If there is a new model you would like to support, or want to add a direct conversion between two existing models, please send us a pull request.

License

Copyright ยฉ 2011-2016, Heather Arthur. Copyright ยฉ 2016-2021, Josh Junon.

Licensed under the MIT License.

More Repositories

1

color

๐ŸŒˆ Javascript color conversion and manipulation library
JavaScript
4,775
star
2

better-exceptions

Pretty and useful exceptions in Python, automatically.
Python
4,531
star
3

color-string

Parser and generator for CSS color strings
JavaScript
214
star
4

tortellini

A really stupid INI file format for C++11
C++
137
star
5

tabs-are-better-than-spaces

They are. Don't fight it.
HTML
73
star
6

node-error-ex

Easy error subclassing and stack customization
CoffeeScript
62
star
7

arua-meta

Standards, RFCs and discussion of the Arua language
Vim Script
44
star
8

node-editor

Generic node editor web components
TypeScript
27
star
9

xopt

ANSI C command line options parser
C
22
star
10

node-man-api

Eases the generation of (proper) man pages
JavaScript
18
star
11

node-is-arrayish

Check if an object can be used like an Array
CoffeeScript
16
star
12

flexicon

A lightweight, regex-based lexer framework for Python
Python
12
star
13

node-simple-swizzle

Simply swizzle your arguments
CoffeeScript
11
star
14

terminfo

Parse the Terminfo database
JavaScript
8
star
15

pet

Dead simple HTTP requests
JavaScript
8
star
16

dot-dot-dash-demo

Quick demo for Dot Dot Dash
C++
7
star
17

arua.io

The Arua language website
HTML
6
star
18

xp

A regular expression search (and replace) command line tool
JavaScript
6
star
19

handler-agent

Zero-dependency (req,res) handler callback agent for use in client requests
JavaScript
6
star
20

casscade

Experimental Cassowary (Kiwi) constraint-based layouts using CSS layout worklets (Houdini layout API)
JavaScript
5
star
21

starfuse

Mount StarBound .pak files as regular directories
Python
4
star
22

scaly

Minimalistic, composable cache and database layering inspired by CPU caches
JavaScript
4
star
23

this-image-is

A Reddit bot that tells you what's in an image using neural networks.
Python
4
star
24

CaptainJack

[ABANDONED] The unofficial JACK audio device driver for OS/X
C
3
star
25

qunit

Super simple unit testing in C99
C
3
star
26

mds-lava

Lava mod for Mindustry
3
star
27

nesmap

Universal NES Map editor
CoffeeScript
3
star
28

node-smart-pipe

Pipe to standard input, cross platform
CoffeeScript
3
star
29

docopt.md

Keep your docopt configuration directly in your readme
JavaScript
2
star
30

node-high-order

Find the length of a UTF-8 glyph by its first byte
CoffeeScript
2
star
31

mds-ancients-units

ANCiENTS Mindustry Mod (Units pack)
JavaScript
2
star
32

factorio-hand-crank

Hand Crank mod for Factorio
Makefile
2
star
33

fugu

(this is a mirror, I didn't write this)
C++
2
star
34

s-storage

localStorage bindings for S.js
JavaScript
2
star
35

node-man-api-mdoc

MDoc macro extensions for man-api
JavaScript
2
star
36

node-backslash

Parse backslashed strings
JavaScript
2
star
37

bellingbot

Bellingcat discord bot
Python
2
star
38

sig-router

A URL/history API router for S.js and Surplus applications
JavaScript
2
star
39

cssquery

Simple object queries using CSS-like selector syntax. I don't recommend you actually use this.
Python
2
star
40

lucky-the-eclectus

My parrot in Discord bot format. Keeps you company; nothing more.
JavaScript
1
star
41

node-transform-domain

Transforms a value given a source and destination domain
JavaScript
1
star
42

match-file-tree

Lightweight regex based file tree traversal
JavaScript
1
star
43

node-find-api

Finds all of the API calls and docs in a source file
JavaScript
1
star
44

arua-bootstrap-2

The Arua bootstrap compiler
C++
1
star
45

libcopynes

Git mirror of libcopynes
C
1
star
46

Codicle

Just Learn.
CoffeeScript
1
star
47

node-ddb

Fixed-sized frame decoding for Node.js using ffmpeg
C++
1
star
48

test-repo

JavaScript
1
star
49

node-legos

Snap your streams together
JavaScript
1
star
50

node-require-with-globals

Safely require a module with a modified global object.
JavaScript
1
star
51

cbind

Real C-binding / argument injection (some seriously cool shit)
C
1
star
52

test-layout-api-bug

Blink engine Houdini Layout API + WebAssembly access violation reproduction
HTML
1
star
53

josh-manders-made-this

But not really.
1
star
54

beep-osx

"Hardware speaker" beep for OS X
Objective-C
1
star
55

eloquent

Make expressive APIs easily and cleanly
CoffeeScript
1
star
56

cassowary-playground

A playground for Cassowary constraints
Rust
1
star
57

mist

Mist build system
CoffeeScript
1
star
58

eslint-plugin-smelly

Find code patterns that might be code smell
JavaScript
1
star
59

mud-json

JSON driver for the Mud ORM
JavaScript
1
star
60

node-call-with-globals

Safely call a function with a modified global object.
JavaScript
1
star
61

lib-llvm-clang

LLVM.js/Clang.js dependencies for Codicle
1
star
62

node-abyss

Deep and asynchronous.
JavaScript
1
star
63

tap-dot-neo

Spiritual successor to `tap-dot`
JavaScript
1
star
64

prisoners-dilemma

Prisoners Dilemma simulator
C
1
star
65

clang-extra-npm

Installs `clang-tools-extra` for you.
Shell
1
star
66

eslint-plugin-wtf

Catch mistakes literally no developer should be making.
JavaScript
1
star
67

node-atomic-fs

Atomically perform file operations
CoffeeScript
1
star
68

did-they-seriously-rename-master

Oh my god what bullshit is this
1
star
69

riffusion-cli

Hacked together CLI for Riffusion. Please, please don't abuse this.
JavaScript
1
star
70

node-find-higher-file

Traverses a path to find a file in a higher directory
JavaScript
1
star
71

node-emplace

Emplace objects and arrays - pointer to pointer style!
CoffeeScript
1
star
72

node-has-module

Checks for the existence of a module
JavaScript
1
star
73

autotest

C/C++ unit testing on Linux as simple as writing functions
C
1
star
74

qix-.github.io

My portfolio site.
Roff
1
star
75

node-consumer-stream

Readline-like functionality that keeps track of line counts and properly handles final lines without trailing newlines
CoffeeScript
1
star
76

duplicant

Finds similar lines in a codebase and shows them as a heatmap in VSCode
Rust
1
star