• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 4 years ago

Reviews

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

Repository Details

Fast and simple color conversion.

colr

Simple color conversion library based on simplicity and stability

API

Constructors

Create a new instance of the Colr class

// create a  empty instance
var colr = new Colr();

// create from hex
var colr = Colr.fromHex('#abc123');

// create from rgb
var colr = Colr.fromRgb(20, 30, 40);
var colr = Colr.fromRgbArray([20, 30, 40]);
var colr = Colr.fromRgbObject({r:20, g:30, b:40});

// create from hsl
var colr = Colr.fromHsl(320, 20, 90);
var colr = Colr.fromHslArray([320, 20, 90]);
var colr = Colr.fromHslObject({h:320, s:20, l:90});

// create from grayscale
var colr = Colr.fromGrayscale(128);

Importers

Change the color of an existing Colr instance.

All methods return the colr instance and can be chained.

var colr = new Colr();

// import from hex
colr.fromHex('#abc123');

// import from rgb
colr.fromRgb(20, 30, 40);
colr.fromRgbArray([20, 30, 40]);
colr.fromRgbObject({r:20, g:30, b:40});

// import from hsl
colr.fromHsl(320, 20, 90);
colr.fromHslArray([320, 20, 90]);
colr.fromHslObject({h:320, s:20, l:90});

// import from hsv/hsb
colr.fromHsv(30, 80, 20);
colr.fromHsvArray([30, 80, 20]);
colr.fromHsvObject({h:30, s:80, v:20});

// create from grayscale
colr.fromGrayscale(128);

Hsl/Hsv ranges

These are not 0 to 255 but rather:

Hue:              [0, 360]
Saturation:       [0, 100]
Lightness/Value:  [0, 100]

Exporters

Convert the color to another format

var colr = Colr().fromHex('bada55');

colr.toHex(); // "#BADA55"

colr.toRgbArray(); // [186, 218, 85]
colr.toRgbObject(); // {r:186, g:218, b:85}
colr.toRawRgbArray(); // [186, 218, 85]
colr.toRawRgbObject(); // {r:186, g:218, b:85}

colr.toHslArray(); // [74, 64, 59]
colr.toHslObject(); // {h:74, s:64, l:59}
colr.toRawHslArray(); // {74.4360902255639, 64.25120772946859, 59.411764705882355]
colr.toRawHslObject(); // {r:74.4360902255639, g:64.25120772946859, b:59.411764705882355}

colr.toHsvArray(); // [74, 61, 85]
colr.toHsvObject(); //{h: 74, s: 61, l: 85}
colr.toRawHsvArray(); // [74.4360902255639, 61.00917431192661, 85.49019607843137]
colr.toRawHsvObject(); // {r:74.4360902255639, g:61.00917431192661, b:85.49019607843137}

colr.toGrayscale(); // 193.27

Modifiers

All methods return the colr instance and can be chained.

var colr = Colr.fromHex('000').lighten(20);
colr.toHex(); // "#333333"

var colr = Colr.fromHex('FFF').darken(20);
colr.toHex(); // "#CCCCCC"

Misc

var a = Colr.fromHex('#000');
var b = a.clone();

a.lighten(20);

a.toHex(); // '#333333'
b.toHex(); // '#000000'

Benchmarks

$ node benchmark.js

# FromHsv -> ToRgb
colr      x 4,552,216 ops/sec ±0.75% (102 runs sampled)
color     x 334,029 ops/sec ±0.40% (98 runs sampled)
tinycolor x 1,018,397 ops/sec ±0.45% (102 runs sampled)
chroma    x 346,686 ops/sec ±0.36% (97 runs sampled)
Fastest is colr

# FromHex -> Lighten -> ToHex
colr      x 1,400,992 ops/sec ±0.27% (102 runs sampled)
color     x 61,486 ops/sec ±0.39% (102 runs sampled)
tinycolor x 80,712 ops/sec ±0.96% (97 runs sampled)
chroma    x 100,885 ops/sec ±0.73% (100 runs sampled)
Fastest is colr

# FromHex -> Lighten -> Darken -> ToHex
colr      x 1,334,580 ops/sec ±0.72% (98 runs sampled)
color     x 47,224 ops/sec ±0.86% (101 runs sampled)
tinycolor x 54,639 ops/sec ±1.07% (101 runs sampled)
chroma    x 65,039 ops/sec ±0.93% (101 runs sampled)
Fastest is colr

# FromHex -> ToHex
colr      x 2,192,730 ops/sec ±0.77% (98 runs sampled)
color     x 92,882 ops/sec ±0.87% (101 runs sampled)
tinycolor x 201,324 ops/sec ±0.98% (98 runs sampled)
chroma    x 266,279 ops/sec ±0.89% (98 runs sampled)
Fastest is colr

# FromHsv -> ToRgb -> ToHex
colr      x 1,680,538 ops/sec ±0.68% (97 runs sampled)
color     x 155,721 ops/sec ±0.48% (101 runs sampled)
tinycolor x 530,777 ops/sec ±0.81% (99 runs sampled)
chroma    x 235,950 ops/sec ±1.00% (95 runs sampled)
Fastest is colr

