npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deepcolour

v2.2.0

Published

Parsing, conversion and manipulation of RGBA, CSS and HSV colours, as well as vectors

Downloads

13

Readme

deepcolour is a stateful library for the parsing, processing and formatting of RGBA colours, as well as vector spaces.

Features

  • All Colour values unclamped and normalized to 0-1.
  • Most operations are in-place.
  • Stateful HSV properties.
  • Conversion to and from CSS.

Example

const Colour = require('deepcolour')

let colour = new Colour(1,0,0,1)

console.log( colour.hex )
// #ff0000

// make colour cyan by shifting hue
colour.hue = 0.5

console.log( colour.hex )
// #00ffff

// Set the colour with a string
colour.set('white')

// Retrieve value and saturation
console.log( colour.value, colour.saturation )
// 1,0

API

Number Properties

  • red : RGB red value.
  • green : RGB green value.
  • blue : RGB blue value.
  • alpha : Alpha value.
  • hue : HSV hue. The range of the HSV colour circle is 0-1. See Hue Preservation.
  • saturation : HSV saturation.
  • value : HSV value.

String Properties

  • hex : CSS hexadecimal color. #112233
  • css : CSS color. If the colour can be represented as a hexadecimal color, hex will be returned. Otherwise a CSS rgb() or rgba() will be returned.

Set Methods

All methods beginning with set may be chained together.

set ( anything ) Safely calls other set function based on type.

setDefault () Sets the colour to opaque black, [0,0,0,1].

setArguments ( arguments ) Sets from an arguments object. The following formats are supported:

  • ( [ red [, green [, blue, [, alpha ] ] ] )
  • ( 'string' [, alpha ] )

set8BitArray ( Array-like ) Set from an array of numbers in the range 0-255. Up to four values will be read, in the order RGBA. This method will also work for slices of Buffer objects, allowing it to work with image libraries such as png-js. Throws error on invalid input.

setRGB ( Number, Number, Number ) Sets red, green and blue values. If non-numeric values are passed, existing values are preserved.

setHSV ( Number, Number, Number ) Sets hue, saturation and value values. If non-numeric values are passed, existing values are preserved.

setKeys ( object ) Sets properties from an object. The following keys are supported: ['red','r','green','g','blue','b','alpha','a','hue','h','saturation','sat','s','value','v','hex','css']

setAlpha ( Number ) Set alpha value.

setRandom () Set red, green and blue to random values in standard range.

setChannel ( index, Number) Set the channel index to a value.

setChannelHex ( index, string ) Set the channel index to a hexadecimal value. The string may be 1 or 2 digits.

Output Methods

toString () Alias for toCSSUnclamped

toHexString () '#ff00ff' CSS hex, clamped with no alpha.

toRGBA () [1,0,1,1] Raw values.

toObject () { r: 1, g: 0, b: 1, a: 1} Raw values.

toBuffer () Buffer.from([255,0,255,255]) Clamped, 8-bit values.

to8BitArrays ( length ) [255,0,255,255] Clamped, 8-bit values.

toHexChannels ( 'rgbahsv' ) 'ff00ffd4ffff' Clamped, 8-bit values from RGB and HSV as hex.

toCSS ( format = 'auto' | 'rgba' | 'hex' | 'unclamped' ) CSS in given format.

toCSSRGBA () 'rgb(255,0,255)' Clamped, 8-bit values with auto adding alpha.

toCSSUnclamped () 'rgba(1024,0,1024,1)' Unclamped values in CSS range, always has alpha.

Other Methods

  • isBlack () -> bool red == 0 && green == 0 && blue == 0
  • isGray () -> bool red == green == blue

Hue Preservation

In HSV & HLS colour spaces, hue and saturation can be invalid when the colour is grey or black. In purely functional colour conversions, this results in saturation being lost when value == 0 and hue being lost when saturation == 0. This is particularly annoying in stateful system, where value or saturation are being modulated.

deepcolour compensates for this by holding hue and saturation values.

// Initialize a new Colour
let colour = new Colour('green')
assert.equal( colour.hue, 1/3)

// Set the colour to black
colour.value = 0
assert( colour.isBlack() )

// Since it does not matter when value is 0,
// hue is preserved.
assert.equal( colour.hue, 1/3)

// We can set the hue, as well.
colour.hue = 1/6

// Colour is still black
assert( colour.isBlack() )

// But the hue is preserved
assert.equal( colour.hue, 1/6)

// Set the colour back to full intensity
colour.value = 1

// Is now yellow
assert.equal( colour.hex, '#ffff00' )

Vectors

By default deepcolour creates a colour space with rgba properties. The library can also be used to create and manipulate arbitrary vector spaces, with or without colour properties.

Colourless Vectors

const { Space } = require('deepcolour')
const Vector = Space({
  rgba: false,
  channels: 'xyz'
})

let vec = Vector.add( [ 1, 0, 0 ], { y: 0.5 } )
assert.deepEqual( vec.toArray(), [ 1, 0.5, 0 ] )

Full Vertex

const { Space } = require('deepcolour')
const Vertex = Space({
  rgba: 5,
  channels: 'xyzuvrgba'
})

let vert = new Vertex( { x: 3, y: 2, z: 1 } )

// All colour manipulation methods may be used.
vert.set('cyan')
vert.alpha = 0.5

assert.deepEqual( vert.toArray(), [ 3, 2, 1, 0, 0, 0, 1, 1, 0.5 ] )