Skip to content

Commit

Permalink
Merge branch 'main' into lagrange
Browse files Browse the repository at this point in the history
  • Loading branch information
gha3mi committed Feb 16, 2024
2 parents a96eb9f + a262a43 commit b185dfc
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 51 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to the gtk-fortran project are documented in this file. The
## [forcolormap dev]

### Added
* A `shift()` method to apply a circular shift to a colormap (left is +, right is -).
* `example/modify.f90` demonstrates how you can modify a colormap with methods like shift(), in concrete cases.
* A private `check()` method in the Colormap class. It checks the validity of the colormap and its parameters, prints warnings, and fixes problems if necessary.
* A `colormaps_list/ForColormap.pdf` manual listing all the available colormaps.
* A function `bezier(colors, levels)` to create a colormap from continuous Bezier interpolation of control colors, in `src/colormap_class.f90`. The corresponding demo was added in `example/demo.f90`.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ In the `example` directory, you will find these commented demos:
* `example1.f90` demonstrates how ForImage can be used to import/export PPM files.
* `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).

Expand Down
5 changes: 3 additions & 2 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ The main goal of v0.9 is to offer a usable library, sufficiently friendly, with
- [ ] Add Interpolation functions.
- [ ] Lagrange
- [x] Bezier
- [ ] Add a `Colormap_test` class where the subroutine `test_colormap()` will be moved.

### Examples

- [ ] Create a `modify` example:
- [ ] For the `shift()` method.
- [x] Create a `modify` example:
- [x] For the `shift()` method.

### Documentation

Expand Down
4 changes: 4 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ target_link_libraries(extract ${PROJECT_NAME})
# INFO
add_executable(info info.f90)
target_link_libraries(info ${PROJECT_NAME})

# MODIFY
add_executable(modify modify.f90)
target_link_libraries(modify ${PROJECT_NAME})
1 change: 1 addition & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ They can be launched with the command `fpm run --example name_of_the_example` (w
* `example1.f90` demonstrates how ForImage can be used to import/export PPM files.
* `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.
12 changes: 6 additions & 6 deletions example/demo.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by vmagnin: 2023-09-26
! Last modification: gha3mi 2024-02-13
! Last modification: gha3mi 2024-01-28, vmagnin 2024-02-15
!-------------------------------------------------------------------------------

