Skip to content

Commit

Permalink
Merge branch 'main' into ali-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
gha3mi committed Feb 23, 2024
2 parents cde5721 + 690afa9 commit f83a2b5
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 132 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.ppm

# Backups
*.bak
*.backup
.gitignore~

Expand Down Expand Up @@ -43,4 +44,4 @@

# Directories
build/
doc/
doc/
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ All notable changes to the gtk-fortran project are documented in this file. The
* A `reverse` boolean option was added to the methods `set`, `create`, and `load` to reverse a colormap.
* An `example/demo_reverse.f90` example.
* The "magma", "inferno","plasma", "viridis" matplotlib colormaps in a `matplotlib_colormaps` module.
* A `src/forcolormap_utils.f90` module with the subroutine `test_colormap()`.
* A `colormap_parameters` module.
* A `miscellaneous_colormaps` module.
* A `colorbar()` type-bound procedure to write a PPM file with the colorbar.

### Changed
* Subroutine `test_colormap()`: the `encoding` argument is now optional (binary by default).
* The colormap `inverted_rainbow` was renamed `inv_rainbow`.
* `TODO.md` was refactored and renamed `ROADMAP.md`.
* Code refactoring.
* Moved `test` subroutine to demo and example1.
* Renamed `get_current()` to `get_name()`.
* For writing PPM files, the project [ForImage](https:/gha3mi/forimage) by @gha3mi is now used as a fpm dependency. The related example must now be launched with `fpm run --example example1` and the ForColormap demo by `fpm run --example demo`.
* `src/colormap_class.f90`: `private` statement is now the default.
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The main goal of v0.9 is to offer a usable library, sufficiently friendly, with
- [x] Add Interpolation functions.
- [x] Lagrange
- [x] Bezier
- [ ] Add a `colormap_utils.f90` module where the subroutine `test_colormap()` will be moved.
- [x] Add a `src/forcolormap_utils.f90` module where the subroutine `test_colormap()` will be moved.

### Examples

Expand Down
53 changes: 9 additions & 44 deletions example/demo.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
! The MIT License (MIT)
!
! Copyright (c) 2023 Vincent Magnin
! Copyright (c) 2023-2024 Vincent Magnin
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,11 +21,12 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by vmagnin: 2023-09-26
! Last modification: gha3mi 2024-01-28, vmagnin 2024-02-15
! Last modification: gha3mi 2024-01-28, vmagnin 2024-02-22
!-------------------------------------------------------------------------------

program demo
use forcolormap, only: Colormap, colormaps_list, wp
use forcolormap_utils, only: test_colormap
implicit none

integer :: i
Expand All @@ -43,66 +44,30 @@ program demo
255, 255, 255 ], &
shape(my_colormap), order = [2, 1] )

