Code Monkey home page Code Monkey logo

forcolormap's Introduction

The ForColormap Fortran library is independent of any graphical toolkit: its main functionality is to convert a real value to RGB values that you can use with any drawing toolkit. It includes:

  • the 222 colormaps of the Scientific colour maps collection v8.0.1 by Fabio Crameri. See Fabio Crameri's poster "Scientific Colour Maps" for more information,
  • the "magma", "inferno","plasma", "viridis" matplotlib colormaps,
  • the Dave Green's cubehelix colormap,
  • a few basic colormaps: "black_body", "fire", "rainbow", "inv_rainbow", "zebra".

And it offers various methods and options to manage colormaps.

Basic usage

Assuming your graphical library has a setpixelgb()-like function and you know your z values will be for example in the [0, 2] range, you can write something like:

use forcolormap, only: Colormap, wp
...
type(Colormap) :: cmap
integer  :: red, green, blue
real(wp) :: z, x, y
...
! Let's use the glasgow colormap:
call cmap%set("glasgow", 0.0_wp, 2.0_wp)
...
z = f(x,y)
call cmap%compute_RGB(z, red, green, blue)
call setpixelrgb(x, y, red, green, blue)

The library is using the precision wp=>real64 defined in the module iso_fortran_env. And depending on the integers expected by your graphical library, you may need to convert the kinds of red, green, blue variables.

This guideline can help you choose the right kind of colormap. And you can visually choose the available colormaps in the colormaps_list/ForColormap.pdf manual or on this page (under development): https://github.com/gha3mi/forcolormap/tree/dev

Installation

Requirements

You need, whatever your operating system:

  • a modern Fortran compiler, for example GFortran or the Intel ifort/ifx compilers. See the Fortran-lang.org compilers page for other compilers.
  • The Fortran Package Manager fpm or CMake (>=3.24) & pkg-config for building the project.
    • For writing PPM files, the library ForImage is used as a fpm or CMake dependency (automatically downloaded).

Testing the project with fpm

If you have a GitHub account, just clone the repository. Then launch the demo example, which is creating PPM files with colormaps and colorbars for all the available colormaps:

$ git clone [email protected]:vmagnin/forcolormap.git
$ cd forcolormap
$ fpm run --example demo

Using ForColormap as a fpm dependency

To use ForColormap within your own fpm project, add the following lines to your fpm.toml manifest file:

[dependencies]
forcolormap = {git = "https://github.com/vmagnin/forcolormap.git"}

Using CMake

You can also build the project with CMake:

$ git clone [email protected]:vmagnin/forcolormap.git
$ cd forcolormap
$ mkdir build && cd build
$ cmake ..
$ make
$ sudo make install

Static linking

By default, ForColormap is built as a static library by CMake. You can compile your program with the -static option:

$ gfortran -static my_program.f90 $(pkg-config --cflags --libs forcolormap forimage)

Note that ForColormap is depending on ForImage, and for static linking you must respect that order.

Dynamic linking

There is a CMake option to obtain a shared library:

$ cmake -D BUILD_SHARED_LIBS=true ..

You can compile your program like this:

$ gfortran my_program.f90 $(pkg-config --cflags --libs forcolormap)

If you encounter linking problems, you should verify the content of your PKG_CONFIG_PATH and LD_LIBRARY_PATH environment variables. For example, in Ubuntu or FreeBSD the .pc files will be installed in /usr/local/lib/pkgconfig/ and the libraries in /usr/local/lib/.

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/

Building examples and tests

You can build the examples with:

$ cmake -D BUILD_FORCOLORMAP_EXAMPLES=true ..
$ make
$ cd example

The automatic tests can be run with:

$ cmake -D BUILD_TESTING=true ..
$ make
$ ctest

Uninstalling ForColormap

From the build directory:

$ sudo make uninstall_forcolormap

Note that its dependency ForImage will also be uninstalled! You will have to reinstall it if needed.

