Color palette text parser to a function, compatible with GMT, GDAL, GRASS, PostGIS, ArcGIS

Overview

cpt2js

Color palette text parser to a function, input compatible with GMT, GDAL, GRASS, PostGIS, ArcGIS

Demo

From GDAL docs:

The text-based color configuration file generally contains 4 columns per line: the elevation value and the corresponding Red, Green, Blue component (between 0 and 255). The elevation value can be any floating point value, or the nv keyword for the nodata value. The elevation can also be expressed as a percentage: 0% being the minimum value found in the raster, 100% the maximum value.

An extra column can be optionally added for the alpha component. If it is not specified, full opacity (255) is assumed.

Various field separators are accepted: comma, tabulation, spaces, ‘:’.

Common colors used by GRASS can also be specified by using their name, instead of the RGB triplet. The supported list is: white, black, red, green, blue, yellow, magenta, cyan, aqua, grey/gray, orange, brown, purple/violet and indigo.

GMT .cpt palette files are also supported (COLOR_MODEL = RGB only).

Note: the syntax of the color configuration file is derived from the one supported by GRASS r.colors utility. ESRI HDR color table files (.clr) also match that syntax. The alpha component and the support of tab and comma as separators are GDAL specific extensions.

Differences from GDAL:

Supported color formats and modes:

  • color formats - named, hex, CSS, RGB, HSL, HSV
  • color modes - RGB, HSL, HSV
  • more color formats and modes can be added as needed

Color palette references:

Install

npm install cpt2js

or

<script src="https://unpkg.com/[email protected]/dist/cpt2js.umd.min.js"></script>

Usage

The library exposes a function parsePalette, which can be used to parse the color palette text or array.

Formats:

The second argument of parsePalette is an options object:

  • bounds ([number, number]) - used for resolving relative values to absolute values, default [0, 1]

The parse result is a Chroma.js Scale, a function (value: number) => Color.

The colors returned are Chroma.js Color objects, with default toString method returning a hex color.

From text

import { parsePalette } from 'cpt2js';

const palette = `
0   black
1   white
`;
const paletteScale = parsePalette(palette);

paletteScale(0.5).toString(); // '#808080'
paletteScale(0.5).css(); // 'rgb(128, 128, 128)' - use for CSS
paletteScale(0.5).rgba(); // [128, 128, 128, 1] - use for deck.gl, multiply alpha by 255

From text - Relative values

import { parsePalette } from 'cpt2js';

const palette = `
0%   black
100% white
`;
const paletteScale = parsePalette(palette, { bounds: [0, 100] });

paletteScale(50).toString(); // '#808080'

From array

import { parsePalette } from 'cpt2js';

const palette = [
  [0, 'black'],
  [1, 'white'],
];
const paletteScale = parsePalette(palette);

paletteScale(0.5).toString(); // '#808080'
paletteScale(0.5).css(); // 'rgb(128, 128, 128)' - use for CSS
paletteScale(0.5).rgba(); // [128, 128, 128, 1] - use for deck.gl, multiply alpha by 255

From array - Relative values

import { parsePalette } from 'cpt2js';

const palette = [
  ['0%',   'black'],
  ['100%', 'white'],
];
const paletteScale = parsePalette(palette, { bounds: [0, 100] });

paletteScale(50).toString(); // '#808080'

Color ramp

The library exposes a function colorRampCanvas, which can be used to color ramp the scale function to a canvas. The canvas can be encoded to a Data URL and rendered as an image.

The second argument of colorRampCanvas is an options object:

  • width (number) - width of the canvas, used also as a number of color ramp colors, default 256
  • height (number) - height of the canvas, default 1
import { parsePalette, colorRampCanvas } from 'cpt2js';

const palette = `
0   black
1   white
`;
const paletteScale = parsePalette(palette);
const paletteCanvas = colorRampCanvas(scale);
const paletteCanvasDataUrl = paletteCanvas.toDataURL();
const html = `<img src="${paletteCanvasDataUrl}">`;

Text format

Formats

GMT4
0   0   0   0   1   255 255 255
GMT5
0   0/0/0   1   255/255/255
GDAL, GRASS, PostGIS, ArcGIS
0   0   0   0
1   255 255 255

Values

Absolute
0   black
Relative
0%  black
Nodata
N      gray
nv     gray
null   gray
nodata gray

Colors

Named
0   black
1   white
Hex
0   #000000
1   #ffffff
RGB
0   0   0   0
1   255 255 255
Alpha
0   0   0   0   0
1   255 255 255 255
HSL
# COLOR_MODEL = hsl
0   300 1 0.5
0.5 150 1 0.5
1   0   1 0.5
HSV
# COLOR_MODEL = hsv
0   300 1 1
0.5 150 1 1
1   0   1 1

Thanks

Discussion at stac-extensions/raster#17 and Cloud-Native Geospatial Outreach Event 2022 that sparked the idea for the library.

You might also like...

Tries to execute sync/async function, returns a specified default value if the function throws

good-try Tries to execute sync/async function, returns a specified default value if the function throws. Why Why not nice-try with it's 70+ million do

Dec 8, 2022

Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Dec 19, 2022

Palette plugin using CSS variables for TailwindCSS

 Palette plugin using CSS variables for TailwindCSS