# FromHsv -> ToHsl
colr      x 4,387,198 ops/sec ±0.45% (99 runs sampled)
color     x 201,875 ops/sec ±0.73% (101 runs sampled)
tinycolor x 434,277 ops/sec ±0.65% (102 runs sampled)
chroma    x 306,594 ops/sec ±1.21% (92 runs sampled)
Fastest is colr

# FromHsl -> ToHsv
colr      x 4,296,682 ops/sec ±0.69% (95 runs sampled)
color     x 187,083 ops/sec ±0.72% (102 runs sampled)
tinycolor x 471,617 ops/sec ±0.59% (102 runs sampled)
chroma    x 229,835 ops/sec ±0.52% (100 runs sampled)
Fastest is colr

More Repositories

1

terminal.sexy

Create, view and edit terminal colorschemes.
JavaScript
848
star
2

rango

A web frontend for hugo.
Go
286
star
3

termcolors

Import and export between multiple terminal colour scheme formats
JavaScript
131
star
4

LightRead

The best RSS reader on Ubuntu
JavaScript
82
star
5

react-colorpicker

Colorpicker for React
JavaScript
72
star
6

dotfiles

My personal config files and shell scripts
JavaScript
50
star
7

reactwm

A minimal window manager built using React.
JavaScript
37
star
8

bar

Print text on the screen using node-webkit.
JavaScript
24
star
9

onepasswordjs

JavaScript implementation of the 1Password cloud keychain
JavaScript
21
star
10

react-ranger

A column browser for the browser
JavaScript
21
star
11

deezer

Deezer API for Go
Go
15
star
12

termio

Perfectly convert ANSI escape codes to HTML.
JavaScript
15
star
13

clickandcollect.nz

Tracking NZ supermarket click & collect slots
TypeScript
13
star
14

Nitro-Mac

Nitrotasks for OS X
Objective-C
11
star
15

perplexed

Plex API Client
TypeScript
10
star
16

hifi.kitchen

A Plex web client built for music playback
JavaScript
9
star
17

gulp-brfs

Gulp plugin for substack's brfs
JavaScript
8
star
18

base

A tiny and simple javascript framework based on spine.js
JavaScript
7
star
19

notes

A basic notes app I made ages ago
CSS
6
star
20

mouse

Draw select boxes with your mouse
JavaScript
5
star
21

invoiced

A web based invoice management system
JavaScript
5
star
22

Gradiator

Simple CSS gradients
JavaScript
5
star
23

colr-convert

Color conversion functions.
JavaScript
4
star
24

splitter

Simply resizable panels.
JavaScript
4
star
25

jandal

Manage sockets using events, callbacks and rooms
JavaScript
4
star
26

khaki

Bluetooth Car Keys
Objective-C
3
star
27

pi-odometer

Monitor a Water or Gas Odometer with a Raspberry Pi
Go
3
star
28

unwire

Dependency injection with 'require()'
TypeScript
2
star
29

stu

It's kinda like jest.mock, but with sinon and without jest
TypeScript
2
star
30

streams

Boilerplate for stream-adventure
JavaScript
2
star
31

how-to-graphql

Example graphql server
JavaScript
2
star
32

shell

My dev environment as a docker container
Shell
2
star
33

scheme-search

Identify a sublime text colour scheme by analyzing the colours in a screenshot
CoffeeScript
2
star
34

splinter

Split an object-mode stream into multiple streams by filtering the chunks
JavaScript
2
star
35

digitalocean

2
star
36

plextools

A collection of tools for working with a plex music library.
TypeScript
2
star
37

cjs-rename

Magically rename 'require()' modules
JavaScript
2
star
38

downtime

It's just ping, but with a graph
JavaScript
2
star
39

volatile

A service for automating cryptocurrency trading.
TypeScript
2
star
40

battleship

Go
1
star
41

autowatch

Watch a site and notify on slack when it has changed
Go
1
star
42

gmail

Query your gmail from within Node.js
JavaScript
1
star
43

chit-chat

Simple chat app
JavaScript
1
star
44

Subtle

Add a texture to an image
CoffeeScript
1
star
45

gradio

A grooveshark library, server and client
JavaScript
1
star
46

scrunch

Concatenate coffee script files using require()
JavaScript
1
star
47

dataloader

Go
1
star
48

tapedeck-dj

Go
1
star
49

gpx

Go
1
star
50

pandora

Export all pandora stations + faves to JSON
Go
1
star
51

google-places-periods-parser

TypeScript
1
star
52

react-ssr-starter

Boilerplate code for server side rendering with react, redux, react-router and react-hot-loader.
JavaScript
1
star
53

resume

My 2020 Résumé
JavaScript
1
star
54

webkeys

A simple password manager.
CoffeeScript
1
star
55

signals

Lightweight event emitting for Node and the browser.
JavaScript
1
star
56

zwolf-service

TypeScript
1
star
57

git-status

Quickly check which projects have uncommited code.
JavaScript
1
star
58

solar-clock

I'm having a hard time trying to figure out what time it is.
TypeScript
1
star
59

boiler

Quickly setup a new package or web app
JavaScript
1
star
60

simplex-ui

JavaScript
1
star
61

piratemap

An ES6 map that supports complex keys with shallow equals
TypeScript
1
star
62

clickandcollect.nz.api

Automated snapshot of NZ supermarket click & collect availability
HTML
1
star