You can also choose and remove files listed in build/install_manifest.txt one by one.

See CMake basics for more information.

Learning

In the example directory, you will find these commented demos:

  • demo.f90 creates demo PPM files for each built-in colormap, plus a PPM file with the corresponding colorbars. It also demonstrates how to create your own colormap defined in an array and how to download a colormap from a .txt file.
  • demo_reverse.f90 demonstrates the usage of the reverse=.true. option to reverse the direction of a colormap.
  • colormaps_list.f90 generates the colormaps_list/COLORMAPS_LIST_*.md files.
  • example1.f90 demonstrates how ForImage can be used to import/export PPM files.
  • create.f90 demonstrates creating a custom colormap using methods like create_lagrange() and create_bezier().
  • extract.f90 demonstrates how to create a specific colormap by extracting a specified number of colors of a colormap.
  • info.f90 demonstrates how to obtain information about a colormap using the Colormaps_info class.
  • modify.f90 demonstrates how you can modify a colormap with methods like shift(), in concrete cases.

They can be launched with the command fpm run --example name_of_the_example (without the .f90 extension).

In the gtk-fortran-extra repository, you will also find a physical model demonstrating the use of ForColormap. It creates a movie with Turing patterns, displayed with various colormaps:

https://www.youtube.com/watch?v=cVHLCVVvZ4U

Licenses

This project is under MIT license. The logo files are under license CC BY-SA 4.0.

Citing colormaps

As any work, a colormap should be cited:

References

Articles and books

  • Nuñez, Jamie R., Christopher R. Anderton, and Ryan S. Renslow. “Optimizing Colormaps with Consideration for Color Vision Deficiency to Enable Accurate Interpretation of Scientific Data.” Edited by Jesús Malo. PLOS ONE 13, no. 7, August 1, 2018, e0199239. https://doi.org/10.1371/journal.pone.0199239.
  • Rogowitz, Bernice E, and Lloyd A Treinish. “Why Should Engineers and Scientists Be Worried About Color?”
  • Thyng, Kristen, Chad Greene, Robert Hetland, Heather Zimmerle, and Steven DiMarco. “True Colors of Oceanography: Guidelines for Effective and Accurate Colormap Selection.” Oceanography 29, no. 3, September 1, 2016, pp. 9–13. https://doi.org/10.5670/oceanog.2016.66.
  • Valeur, Bernard. La couleur dans tous ses éclats. Bibliothèque scientifique. Paris: Belin-"Pour la science", 2011, ISBN 9782701158761.
  • Valeur, Bernard. Lumière et luminescence - Ces phénomènes lumineux qui nous entourent. Bibliothèque scientifique. Paris: Belin-"Pour la science", 2005, ISBN 9782701136035.

Web pages

About colormaps

Specific colormaps

forcolormap's People

Contributors

aslozada avatar gha3mi avatar jchristopherson avatar vmagnin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

forcolormap's Issues

Using ForColormap as a static library fails

@jchristopherson @gha3mi

System: Ubuntu 23.10, FreeBSD 14.0
Compiler: GFortran 13.2.0

I am now trying to use ForColormap as a static library (.a under Linux). I have written a toy.f90 minimalist program:

use forcolormap, only: Colormap, wp
type(Colormap) :: cmap

call cmap%set("cubehelix", 0.0_wp, 1.0_wp, 1024)
print *, "test"
end

ForColormap was configured with cmake -D BUILD_SHARED_LIBS=false .. before building and installing.
The .mod files are all in /usr/local/include/ and the two .a are in /usr/local/lib/.

But I can not compile my toy program with the -static option:

