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-scales

v3.0.2

Published

A simple utility mimicking Microsoft Excel's Color Scales conditional formatting, which returns the color of a value in a linear gradient between two color endpoints with defined min and max values.

Downloads

8,797

Readme

Codecov Coverage

color-scales

A utility mimicking Microsoft Excel's Color Scales conditional formatting, which returns the color of a value in a linear gradient between two color endpoints with defined min and max values.

Basic CodePen Demo

Usage

Install from NPM

npm i color-scales

Import it in your project:

const ColorScale = require("color-scales")

Alternatively, import directly from Skypack, a free CDN for Javascript/TypeScript packages:

import ColorScale from "https://cdn.skypack.dev/color-scales";

Important Classes

ColorScale Class

The ColorScale has the following properties:

  • min (number): The minimum value of the range.
  • max (number): The maximum value of the range.
  • colorStops (string[]): An array of colors that act as color stops for the gradient. The colors are spaced out evenly. At least two colors must be passed in. Example: ['#ff0000', '#00ff00', '#0000ff']
  • alpha (number): The alpha value that indicates the level of transparency/opagueness. E.g. Alpha of 0.8 means 20% transparent. Optional, defaults to 1.

Its constructor is ColorScale(min, max, colorStops, alpha);

The ColorScale class has the following functions:

  • getColor: Param: number. Returns an instance Color corresponding to the numerical value supplied, calculated based on the class properties above.

Color Class

The Color class is an unexported class. It has the following properties:

  • r: An integer representing the intensity of the red hue. Ranges from 0 to 255.
  • g: An integer representing the intensity of the green hue. Ranges from 0 to 255.
  • b: An integer representing the intensity of the blue hue. Ranges from 0 to 255.
  • a: The alpha value that indicates the level of transparency/opagueness. E.g. Alpha of 0.8 means 20% transparent. Defaults to 1.

The Color class has the following functions:

  • toHexString: Returns the equivalent hex string representation. The string will be in lower case. Does not support transparency. Example: "#7f7f7f"
  • toRGBString: Returns the equivalent RGB string representation. The string will be in lower case. Does not support transparency. Example: "rgb(127,127,127)"
  • toRGBAString: Returns the equivalent RGBA string representation. The string will be in lower case. Example: "rgba(127,127,127, 0.8)"

Example Usage

Constructor

The following import and constructor creates a ColorScale object.

const ColorScale = require("color-scales");
// Alternatively, import from Skypack, a free CDN for Javascript/TypeScript packages:
// import ColorScale from "https://cdn.skypack.dev/color-scales";

let colorScale = new ColorScale(min, max, colorStops, alpha); // alpha is optional. defaults to 1

where min, max, colorStops, and alpha are replaced by their intended value;

Example:

const ColorScale = require("color-scales");

let colorScale = new ColorScale(0, 100, ["#ffffff", "#000000"], 0.5); // white to black from 0 to 100 with 50% transparency

Get Color Object

Example:

const ColorScale = require("color-scales");

let colorScale = new ColorScale(0, 100, ["#ffffff", "#000000"], 0.5); // red to green from 0 to 100
let colorObj = colorScale.getColor(50); // returns new Color(127, 127, 127, 0.5)

Get Hex String

Example 1 (full opacity):

let colorScale = new ColorScale(0, 100, ["#ffffff", "#000000"]); // passing in no alpha value defaults it to 1
let hexStr = colorScale.getColor(50).toHexString(); // returns "#7f7f7f"

Example 2 (0.5 alpha):

let colorScale = new ColorScale(0, 100,[ "#ffffff", "#000000"], 0.5);
let hexStr = colorScale.getColor(50).toHexString(); // returns "#3f3f3f"

As this will give acolor equivalent to if the transparent color was overlaid on a white background. This package version does not support 8-digit hex values. Thus, if users want to implement true transparency, it is recommended that they use the RGBA string option documented in the next section.

Get RGB String

Example 1 (full opacity):

let colorScale = new ColorScale(0, 100, ["#ffffff", "#000000"]); // passing in no alpha value defaults it to 1
let rgbStr = colorScale.getColor(50).toRGBString(); returns "rgba(127,127,127)"

Example 2 (0.5 alpha):

let colorScale = new ColorScale(0, 100, ["#ffffff", "#000000"], 0.5);
let rgbStr = colorScale.getColor(50).toRGBString(); // returns "rgba(63,63,63)"

As this will give color equivalent to if the transparent color was overlaid on a white background. Thus, if users want to implement true transparency, it is recommended that they use the RGBA string option documented in the next section.

Get RGBA String

Example 1 (full opacity):

let colorScale = new ColorScale(0, 100, ["#ffffff", "#000000"]);
let rgbaStr = colorScale.getColor(50).toRGBAString(); // returns "rgba(127,127,127)"

Example 2 (50% transparent):

let colorScale = new ColorScale(0, 100, ["#ffffff", "#000000"], 0.5);
let rgbaStr = colorScale.getColor(50).toRGBAString(); // returns "rgba(127,127,127, 0.5)"