Skip to content

Commit

Permalink
example/modify.f90 added
Browse files Browse the repository at this point in the history
  • Loading branch information
vmagnin committed Feb 15, 2024
1 parent bf43e41 commit 3fe57f6
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to the gtk-fortran project are documented in this file. The

### 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
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.
97 changes: 97 additions & 0 deletions example/modify.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
! 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", encoding='binary')
call cmap%shift(cmap%get_levels() / 2)
call cmap%colorbar("bamO_shifted", encoding='binary')
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", encoding='binary')
call cmap%shift(+2) !! Two levels towards left
call cmap%colorbar("actonS_shifted", encoding='binary')
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", encoding='binary')
call cmap%shift(cmap%get_levels() / 2)
call cmap%colorbar("bam_shifted", encoding='binary')
print *, "See the bam.ppm and bam_shifted.ppm colorbars"

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 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

0 comments on commit 3fe57f6

Please sign in to comment.