$ gfortran -static toy.f90 $(pkg-config --cflags --libs forcolormap)
/usr/local/bin/ld: /usr/local/lib/libforcolormap.a(colormap_class.f90.o): in function `__forcolormap_MOD_write_ppm_colorbar':
colormap_class.f90:(.text+0xc10): undefined reference to `__pnm_MOD___vtab_pnm_Format_pnm'
/usr/local/bin/ld: colormap_class.f90:(.text+0xf38): undefined reference to `__pnm_MOD_set_format'
/usr/local/bin/ld: colormap_class.f90:(.text+0xf90): undefined reference to `__pnm_MOD_set_pnm'
/usr/local/bin/ld: colormap_class.f90:(.text+0xfbe): undefined reference to `__pnm_MOD_export_pnm'
/usr/local/bin/ld: colormap_class.f90:(.text+0x10a7): undefined reference to `__pnm_MOD_set_format'
collect2: error: ld returned 1 exit status

Wrong number of levels for the categorical colormaps

  • In the Colormaps_info class: the categorical colormaps of the Scientific colour maps collection (name terminated by a S) should not have 256 levels, but 100.
  • The PDF guide will have to be regenerated (at least before the 0.9 release).

ForImage dependency

Dear @vmagnin,

Thank you for this project and for mentioning ForImage in the Discourse forum.

I forked ForColormap to add and test ForImage functionality in your project. The PNM functionality of ForImage enables the import and export of PNM files in both binary and ASCII encodings. I have provided an example for your reference.

To review the modifications, kindly visit the GitHub repository at: https://github.com/gha3mi/forcolormap

Best regards,
Ali

Using ForColormap as a shared library fails

@jchristopherson @gha3mi

The issue

I am trying to use ForColormap as a shared library (.so under Linux). I have written a toy.f90 minimalist program:

use forcolormap, only: Colormap, wp
type(Colormap) :: cmap

call cmap%set("cubehelix", 0.0_wp, 1.0_wp, 1024)
end

but the shared library libforimage.so is not found at runtime:

$ gfortran toy.f90 -Xlinker -rpath=/usr/local/lib $(pkg-config --cflags --libs forimage forcolormap)
$ ./a.out
./a.out: error while loading shared libraries: libforimage.so.0: cannot open shared object file: No such file or directory

More information

System: Ubuntu 23.10
Compiler: GFortran 13.2.0

The -Xlinker -rpath=/usr/local/lib option allows the compiler to pass to the linker the runtime library search path.

The ForColormap project was built with the command cmake -D BUILD_SHARED_LIBS=true .., then make, and installed in the system with:

$ sudo make install
[ 46%] Built target forimage
[100%] Built target forcolormap
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/lib/libforimage.so.0.3.0
-- Installing: /usr/local/lib/libforimage.so.0
-- Installing: /usr/local/lib/libforimage.so
-- Up-to-date: /usr/local/include
-- Installing: /usr/local/include/lut.mod
-- Installing: /usr/local/include/pnm.mod
-- Installing: /usr/local/include/forimage_parameters.mod
-- Installing: /usr/local/include/forcolor.mod
-- Installing: /usr/local/include/forimage.mod
-- Installing: /usr/local/lib/cmake/forimage/forimageTargets.cmake
-- Installing: /usr/local/lib/cmake/forimage/forimageTargets-release.cmake
-- Installing: /usr/local/lib/cmake/forimage/forimageConfig.cmake
-- Installing: /usr/local/lib/cmake/forimage/forimageConfigVersion.cmake
-- Installing: /usr/local/lib/pkgconfig/forimage.pc
-- Installing: /usr/local/lib/libforcolormap.so.0.8.0
-- Installing: /usr/local/lib/libforcolormap.so.0
-- Set runtime path of "/usr/local/lib/libforcolormap.so.0.8.0" to ""
-- Installing: /usr/local/lib/libforcolormap.so
-- Up-to-date: /usr/local/include
-- Installing: /usr/local/include/miscellaneous_colormaps.mod
-- Installing: /usr/local/include/matplotlib_colormaps.mod
-- Installing: /usr/local/include/colormap_parameters.mod
-- Installing: /usr/local/include/forcolormap.mod
-- Installing: /usr/local/include/forcolormap_info.mod
-- Installing: /usr/local/include/scientific_colour_maps.mod
-- Installing: /usr/local/lib/cmake/forcolormap/forcolormapTargets.cmake
-- Installing: /usr/local/lib/cmake/forcolormap/forcolormapTargets-release.cmake
-- Installing: /usr/local/lib/cmake/forcolormap/forcolormapConfig.cmake
-- Installing: /usr/local/lib/cmake/forcolormap/forcolormapConfigVersion.cmake
-- Installing: /usr/local/lib/pkgconfig/forcolormap.pc

