• Stars
    star
    157
  • Rank 230,168 (Top 5 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 14 years ago
  • Updated over 8 years ago

Reviews

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

Repository Details

Color management JavaScript libary

color.js

Version 0.2.1.2

Create and manipulate colors with ease.

Examples

Making violet (#EE82EE):

Color.get("violet")
Color(0xEE82EE)
Color("#EE82EE")
Color("EE82EE")
Color(0xEE, 0x82, 0xEE)
Color("EE", "82", "EE")
Color(238, 130, 238)
Color.rgb(238, 130, 238)
Color.hsl(0.8333333333333334, 0.7605633802816902, 0.7215686274509804)
// 300°, 75.88652482269505%, 72.35294117647058%

Shorthand color notation:

Color("#ABC") // "#AABBCC"
Color("ABC")  // "#AABBCC"

Convert a hex color to RGBA:

var c = new Color('#ABC123', 0.6);
c.getRGBA(); // rgba(171,193,35,0.6)

setValue and channels

var myColor = new Color;
myColor.setValue(0xAA7F00);
myColor.hexTriplet() === "#aa7f00";
myColor.channels[0] = 0xFF;
myColor.hexTriplet() === "#ff7f00"; // orange

Defining a custom color:

Color.define("rind", [92, 163, 16])
Color.get("rind").hexTriplet() === "#5ca310"

Get a random color between green and blue

Color.random("green", "blue");

Parsing CSS color values

red   = Color.parse("rgb(255, 0, 0)");
green = Color.parse("rgb ,128"); // shorthand
blue  = Color.parse("hsl(240, 100%, 50%)");

red.hexTriplet()   === "#ff0000";
green.hexTriplet() === "#008000";
blue.hexTriplet()  === "#0000ff";

Supported Browsers

  • Firefox 1+
  • Safari 2+
  • IE 6+
  • Opera 7+
  • Google Chrome 0.1+

Usage

color.js can be used in any ECMAScript environment as it does not make use of the DOM. color.js is very useful on the server-side and the client-side.

color.js CSS Module

The color.js CSS module defines all of the standard CSS colors for use in color.js.

API

Note: HSL values are in the form of fractions (0 to 1). A hue value of 0.5 is equivalent to a hue of 180°.

Instantiation

Color objects can be instantiated in any of the following ways:

color = [new ]Color(color:int | string, [, alpha:float])
color = [new ]Color(red:int | string, green:int | string, blue:int | string [, alpha:float | string])
color = Color.rgb(red:int, green:int, blue:int [, alpha:float])
color = Color.hsl(hue:float, saturation:float, lightness:float [, alpha:float])

Instance methods and properties

channels : array
An array containing a color's red, green, blue, and alpha channels in that order.
rgb()
Returns a CSS rgb() function representing the color.
rgba()
Returns a CSS rgba() function representing the color.
hsl()
Returns a CSS hsl() function representing the color.
hsla()
Returns a CSS hsla() function representing the color.
css()
If the alpha channel is 1, this returns hexTriplet(). Otherwise, this returns rgba().
getValue() and valueOf()
Returns an integer representation of the color.
setValue(value:int)
Sets the to value.
hexTriplet()
Returns the hex triplet (#RRGGBB) representation of the color.
rgbData()
Returns an array containing the color's red, green, and blue channels in that order.
hslData()
Returns an array containing the color's hue, saturation, and lightness channels in that order.
toString()
Returns this[Color.TO_STRING_METHOD]().

Constructor methods

Color names are case-insensitive.

Color.define(colorName:string, RGB:array | string)
Saves the specified RGB color under colorName, for later retrieval.
Color.get(colorName:string)
Returns a new color instance instance using the RGB data from a previously defined color with the name, colorName.
Color.del(colorName:string)
Deletes a previously defined color with the name of colorName.
Color.clearColors()
Clears all color definitions.
Color.parse(cssFunction:string)
Returns the color represented by a CSS function (eg. hsl(0, 100%, 50%)) or hex triplet. If no color could be parsed, this returns null. This function intentionally allows invalid syntax when parsing css functions. As long as the first three non-whitespace characters (case-insensitive) are "rgb" or "hsl" and the arguments (which are all optional and default to zero) are separated by commas, this function will be able to parse the CSS function.
Color.random([rangeStart:int | string] [, rangeEnd:int | string])
Returns a random color from rangeStart to rangeEnd. If rangeStart is not specified, this returns a random color from #000000 to #FFFFFF. rangeStart and rangeEnd can also be color names.
Color.rgb(red:int, green:int, blue:int [, alpha:float])
Returns a color constructed from the specified RGB values.
Color.hsl(hue:float, saturation:float, lightness:float [, alpha:float])
Returns a color constructed from the specified HSL values.
Color.RGBtoHSL(rgb:array)
Returns the array of RGB values converted to HSL values.
Color.HSLtoRGB(hsl:array)
Returns the array of HSL values converted to RGB values.

Configuration properties

Color.TO_STRING_METHOD : string
The method name of which to call when a color instance's toString() method is called. This defaults to hexTriplet.

Tracking image

More Repositories

1

FileSaver.js

An HTML5 saveAs() FileSaver implementation
JavaScript
20,738
star
2

Blob.js

An HTML5 Blob implementation
JavaScript
1,141
star
3

classList.js

Cross-browser element.classList
JavaScript
1,105
star
4

canvas-toBlob.js

A canvas.toBlob() implementation
JavaScript
657
star
5

l10n.js

Passive localization JavaScript library
JavaScript
392
star
6

jsandbox

A JavaScript sandboxing library that uses web worker threads
JavaScript
190
star
7

async-document-write

An asynchronous document.write implementation
JavaScript
65
star
8

pmxdr

Cross-domain XHR using postMessage
JavaScript
63
star
9

voice-search

Chrome extension for searching by speaking.
JavaScript
48
star
10

async.js

async/await before promises. async.js facilitates asynchronous actions ('promises') with synchronous-style syntax
JavaScript
41
star
11

timer.js

High-precision JavaScript timer
JavaScript
37
star
12

Xccessors

Xccessors (cross-browser accessors) is a JavaScript shim that implements the legacy or standard methods for defining and looking up accessors (getters and setters) of objects.
JavaScript
36
star
13

RetargetMouseScroll

A JavaScript library for retargetting mouse scroll events.
HTML
35
star
14

libxdr

A library that implements a cross-browser XDR constructor
JavaScript
27
star
15

hotlink.js

Hide image referrers
JavaScript
26
star
16

jData-host

jData host JavaScript library.
JavaScript
18
star
17

mumbl

A JavaScript library that abstracts audio-playing functionality of HTML5, Songbird, and SoundManager 2 for use in music playlists
JavaScript
17
star
18

tinylog

A minimalistic logging platform
JavaScript
17
star
19

reddit-bots

A collection of various reddit bots.
Python
17
star
20

emoji-favicon-toolkit

Emoji Favicon Toolkit - Set your favicon to emoji using canvas & cache as /favicon.ico with service workers
TypeScript
15
star
21

e4x.js

Implementation of all of the optional features in the ECMA-357 specification.
JavaScript
13
star
22

subscribe.js

An easy-to-use and highly extensible client-side notification-based feed reader Service Worker (coming soon!)
JavaScript
11
star
23

mutaprophylaxis

Methods for preventing unauthorized DOM mutations
JavaScript
10
star
24

code.eligrey.com-archive

Archive of code.eligrey.com
JavaScript
8
star
25

attrs

JavaScript 1.6+ library for setting DOM element attributes
JavaScript
7
star
26

chrome-enhanced-font-smoothing

Chrome extension that that improves font smoothing on Windows
CSS
6
star
27

CiteDrag

CiteDrag automatically cites data dragged from one website to a normal text input (ie. input type="text", textarea) or rich text input field (ie. Microsoft Word, contenteditable HTML elements, your blogging platform, etc.)
JavaScript
5
star
28

BlobBuilder.js

Moved to https://github.com/eligrey/Blob.js
4
star
29

js-iterators

A collection of iterator and generator-related JavaScript scripts.
JavaScript
4
star
30

http-index-format-automator

Generates application/http-index-format for directory listings
PHP
3
star
31

mixest-downloader

Adds download functionality to mixest.com.
JavaScript
3
star
32

jil

jData Interface Library (client JavaScript library)
JavaScript
3
star
33

myrandom.js

reddit's paywalled myrandom button, for everyone
JavaScript
2
star
34

e4x-array-methods.js

A JavaScript library that implements array methods for E4X XML. Intended for making XML easier to use with server-side JavaScript.
JavaScript
2
star
35

social-analyzer

API, CLI & Web App for analyzing & finding a person's profile across 350+ social media websites (Detections are updated regularly)
JavaScript
2
star
36

docswap

DOM document replacement tools
TypeScript
1
star
37

clipvertise

Use the clipboard for ads or something
1
star
38

speculative-request-control

Speculative Request Control explainer
Bikeshed
1
star