! Let's create PPM files for each built-in colormap.
! The built-in z=f(x,y) test function is in the [0, 2] range:
!> We create PPM files (binary encoded by default) for each built-in colormap.
!> The built-in z=f(x,y) test function is in the [0, 2] range:
do i = 1, size(colormaps_list)
call cmap%set(trim(colormaps_list(i)), 0.0_wp, 2.0_wp)
call cmap%colorbar(trim(colormaps_list(i))//'_colorbar')
call test_colormap(cmap, trim(colormaps_list(i))//'_test', encoding='binary')
call test_colormap(cmap, trim(colormaps_list(i))//'_test')
print '("Colormap ", A30, " has ", I0, " levels")', trim(cmap%get_name()), cmap%get_levels()
end do

! Cubehelix can also accept other parameters (varargs array):
call cmap%set("cubehelix", 0.0_wp, 2.0_wp, 1024, [0.5_wp, -1.0_wp, 1.0_wp, 1.0_wp])
! We change the name for the output test files:
call cmap%colorbar('cubehelix_customized_colorbar')
call test_colormap(cmap, 'cubehelix_customized_test', encoding='binary')
call test_colormap(cmap, 'cubehelix_customized_test')

! You can create your own colormap defined in an array:
call custom_cmap%create('discrete', 0.0_wp, 2.0_wp, my_colormap)
call custom_cmap%colorbar('discrete_colorbar')
call test_colormap(custom_cmap, 'discrete_test', encoding='binary')
call test_colormap(custom_cmap, 'discrete_test')

! Or you can download it from a .txt file:
call custom_cmap%load("test_map_to_load.txt", 0.0_wp, 2.0_wp)
call custom_cmap%colorbar('a_loaded_colorbar')
call test_colormap(custom_cmap, 'a_loaded_colormap_test', encoding='binary')
call test_colormap(custom_cmap, 'a_loaded_colormap_test')
call custom_cmap%print()

contains

subroutine test_colormap(self, filename, encoding)
use forimage, only: format_pnm
type(Colormap), intent(inout) :: self
character(*), intent(in) :: filename
integer :: k, j ! Pixbuffer coordinates
integer, parameter :: pixwidth = 600
integer, parameter :: pixheight = 600
integer, dimension(pixheight,pixwidth*3) :: rgb_image
integer :: red, green, blue
real(wp) :: z
type(format_pnm) :: ppm
character(*), intent(in) :: encoding

do k = 0, pixwidth-1
do j = 0, pixheight-1
! Computing a z=f(x,y) function:
z = 1.0_wp + sin(k*j/10000.0_wp) * cos(j/100.0_wp)
! The corresponding RGB values in our colormap:
call self%compute_RGB(z, red, green, blue)
rgb_image(pixheight-j, 3*(k+1)-2) = red
rgb_image(pixheight-j, 3*(k+1)-1) = green
rgb_image(pixheight-j, 3*(k+1)) = blue
end do
end do

call ppm%set_pnm(encoding = encoding,&
file_format = 'ppm',&
width = pixwidth,&
height = pixheight,&
max_color = 255,&
comment = 'comment',&
pixels = rgb_image)
call ppm%export_pnm(filename)
end subroutine test_colormap
end program demo
53 changes: 9 additions & 44 deletions example/demo_reverse.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
! The MIT License (MIT)
!
! Copyright (c) 2023 Vincent Magnin
! Copyright (c) 2023-2024 Vincent Magnin
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,11 +21,12 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by vmagnin: 2023-09-26
! Last modification: gha3mi 2023-11-01, vmagnin 2024-02-15
! Last modification: gha3mi 2023-11-01, vmagnin 2024-02-22
!-------------------------------------------------------------------------------

program demo_reverse
use forcolormap, only: Colormap, colormaps_list, wp
use forcolormap_utils, only: test_colormap
implicit none

integer :: i
Expand All @@ -42,66 +43,30 @@ program demo_reverse
255, 255, 255 ], &
shape(my_colormap), order = [2, 1] )

! Let's create PPM files for each built-in colormap.
! The built-in z=f(x,y) test function is in the [0, 2] range:
!> We create PPM files (binary encoded by default) for each built-in colormap.
!> The built-in z=f(x,y) test function is in the [0, 2] range:
do i = 1, size(colormaps_list)
call cmap%set(trim(colormaps_list(i)), 0.0_wp, 2.0_wp, reverse=.true.)
call cmap%colorbar(trim(colormaps_list(i))//'_reverse_colorbar')
call test_colormap(cmap, trim(colormaps_list(i))//'_reverse_test', encoding='binary')
call test_colormap(cmap, trim(colormaps_list(i))//'_reverse_test')
print '("Colormap ", A30, " has ", I0, " levels")', trim(cmap%get_name()), cmap%get_levels()
end do

! Cubehelix can also accept other parameters (varargs array):
call cmap%set("cubehelix", 0.0_wp, 2.0_wp, 1024, [0.5_wp, -1.0_wp, 1.0_wp, 1.0_wp], reverse=.true.)
! We change the name for the output test files:
call cmap%colorbar('cubehelix_customized_reverse_colorbar')
call test_colormap(cmap, 'cubehelix_customized_reverse_test', encoding='binary')
call test_colormap(cmap, 'cubehelix_customized_reverse_test')

! You can create your own colormap defined in an array:
call custom_cmap%create('discrete', 0.0_wp, 2.0_wp, my_colormap, reverse=.true.)
call custom_cmap%colorbar('discrete_reverse_colorbar')
call test_colormap(custom_cmap, 'discrete_reverse_test', encoding='binary')
call test_colormap(custom_cmap, 'discrete_reverse_test')

! Or you can download it from a .txt file:
call custom_cmap%load("test_map_to_load.txt", 0.0_wp, 2.0_wp, reverse=.true.)
call custom_cmap%colorbar('a_loaded_reverse_colorbar')
call test_colormap(custom_cmap, 'a_loaded_reverse_colormap_test', encoding='binary')
call test_colormap(custom_cmap, 'a_loaded_reverse_colormap_test')
call custom_cmap%print()

contains

subroutine test_colormap(self, filename, encoding)
use forimage, only: format_pnm
type(Colormap), intent(inout) :: self
character(*), intent(in) :: filename
integer :: k, j ! Pixbuffer coordinates
integer, parameter :: pixwidth = 600
integer, parameter :: pixheight = 600
integer, dimension(pixheight,pixwidth*3) :: rgb_image
integer :: red, green, blue
real(wp) :: z
type(format_pnm) :: ppm
character(*), intent(in) :: encoding

do k = 0, pixwidth-1
do j = 0, pixheight-1
! Computing a z=f(x,y) function:
z = 1.0_wp + sin(k*j/10000.0_wp) * cos(j/100.0_wp)
! The corresponding RGB values in our colormap:
call self%compute_RGB(z, red, green, blue)
rgb_image(pixheight-j, 3*(k+1)-2) = red
rgb_image(pixheight-j, 3*(k+1)-1) = green
rgb_image(pixheight-j, 3*(k+1)) = blue
end do
end do

call ppm%set_pnm(encoding = encoding,&
file_format = 'ppm',&
width = pixwidth,&
height = pixheight,&
max_color = 255,&
comment = 'comment',&
pixels = rgb_image)
call ppm%export_pnm(filename)
end subroutine test_colormap
end program demo_reverse
43 changes: 3 additions & 40 deletions example/example1.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by gha3mi: 2023-10-26
! Last modification: gha3mi 2024-01-06
! Last modification: gha3mi 2024-01-06, vmagnin 2024-02-21
!-------------------------------------------------------------------------------

! This example demonstrates how ForImage can be used to import/export PPM files.
!> This example demonstrates how ForImage can be used to import/export PPM files.
program example1
use forcolormap
use forcolormap_utils, only: test_colormap
use forimage
implicit none

Expand Down Expand Up @@ -55,42 +56,4 @@ program example1
call ex1_colormap%finalize()
call ex1_colorbar%finalize()

contains

subroutine test_colormap(self, filename, encoding)
use forimage, only: format_pnm
type(Colormap), intent(inout) :: self
character(*), intent(in) :: filename
integer :: k, j ! Pixbuffer coordinates
integer, parameter :: pixwidth = 600
integer, parameter :: pixheight = 600
integer, dimension(:,:), allocatable :: rgb_image
integer :: red, green, blue
real(wp) :: z
type(format_pnm) :: ppm
character(*), intent(in) :: encoding

allocate(rgb_image(pixheight,pixwidth*3))

do k = 0, pixwidth-1
do j = 0, pixheight-1
! Computing a z=f(x,y) function:
z = 1.0_wp + sin(k*j/10000.0_wp) * cos(j/100.0_wp)
! The corresponding RGB values in our colormap:
call self%compute_RGB(z, red, green, blue)
rgb_image(pixheight-j, 3*(k+1)-2) = red
rgb_image(pixheight-j, 3*(k+1)-1) = green
rgb_image(pixheight-j, 3*(k+1)) = blue
end do
end do

call ppm%set_pnm(encoding = encoding,&
file_format = 'ppm',&
width = pixwidth,&
height = pixheight,&
max_color = 255,&
comment = 'comment',&
pixels = rgb_image)
call ppm%export_pnm(filename)
end subroutine test_colormap
end program example1
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ set(dir ${CMAKE_CURRENT_SOURCE_DIR})

set(FORCOLORMAP_SOURCES
${dir}/colormap_class.f90
${dir}/forcolormap_utils.f90
${dir}/colormap_parameters.f90
${dir}/colormaps_info.f90
${dir}/matplotlib_colormaps.f90
${dir}/miscellaneous_colormaps.f90
${dir}/scientific_colour_maps.f90
)
set(FORCOLORMAP_SOURCES ${FORCOLORMAP_SOURCES} PARENT_SCOPE)
set(FORCOLORMAP_SOURCES ${FORCOLORMAP_SOURCES} PARENT_SCOPE)
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* `colormap_class.f90`: the Colormap class and the `colormaps_list`.
* `colormap_parameters.f90`: a few parameters used in several modules of the library.
* `forcolormap_utils.f90`: contains miscellaneous procedures and functions.
* `colormaps_info.f90`: the Colormaps_info class offers information about each colormap.
* The modules containing the different collections of colormaps:
* `scientific_colour_maps.f90`
Expand Down
86 changes: 86 additions & 0 deletions src/forcolormap_utils.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
! The MIT License (MIT)
!
! Copyright (c) 2024 vmagnin, gha3mi
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by vmagnin & gha3mi: 2024-02-21
! Last modification: vmagnin 2024-02-22
!-------------------------------------------------------------------------------

!> This module contains miscellaneous procedures and functions.
module forcolormap_utils
use forcolormap, only: Colormap, wp

implicit none

private

public :: test_colormap

contains

!> This procedure computes a default z=f(x,y) function and plot it
!> in a .ppm file using the specified colormap. That function is defined
!> in the [0, 2] range.
subroutine test_colormap(self, filename, encoding)
use forimage, only: format_pnm
type(Colormap), intent(inout) :: self
character(*), intent(in) :: filename
integer :: k, j ! Pixbuffer coordinates
integer, parameter :: pixwidth = 600
integer, parameter :: pixheight = 600
integer, dimension(:,:), allocatable :: rgb_image
integer :: red, green, blue
real(wp) :: z
type(format_pnm) :: ppm
character(*), intent(in), optional :: encoding !> Default is binary

allocate(rgb_image(pixheight,pixwidth*3))

do k = 0, pixwidth-1
do j = 0, pixheight-1
! Computing a z=f(x,y) function:
z = 1.0_wp + sin(k*j/10000.0_wp) * cos(j/100.0_wp)
! The corresponding RGB values in our colormap:
call self%compute_RGB(z, red, green, blue)
rgb_image(pixheight-j, 3*(k+1)-2) = red
rgb_image(pixheight-j, 3*(k+1)-1) = green
rgb_image(pixheight-j, 3*(k+1)) = blue
end do
end do

if (present(encoding)) then
call ppm%set_format(encoding)
else
call ppm%set_format('binary')
end if

call ppm%set_pnm(encoding = ppm%get_format(),&
file_format = 'ppm',&
width = pixwidth,&
height = pixheight,&
max_color = 255,&
comment = 'Test generated by ForColormap',&
pixels = rgb_image)
call ppm%export_pnm(filename)

end subroutine test_colormap

end module forcolormap_utils

0 comments on commit f83a2b5

Please sign in to comment.