We can see that the libforimage.so and libforcolormap.so shared libraries are installed.

But we can notice a difference between ForImage and ForColormap, the following line is present only for ForColormap:

-- Set runtime path of "/usr/local/lib/libforcolormap.so.0.8.0" to ""

Files in the libraries directory:

$ ls -thor /usr/local/lib/libfor*
-rw-r--r-- 1 root 145K janv. 11 14:12 /usr/local/lib/libforimage.a
-rw-r--r-- 1 root 116K janv. 24 11:08 /usr/local/lib/libforimage.so.0.3.0
-rw-r--r-- 1 root 509K janv. 24 11:08 /usr/local/lib/libforcolormap.so.0.8.0
lrwxrwxrwx 1 root   20 janv. 24 11:08 /usr/local/lib/libforimage.so.0 -> libforimage.so.0.3.0
lrwxrwxrwx 1 root   16 janv. 24 11:08 /usr/local/lib/libforimage.so -> libforimage.so.0
lrwxrwxrwx 1 root   23 janv. 24 11:08 /usr/local/lib/libforcolormap.so.0 -> libforcolormap.so.0.8.0
lrwxrwxrwx 1 root   19 janv. 24 11:08 /usr/local/lib/libforcolormap.so -> libforcolormap.so.0

We can also see that the system knows the path to ForImage:

$ whereis libforimage.so.0
libforimage.so.0: /usr/local/lib/libforimage.so.0

But if we used the command ldd to print shared object dependencies:

$ ldd ./a.out
        linux-vdso.so.1 (0x00007ffc20faa000)
        libforcolormap.so.0 => /usr/local/lib/libforcolormap.so.0 (0x00007f4a2ef85000)
        libgfortran.so.5 => /lib/x86_64-linux-gnu/libgfortran.so.5 (0x00007f4a2ec00000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4a2eb15000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4a2ef37000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4a2e800000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f4a2f006000)
        libforimage.so.0 => not found

we see that ForImage is not found.

$ pkg-config --cflags --libs forimage forcolormap
-I/usr/local/include -L/usr/local/lib -lforimage -lforcolormap 

In MSYS2/Windows, CMake encounters problems with the ForImage dependency

Ìn MSYS2/Windows 10, CMake 3.28.3 has problems with ForImage:

$ cmake -G "MSYS Makefiles" -D BUILD_SHARED_LIBS=true ..
-- The Fortran compiler identification is GNU 13.2.0
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Check for working Fortran compiler: D:/Logiciels/MSYS2/ucrt64/bin/gfortran.exe - skipped
-- Setting build type to 'Release' as none was specified.
-- Build FORIMAGE tests: OFF
-- Build FORIMAGE examples: OFF
CMake Error at cmake/dependencies/forimage/CMakeLists.txt:15 (add_custom_command):
  No TARGET 'forcolormap' has been created in this directory.


-- Build FORCOLORMAP tests: OFF
-- Build FORCOLORMAP examples: OFF
-- Configuring incomplete, errors occurred!

Note: no problem with fpm.

access='append' is a GNU extension

When compiling with GFortran -std=f2018 options, access='append' is not accepted.

$ fpm build --flag "-Ofast -march=native -mtune=native -Wall -Wextra -std=f2018"
...
colormaps_info.f90                     failed.
[ 39%] Compiling...
build/dependencies/forcolormap/src/colormaps_info.f90:319:61:

  319 |             open(newunit=nunit, file=trim(file_name), access='append', status='unknown', action='write')
	  |                                                             1
