• Stars
    star
    340
  • Rank 123,944 (Top 3 %)
  • 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

Find the nearest color

nearest-color

Find the nearest color given a predefined list of colors.

Usage

var colors = {
  red: '#f00',
  yellow: '#ff0',
  blue: '#00f'
};

var nearestColor = require('nearest-color').from(colors);

nearestColor('#800'); // => { name: 'red', value: '#f00', rgb: { r: 255, g: 0, b: 0 }, distance: 119 }
nearestColor('#ffe'); // => { name: 'yellow', value: '#ff0', rgb: { r: 255, g: 255, b: 0 }, distance: 238 }

How it works

Finding the nearest color is a specific case of the "nearest neighbor search" (or NNS) problem. The predefined colors can be thought of as points in 3D space where the X, Y, and Z axes represent each color's red, green, and blue (RGB) values. So finding the nearest color to any given value amounts to finding the closet neighbor to the point where that color would reside when plotted in such a 3D space.

From the Wikipedia article on the subject:

The simplest solution to the NNS problem is to compute the distance from the query point to every other point in the database, keeping track of the "best so far". This algorithm, sometimes referred to as the naive approach, has a running time of O(Nd) where N is the cardinality of S and d is the dimensionality of M. There are no search data structures to maintain, so linear search has no space complexity beyond the storage of the database. Naive search can, on average, outperform space partitioning approaches on higher dimensional spaces.

This library uses the naive approach, which is hard to beat. Performance should be totally fine unless there are many pre-defined colors to search (and even then, it will probably only matter if you're calling nearestColor a ton of times).

The most realistic optimization that could be made here would probably be to cache results so that multiple calls for the same color can return immediately.

More Repositories

1

lazy.js

Like Underscore, but lazier
JavaScript
6,026
star
2

autodoc

Doc generation on steroids
JavaScript
232
star
3

safe_yaml

Parse YAML safely
Ruby
215
star
4

ConcurrentList

A thread-safe, lock-free implementation of the IList<T> interface for .NET
C#
55
star
5

lemming.js

Evaluate user-input JS code
JavaScript
44
star
6

console-highlight

Syntax highlighting in the console
CSS
35
star
7

fast-matcher

Fast matching, e.g. for autocompletes
JavaScript
31
star
8

angular-fast-matcher

Angular directive based on fast-matcher
JavaScript
27
star
9

HighTables

A JavaScript library that makes it trivial to render charts from existing HTML tables
JavaScript
24
star
10

whatever.js

Just whatever
JavaScript
18
star
11

htmlout

HTML-styled console output
JavaScript
15
star
12

slidedown

Markdown slide decks
JavaScript
12
star
13

dm-noisy-failures

Noisy (and descriptive) failures for DataMapper
Ruby
12
star
14

simplex

Simpler than regular expressions
JavaScript
10
star
15

string-table

Format an array of data objects as a textual table
CoffeeScript
10
star
16

named-args

Named arguments in JavaScript
JavaScript
9
star
17

SublimeBucket

Bitbucket plugin for Sublime Text 3
Python
8
star
18

todo-backend-express

Node.js/Express implementation of the Todo-Backend API spec
JavaScript
8
star
19

apiif

API in folders
Ruby
7
star
20

6th-css-sense

I see dead CSS selectors
JavaScript
5
star
21

PhilosopherDeveloper

A repository for the blog The Philosopher Developer
CSS
5
star
22

gulp-esprima

Parse JS to ASTs w/ esprima
JavaScript
4
star
23

updown

Let your users vote
Ruby
4
star
24

CloudDevelop

An online development tool allowing coders to write and compile snippets of source code in a variety of languages, all within a web browser
JavaScript
4
star
25

deft

JS dependency declaration
JavaScript
4
star
26

NBinarySearch

Flexible binary search for all indexed collections in .NET
C#
4
star
27

race.js

Pit JavaScript libraries against each other
JavaScript
4
star
28

sortfix

Fixes Array.prototype.sort
JavaScript
2
star
29

SimpleDevelop

A cross-platform multi-language snippet compiler?
JavaScript
2
star
30

protips

I am a pro, and these are my tips
2
star
31

truman.js

Smoke & mirrors so you can prototype quickly
JavaScript
2
star
32

just-use-markdown

Put Markdown in <script> tags
JavaScript
2
star
33

ghdb

Ruby
1
star
34

randy

Generate random stuff
Ruby
1
star
35

shmomise

Promises, shmomises
JavaScript
1
star
36

VariantCollections

A library to provide some much-needed type variance to widely used collection classes from the .NET BCL
C#
1
star
37

fancy-server

Oh server, you so fancy
JavaScript
1
star
38

writer.js

Abstraction library for writing to console, strings
JavaScript
1
star
39

superout

When stdout just isn't enough
JavaScript
1
star
40

cleanSlate.js

Reset any pending asynchronous actions in the browser
JavaScript
1
star
41

validatorsquared

The validator validator
1
star
42

django-starter

Like sourdough starter, but replace sourdough with Django
Python
1
star
43

gquery

Generic jQuery
JavaScript
1
star
44

sketchy.js

Another HTML5 canvas drawing pad
JavaScript
1
star
45

Charter

Make your own web-embeddable charts really fast and easily
JavaScript
1
star
46

RPerft

Ruby gem for performance testing w/ Perft
Ruby
1
star
47

LiveDraft

Just an idea—we'll see where it goes
JavaScript
1
star