program demo
Expand All @@ -47,25 +47,25 @@ program demo
! 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', encoding='binary')
call cmap%colorbar(trim(colormaps_list(i))//'_colorbar')
call test_colormap(cmap, trim(colormaps_list(i))//'_test', encoding='binary')
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', encoding='binary')
call cmap%colorbar('cubehelix_customized_colorbar')
call test_colormap(cmap, 'cubehelix_customized_test', encoding='binary')

! 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', encoding='binary')
call custom_cmap%colorbar('discrete_colorbar')
call test_colormap(custom_cmap, 'discrete_test', encoding='binary')

! 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', encoding='binary')
call custom_cmap%colorbar('a_loaded_colorbar')
call test_colormap(custom_cmap, 'a_loaded_colormap_test', encoding='binary')
call custom_cmap%print()

Expand All @@ -76,7 +76,7 @@ program demo
colors(2,:) = [0, 255, 0] ! Green
colors(3,:) = [0, 0, 255] ! Blue
call custom_cmap%create('custom_bezier', 0.0_wp, 2.0_wp, bezier(colors, levels=1024)) ! levels is optional, default is 256
call custom_cmap%colorbar('custom_colorbar_bezier', encoding='binary')
call custom_cmap%colorbar('custom_colorbar_bezier')
call test_colormap(custom_cmap, 'custom_test_bezier', encoding='binary')

! You can also create your own colormap using lagrange interpolation:
Expand Down
10 changes: 5 additions & 5 deletions example/demo_reverse.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by vmagnin: 2023-09-26
! Last modification: gha3mi 2023-11-01
! Last modification: gha3mi 2023-11-01, vmagnin 2024-02-15
!-------------------------------------------------------------------------------

program demo_reverse
Expand All @@ -46,25 +46,25 @@ program demo_reverse
! 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', encoding='binary')
call cmap%colorbar(trim(colormaps_list(i))//'_reverse_colorbar')
call test_colormap(cmap, trim(colormaps_list(i))//'_reverse_test', encoding='binary')
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', encoding='binary')
call cmap%colorbar('cubehelix_customized_reverse_colorbar')
call test_colormap(cmap, 'cubehelix_customized_reverse_test', encoding='binary')

! 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', encoding='binary')
call custom_cmap%colorbar('discrete_reverse_colorbar')
call test_colormap(custom_cmap, 'discrete_reverse_test', encoding='binary')

! 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', encoding='binary')
call custom_cmap%colorbar('a_loaded_reverse_colorbar')
call test_colormap(custom_cmap, 'a_loaded_reverse_colormap_test', encoding='binary')
call custom_cmap%print()

Expand Down
8 changes: 4 additions & 4 deletions example/extract.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by gha3mi: 2023-11-07
! Last modification: gha3mi 2023-11-07
! Last modification: gha3mi 2023-11-07, vmagnin 2024-02-15
!-------------------------------------------------------------------------------

! This example demonstrates the process of extracting a specified number of colors.
Expand All @@ -42,7 +42,7 @@ program extract
! The extracted colormap will overwrite the existing colormap type (cmap)
call cmap%extract(10)

! Generate a color bar for the extracted colormap with binary encoding
call cmap%colorbar('fes10_ex_colorbar', encoding='binary')
! Generate a color bar for the extracted colormap
call cmap%colorbar('fes10_ex_colorbar')

end program extract
61 changes: 61 additions & 0 deletions example/modify.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
! The MIT License (MIT)
!
! Copyright (c) 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
! 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: 2024-02-14
! Last modification: vmagnin 2024-02-15
!-------------------------------------------------------------------------------

!> This example shows how you can modify a colormap with methods like shift(),
!> in concrete cases.
program modify
use forcolormap, only: Colormap, wp

implicit none
type(Colormap) :: cmap

!> In the Scientific colour maps collection, all cyclic colormaps have their
!> bright part in the middle. But we can shift the dark part towards the
!> center.
call cmap%set("bamO", 0.0_wp, 2.0_wp)
call cmap%colorbar("bamO")
call cmap%shift(cmap%get_levels() / 2)
call cmap%colorbar("bamO_shifted")
print *, "See the bamO.ppm and bamO_shifted.ppm colorbars"

!> In the Scientific colour maps collection, all categorical colormaps
!> begin with a dark colour, but a shift can be applied to begin with a
!> brighter colour.
call cmap%set("actonS", 0.0_wp, 2.0_wp)
call cmap%colorbar("actonS")
call cmap%shift(+2) !! Two levels towards left
call cmap%colorbar("actonS_shifted")
print *, "See the actonS.ppm and actonS_shifted.ppm colorbars"

!> Starting from a diverging colormap, we can obtain what could be called
!> a diverging multi-sequential colormap.
call cmap%set("bam", 0.0_wp, 2.0_wp)
call cmap%colorbar("bam")
call cmap%shift(cmap%get_levels() / 2)
call cmap%colorbar("bam_shifted")
print *, "See the bam.ppm and bam_shifted.ppm colorbars"

end program modify
5 changes: 5 additions & 0 deletions fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ name = "extract"
source-dir = "example"
main = "extract.f90"

[[example]]
name = "modify"
source-dir = "example"
main = "modify.f90"

[build]
auto-executables = true
auto-tests = true
Expand Down
13 changes: 11 additions & 2 deletions src/colormap_class.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by vmagnin: 2023-09-26
! Last modification: gha3mi 2024-01-28
! Last modification: gha3mi 2024-02-12, vmagnin 2024-02-15
!-------------------------------------------------------------------------------


Expand Down Expand Up @@ -63,6 +63,7 @@ module forcolormap
procedure :: print
procedure :: colorbar => write_ppm_colorbar
procedure, private :: reverse_map
procedure :: shift
procedure :: extract
procedure, private :: check
end type Colormap
Expand Down Expand Up @@ -798,7 +799,7 @@ impure subroutine write_ppm_colorbar(self, filename, width, height, encoding)
call ppm%set_format('binary')
end if

call ppm%set_pnm(encoding = encoding,&
call ppm%set_pnm(encoding = ppm%get_format(),&
file_format = 'ppm',&
width = pixwidth,&
height = pixheight,&
Expand All @@ -820,6 +821,14 @@ pure subroutine reverse_map(self, name)
end if
end subroutine reverse_map

!> Apply a circular shift to the colormap (left is +, right is -)
pure subroutine shift(self, sh)
class(Colormap), intent(inout) :: self
integer, intent(in) :: sh !! The shift

self%map(:,:) = cshift(self%map(:,:), sh)
end subroutine shift

! Normalize the input real array to the range [0, 1]
pure function scale_real_real(real_array,a,b) result(real_scaled_array)
real(wp), dimension(:), intent(in) :: real_array
Expand Down
Loading

0 comments on commit b185dfc

Please sign in to comment.