Error: GNU Extension: ACCESS specifier in OPEN statement at (1) has value ‘APPEND’
build/dependencies/forcolormap/src/colormaps_info.f90:338:61:

  338 |             open(newunit=nunit, file=trim(file_name), access='append', status='unknown', action='write')
	  |                                                             1
Error: GNU Extension: ACCESS specifier in OPEN statement at (1) has value ‘APPEND’
...

Indeed, access should be sequential (default), direct or stream. We should use instead the specifier position='append':

open(newunit=nunit, file=trim(file_name), position='append', status='unknown', action='write')

Suggestion: demo

  • Move all test results to the demo folder to keep the main directory clean.
  • Add the write_ppm_file_colorbar subroutine as a type-bound procedure for the Colormap type. Move write_ppm_file_test to demo.f90 since it's a specific subroutine for the demo.

Unused dummy argument ‘check_zmax’

$ fpm build --verbose
...
[ 41%]             colormap_class.f90
 + gfortran -c ././src/colormap_class.f90   -Wall -Wextra -fPIC -fmax-errors=1 -g -fcheck=bounds -fcheck=array-temps -fbacktrace -fcoarray=single -fimplicit-none -Werror=implicit-interface -ffree-form -J build/gfortran_87E2AE0597D39913 -Ibuild/gfortran_87E2AE0597D39913 -o build/gfortran_87E2AE0597D39913/forcolormap/src_colormap_class.f90.o
././src/colormap_class.f90:1109:65:

 1109 |     pure subroutine check(self,check_name, check_zmin, check_zmax, check_levels)
      |                                                                 1
Warning: Unused dummy argument ‘check_zmax’ at (1) [-Wunused-dummy-argument]
[ 44%]             colormap_class.f90  done.
...
        if (present(check_zmin)) then
            if (check_zmin) then

                ! Check validity of zmin and zmax
                if (self%zmin > self%zmax) status(2) = .false.

                ! Fix zmin and zmax if they are not valid
                if (status(2) .eqv. .false.) then
                    temp = self%zmin
                    self%zmin = self%zmax
                    self%zmax = temp
                end if

            end if
        end if

Should we write something like:

        if (present(check_zmin).or.present(check_zmax)) then
            if (check_zmin.or.check_zmax) then

?

Or replace check_zmin and check_zmax by only one optional argument check_bounds? Because if both are present but check_zmin=.true. and check_zmax=.false., what would it mean?

Add Support lut File Format

I noticed the idea of lut files in the TODO list. I have added support for the lut file format in ForImage. Examples of this functionality can be found at the following links: example15 and example16.

If you believe ForColormap is the appropriate place for this functionality, I can move it there.

Now, it is possible to import and export lut files. We can implement support for importing and exporting lut files in ForColormap by adding two type-bound procedures. What's your opinion?

Error in the pkg-config libdir variable (CMake)

After installing ForColormap in Ubuntu 23.04:

$ pkg-config --cflags --libs forcolormap
-I/usr/local/include -L/usr/local/usr/local/lib -lforcolormap 

The path /usr/local/usr/local/lib should be /usr/local/lib instead.

In the install/template.pc file we have:

prefix = @CMAKE_INSTALL_PREFIX@
libdir = ${prefix}/@CMAKE_INSTALL_FULL_LIBDIR@
includedir = ${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
...
Libs: -L${libdir} -l@PROJECT_NAME@
Cflags: -I${includedir}

We can write libdir = ${prefix}/@CMAKE_INSTALL_LIBDIR@
but we could also delete the prefix variable and use the CMAKE_INSTALL_FULL_ version for both directories.
What solution should we use? @jchristopherson

https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html

Ideia for the logo

The figure resembling a marble (canica in spanish). The colors inside the marble can be chosen from the Scientific color maps. If there is any interest, I can convert it to an SVG file. (fcm 😉 fpm )

fcm_ideia

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.