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

color-extensions

v1.2.0

Published

Parse, convert and interpolate colors and create colormaps.

Downloads

100

Readme

Color extensions

Parse and convert colors. Interpolate between colors or create colormaps as lists.

Features

  • Automatically infer color type and parse from string.
  • Convert between types.
  • Create colormaps of two or more colors.
  • Interpolate colors in colormaps.
  • Create lists of colors from colormaps.

Currently supported types

  • Hex
    • Short or long form, with or without hash
    • #000, #FF00FF, ae141c
  • RGB
    • Whitespace between values allowed but not encouraged
    • rgb(255, 0, 123) rgb(0,0,255)
  • RGBA
    • rgba(0, 0, 0, 0.5)
  • HSL
    • hsl(120, 100%, 50%)
  • HSLA
    • hsla(120, 100%, 50%, 0.5)
  • Javascript object
    • Properties r, g, b, a

Examples

const {
    hexToRgb,
    objToRgb,
    convertTo,
    getColorType
} = require("color-extensions");

// Convert between types
hexToRgb("#fff"); // Produces 'rgb(255,255,255)'
objToRgb({ r: 255, g: 128, b: 0, a: 0.1 }); // Produces 'rgba(255,128,0,0.1)'

// Automatic inference of source type.
convertTo("#00ff00", "rgb"); // Produces 'rgb(0,255,0)'
convertTo("#00ff00", "object"); // Produces { r: 0, g: 255, b: 0 }
convertTo("hsla(240, 100%, 50%, 0.5)", "rgb"); // Produces 'rgba(0, 0, 255, 0.5)'

// Infer the types.
getColorType("#FFF"); // Produces 'hex_short'
getColorType("#FFF000"); // Produces 'hex_long'
const { ColorInterpolator } = require("color-extensions");

// Create colormap from interval points (range 0-1) and endpoint colors.
const colorMap = {
    0: "#fff", // Start white
    0.3: "#f00", // Red
    0.5: "#000", // Black
    1.0: "#0000ff" // End with blue
};

const interpolator = new ColorInterpolator(colorMap);

// Get single value.
interpolator.getColor(0.23); // Produces '#ff3c3c', the type is inferred from colormap.
interpolator.getColor(0.23, "rgb"); // Produces 'rgb(255,60,60)'

// Get range of color values.
interpolator.generateColors(10);
/*
Produces:
[
    '#ffffff',
    '#ffa1a1',
    '#ff4242',
    '#d50000',
    '#470000',
    '#00001c',
    '#000055',
    '#00008e',
    '#0000c6',
    '#0000ff'
]
*/

// Same thing, explicit format.
interpolator.generateColors(10, "rgb");

// Static version without creating object, also using alpha values.
ColorInterpolator.generateColors(10, "rgba(0,0,0,0.5)", "rgba(255,255,255,1)");

Installation

npm install color-extensions

Node:

const colorExtensions = require("color-extensions")

Webpack:

import * as colorExtensions from "color-extensions";

Browser script:

Include file colorextensions.min.js in page and access through global object ColorExtensions

Conversions

Root level functions.

Functions converting to hex take optional object which has properties prefix and shortForm. Hex colors with shortForm=true use short (3 character) hex color if possible and fall back into 6 character form if not applicable.

Classes

Root level classes.

ColorMap

Represents a color map used by interpolator and produces interval colors. Not really necessary for user.

ColorInterpolator

Produces values from colormap.

Changelog

1.2

  • Support for HSL and HSLA types
  • Various minor fixes and improvements

1.1

  • Support interpolating alpha values

TODO

  • Hardcoded Matplotlib colormaps