Colormap
C++ headers for mapping values to colors and creating PPM images from them
Install / Use
/learn @jgreitemann/ColormapREADME
colormap
This repository contains a number of somewhat connected utilities which originated in the development of the svm-order-params project, but are independent from this application. It's components are split amongs several headers:
color.hpp: Provides acolormap::colorclass representing a grayscale, RGB, or RGBA color, including the ability to mix colors.map.hpp: Provides thecolormap::map, a functor that maps real numbers to a color by interpolating between colors at pre-defined support points.palettes.hpp: Defines a variety of ready-to-usecolormap::maps, mostly inspired by ColorBrewer and the gnuplot-palettes repository by Gnuplotting (a.k.a. Hagen Wierstorf). The palettes are exposed through a global variablecolormap::palettes, astd::mapthat maps names (std::strings) tocolormap::maps.itadpt/map_iterator_adapter.hpp: Defines an iterator adapter that lazily applies a functor on a base iterator upon dereferenciation. Notable API are the non-member functionsmap_iterator(BaseIterator, Functor&)which returns amap_iterator_adapter<BaseIterator, Functor>, andmap(Container const&, Functor&)which returns a quasi-container objectmapped<Container, Functor>.
grid.hpp: Provides a classcolormap::gridwhich represents multidimensional uniform grids which can be initialized very easily and are cheap and iterable.pixmap.hpp: Provides a classcolormap::pixmapwhich can write iterators overcolors to disk in PPM (or PGM) format, both in binary, and in ASCII form.
Instalation
To install colormaps use the following commands:
$ git clone https://github.com/JulianWww/colormap
$ mkdir build && cd build
$ cmake ..
$ make install
than include using:
find_package(colormap INTERFACE)
and add as a library to your project. Tested for:
- Unix
Using colormaps
The following code snippet showcases some of the features of the headers in this
repository. For a compilable example, see test/mandelbrot.cpp.
using namespace colormap;
// set up the grid: 701 x 401 grid points (pixels) representing the region
// [-2.5, 1] x [-1, 1].
grid<2, major_order::col> g { {701, {-2.5, 1.}}, {401, {-1., 1.}} };
using grid_point_t = typename decltype(g)::grid_point_type;
// set up the functor to be visualized on the grid, e.g. the mandelbrot set
auto mandelbrot = [] (grid_point_t c_arr) { /* omitted */ };
// Iterator adapter mapping grid points to function values.
// Result is a `mapped` object that behave like a container.
auto val = itadpt::map(g, mandelbrot);
// find the maximum value
double max = *std::max_element(val.begin(), val.end());
// get a colormap and rescale it
auto pal = palettes.at("inferno").rescale(1, max);
// and use it to map the values to colors
auto pix = itadpt::map(val, pal);
// Construct a PPM object from the `mapped` object `pix`. Color space is
// inferred from color type of pix. "inferno" is an RGB palette, so `pmap`
// will represent a PPM image. For a grayscale colormap, it would result in
// a PGM image.
pixmap<decltype(pix.begin())> pmap(pix.begin(), g.shape());
std::ofstream os("appleman." + pmap.file_extension(),
std::ios_base::binary);
pmap.write_binary(os);
produces the image appleman.ppm:

Color Palettes

License
Copyright (C) 2018-2019 Jonas Greitemann
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Related Skills
node-connect
338.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
338.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.6kCommit, push, and open a PR
