• Stars
    star
    4,713
  • Rank 8,496 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 13 years ago
  • Updated about 2 years ago

Reviews

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

Repository Details

🌈 Javascript color conversion and manipulation library

color

JavaScript library for immutable color conversion and manipulation with support for CSS color strings.

const color = Color('#7743CE').alpha(0.5).lighten(0.5);
console.log(color.hsl().string());  // 'hsla(262, 59%, 81%, 0.5)'

console.log(color.cmyk().round().array());  // [ 16, 25, 0, 8, 0.5 ]

console.log(color.ansi256().object());  // { ansi256: 183, alpha: 0.5 }

Install

$ npm install color

Usage

const Color = require('color');

Constructors

const color = Color('rgb(255, 255, 255)')
const color = Color({r: 255, g: 255, b: 255})
const color = Color.rgb(255, 255, 255)
const color = Color.rgb([255, 255, 255])

Set the values for individual channels with alpha, red, green, blue, hue, saturationl (hsl), saturationv (hsv), lightness, whiteness, blackness, cyan, magenta, yellow, black

String constructors are handled by color-string

Getters

color.hsl();

Convert a color to a different space (hsl(), cmyk(), etc.).

color.object(); // {r: 255, g: 255, b: 255}

Get a hash of the color value. Reflects the color's current model (see above).

color.rgb().array()  // [255, 255, 255]

Get an array of the values with array(). Reflects the color's current model (see above).

color.rgbNumber() // 16777215 (0xffffff)

Get the rgb number value.

color.hex() // #ffffff

Get the hex value. (NOTE: .hex() does not return alpha values; use .hexa() for an RGBA representation)

color.red()       // 255

Get the value for an individual channel.

CSS Strings

color.hsl().string()  // 'hsl(320, 50%, 100%)'

Calling .string() with a number rounds the numbers to that decimal place. It defaults to 1.

Luminosity

color.luminosity();  // 0.412

The WCAG luminosity of the color. 0 is black, 1 is white.

color.contrast(Color("blue"))  // 12

The WCAG contrast ratio to another color, from 1 (same color) to 21 (contrast b/w white and black).

color.isLight();  // true
color.isDark();   // false

Get whether the color is "light" or "dark", useful for deciding text color.

Manipulation

color.negate()         // rgb(0, 100, 255) -> rgb(255, 155, 0)

color.lighten(0.5)     // hsl(100, 50%, 50%) -> hsl(100, 50%, 75%)
color.lighten(0.5)     // hsl(100, 50%, 0)   -> hsl(100, 50%, 0)
color.darken(0.5)      // hsl(100, 50%, 50%) -> hsl(100, 50%, 25%)
color.darken(0.5)      // hsl(100, 50%, 0)   -> hsl(100, 50%, 0)

color.lightness(50)    // hsl(100, 50%, 10%) -> hsl(100, 50%, 50%)

color.saturate(0.5)    // hsl(100, 50%, 50%) -> hsl(100, 75%, 50%)
color.desaturate(0.5)  // hsl(100, 50%, 50%) -> hsl(100, 25%, 50%)
color.grayscale()      // #5CBF54 -> #969696

color.whiten(0.5)      // hwb(100, 50%, 50%) -> hwb(100, 75%, 50%)
color.blacken(0.5)     // hwb(100, 50%, 50%) -> hwb(100, 50%, 75%)

color.fade(0.5)     // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 0.4)
color.opaquer(0.5)     // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 1.0)

color.rotate(180)      // hsl(60, 20%, 20%) -> hsl(240, 20%, 20%)
color.rotate(-90)      // hsl(60, 20%, 20%) -> hsl(330, 20%, 20%)

color.mix(Color("yellow"))        // cyan -> rgb(128, 255, 128)
color.mix(Color("yellow"), 0.3)   // cyan -> rgb(77, 255, 179)

// chaining
color.green(100).grayscale().lighten(0.6)

Propers

The API was inspired by color-js. Manipulation functions by CSS tools like Sass, LESS, and Stylus.

More Repositories

1

better-exceptions

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

color-convert

Plain color conversion functions in JavaScript
JavaScript
721
star
3

color-string

Parser and generator for CSS color strings
JavaScript
213
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

scaly

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

starfuse

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

this-image-is

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

qunit

Super simple unit testing in C99
C
3
star
25

mds-lava

Lava mod for Mindustry
3
star
26

nesmap

Universal NES Map editor
CoffeeScript
3
star
27

node-smart-pipe

Pipe to standard input, cross platform
CoffeeScript
3
star
28

CaptainJack

[ABANDONED] The unofficial JACK audio device driver for OS/X
C
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-backslash

Parse backslashed strings
JavaScript
2
star
36

node-man-api-mdoc

MDoc macro extensions for man-api
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

libcopynes

Git mirror of libcopynes
C
1
star
43

match-file-tree

Lightweight regex based file tree traversal
JavaScript
1
star
44

node-find-api

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

Codicle

Just Learn.
CoffeeScript
1
star
46

arua-bootstrap-2

The Arua bootstrap compiler
C++
1
star
47

node-ddb

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

node-call-with-globals

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

test-repo

JavaScript
1
star
50

node-legos

Snap your streams together
JavaScript
1
star
51

node-require-with-globals

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

mist

Mist build system
CoffeeScript
1
star
53

cbind

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

beep-osx

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

test-layout-api-bug

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

josh-manders-made-this

But not really.
1
star
57

mud-json

JSON driver for the Mud ORM
JavaScript
1
star
58

cassowary-playground

A playground for Cassowary constraints
Rust
1
star
59

eloquent

Make expressive APIs easily and cleanly
CoffeeScript
1
star
60

eslint-plugin-smelly

Find code patterns that might be code smell
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-find-higher-file

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

node-atomic-fs

Atomically perform file operations
CoffeeScript
1
star
69

did-they-seriously-rename-master

Oh my god what bullshit is this
1
star
70

riffusion-cli

Hacked together CLI for Riffusion. Please, please don't abuse this.
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