JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2572
  • Score
    100M100P100Q133786F
  • License MIT

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.

Package Exports

  • color-scales

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (color-scales) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

color-scales-js

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

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

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

Important Classes

ColorScale Class

The ColorScale has the following properties:

  • min: The minimum value of the range.
  • max: The maximum value of the range.
  • minColor: The minimum color corresponding to the minimum value.
  • maxColor: The maximum color corresponding to the maximum value.

Its constructor is ColorScale(min, max, minColor, maxColor);

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.

The Color class has the following functions:

  • toHexString: Returns the equivalent hex string representation. The string will be in lower case. Example: "#7f7f7f"
  • toRGBString: Returns the equivalent RGB string representation. The string will be in lower case. Example: "rgb(127,127,127)"

Example Usage

Constructor

The following import and constructor creates a ColorScale object.

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

let colorScale = new ColorScales(min, max, minColor, maxColor);

where min, max, minColor, are maxColor are replaced by their intended value;

Example:

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

let colorScale = new ColorScales(0, 100, "#ffffff", "#000000"); // white to black from 0 to 100

Get Color Object

Example:

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

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

Get Hex String

Example:

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

let colorScale = new ColorScales(0, 100, "#ffffff", "#000000");
let hexStr = colorScale.getColor(50).toHexString(); // returns "#7f7f7f"

Get RGB String

Example:

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

let colorScale = new ColorScales(0, 100, "#ffffff", "#000000");
let rgbStr = colorScale.getColor(50).toRGBString(); // returns "rgb(127,127,127)"