Palette plugin using CSS variables for TailwindCSS Adds a color palette (from 100 to 900) for each color of your tailwind configuration while using CS

Dec 28, 2022

MacOS launcher & command palette

MacOS launcher & command palette

About If you ever liked tools like Raycast and Alfred, but wanted to own them and tweak them yourself, then SOL is the tool for you. All the affordanc

Dec 30, 2022

Supercharge your All-in-One workspace with the Command Palette within Notion 🕹️

Supercharge your All-in-One workspace with the Command Palette within Notion 🕹️

Notion Palette Supercharge your All-in-One workspace with the Command Palette within Notion 🕹️ Notion Palette is a free and open source extension, yo

Nov 10, 2022

✒️ A VSCode-like command palette plugin for inkdrop

✒️ A VSCode-like command palette plugin for inkdrop

inkdrop command palette A VSCode-like command palette for Inkdrop. (Invoke using Ctrl+K) https://my.inkdrop.app/plugins/command-palette 💡 You may nee

Nov 1, 2022

Toaster is a Pure Javascript plugin for displaying toast notifications. It comes with different options e.g custom text or HTML message, duration, custom class, toggle close button, position, custom close icon and backgorund color.

Pure Javascript Toaster Requires Nothing Demo Toaster is a Pure Javascript plugin for displaying toast notifications. It comes with different options

Jun 21, 2022

Twitter Text Libraries. This code is used at Twitter to tokenize and parse text to meet the expectations for what can be used on the platform.

twitter-text This repository is a collection of libraries and conformance tests to standardize parsing of Tweet text. It synchronizes development, tes

Jan 8, 2023

Obsidian text generator Plugin Text generator using GPT-3 (OpenAI)

Obsidian text generator Plugin Text generator using GPT-3 (OpenAI)

is a handy plugin for Obsidian that helps you generate text content using the powerful language model GP

Dec 29, 2022
Comments
  • "Normalize" colors to return RGB?

    As a consumer, I think it would be nice to have an option that allows getting the colors always as RGB so that I don't need to care about the different types (color name, RGB, HSV, HSL)?

    Thanks for this library!

    opened by m-mohr 4
  • Palettes with values in scientific notation are parsed incorrectly

    Palettes with values in scientific notation are parsed incorrectly

    Palettes with values in scientific notation such as:

    5.3e-11	198	188	115	0
    5.3e-9	198	188	115
    5.3e-8	228	103	42
    1e-7	75	12	0
    

    are parsed incorrectly, due to - character detected as GMT5 format.

    opened by zakjan 1
Owner
WeatherLayers
Weather Visualization as a Service
WeatherLayers
Color palette generation function using hue cycling and simple easing functions.

Rampensau ?? Color palette generation function using hue cycling and easing functions. Check out a simple demo or see it in action over on farbvelo 10

David Aerne 24 Dec 28, 2022
Exposes theming options available in Joy UI by providing color palette and typography controls.

Joy Theme Creator Note: Joy UI is currently in alpha - some things may not be finished or working as expected. This project exposes the theming option

Oliver Benns 10 Dec 28, 2022
Contains html file showcasing Earthquake related data generated in the form of VR model, ArcGIS API with real-time earthquake feed and video of simulation of earthquake generated in blender

Module-EADGI-Project-All about Earthquakes Introduction Contains html file showcasing Earthquake related data generated in the form of VR model, ArcGI

Abhishek Rawat 2 Jun 9, 2022
Json-parser - A parser for json-objects without dependencies

Json Parser This is a experimental tool that I create for educational purposes, it's based in the jq works With this tool you can parse json-like stri

Gabriel Guerra 1 Jan 3, 2022
A simple C++ function parser/tagger based on tree-sitter

What the func?! - A simple C++ function parser/tagger This project implements a simple C++ function parser, what-the-func, based on the tree-sitter C+

Software and Systems Engineering Chair 4 - Faculty of Informatics 3 May 21, 2022
Change the color of an image to a specific color you have in mind.

image-recolor Run it: https://image-recolor.vercel.app/ image.recolor.mov Acknowledgments Daniel Büchele for the algorithm: https://twitter.com/daniel

Christopher Chedeau 21 Oct 25, 2022
Tool Cool Color Picker is a color picker library written in typescript and using web component technologies.

Tool Cool Color Picker Tool Cool Color Picker is a color picker library written in typescript and using web component technologies. Check out the demo

Tool Cool 13 Oct 23, 2022
Colr Pickr, a vanilla JavaScript color picker component built with SVGs, with features like saving colors. Similar design to the chrome-dev-tools color picker.

Colr Pickr Colr Pickr, a vanilla JavaScript color picking component built with SVGs, with features like saving colors. Similar design to the chrome-de

TEK 27 Jun 27, 2022
A NodeJS package to convert any RGB color to HEX color or viceversa. Also supports HSL conversion.

Unhex ?? A NodeJS package to convert any RGB color to HEX, HSL color or viceversa. Example div { color: #fff; background-color: #0070f3; } After r

Arnau Espin 2 Oct 1, 2022
A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM deffiniation and appropriate file structure.

Welcome to function-stencil ?? A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM

Ben Smith 21 Jun 20, 2022