Skip to content

Commit b76c03f

Browse files
authored
fix: add robust RGB validation to prevent PDF color crashes (#318)
## Summary - Adds comprehensive RGB value validation to pdf_write_color subroutine - Handles NaN, infinity, and out-of-range color values gracefully - Fixes critical crash in colored_contours example that was blocking GitHub Pages deployment ## Changes - Added IEEE_ARITHMETIC intrinsic module for NaN/infinity detection - Implemented validation logic to check each RGB component - Clamps values to valid [0.0, 1.0] range - Defaults invalid values to black (0.0) for safety ## Testing - ✅ colored_contours example now runs successfully - ✅ All 5 PDF files generated correctly with valid content - ✅ All existing tests continue to pass - ✅ No regressions introduced ## Impact This fix restores the GitHub Pages visual showcase functionality by allowing the colored_contours example to complete successfully. The validation ensures robust handling of edge cases in color mapping algorithms. Fixes #317
1 parent ffde8fa commit b76c03f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

BACKLOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
**🚨 CRITICAL: User-visible PNG/PDF rendering completely broken**
66

77
**Infrastructure & Documentation Issues (Lower Priority)**
8+
- [ ] #319: Refactor - investigate source of invalid RGB values in contour color mapping
9+
- [ ] #320: Feature - add debug logging for invalid color value corrections
10+
- [ ] #321: Refactor - apply consistent validation pattern to other PDF write functions
811

912
## DOING (Current Work)
10-
- [ ] Fix GitHub Pages deployment failure - colored_contours example crashes with "End of record" error at fortplot_pdf_drawing.f90:69 in pdf_write_color subroutine
13+
- [ ] #317: Fix GitHub Pages deployment failure - colored_contours PDF runtime crash in pdf_write_color
1114

1215
## BLOCKED (Infrastructure Issues)
1316

src/fortplot_pdf_drawing.f90

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module fortplot_pdf_drawing
77
!! Author: fortplot contributors
88

99
use, intrinsic :: iso_fortran_env, only: wp => real64
10+
use, intrinsic :: ieee_arithmetic, only: ieee_is_nan, ieee_is_finite
1011
use fortplot_vector, only: vector_stream_writer, vector_graphics_state
1112
use fortplot_markers, only: get_marker_size, MARKER_CIRCLE, MARKER_SQUARE, MARKER_DIAMOND, MARKER_CROSS
1213
implicit none
@@ -62,11 +63,37 @@ subroutine pdf_write_stroke(this)
6263
end subroutine pdf_write_stroke
6364

6465
subroutine pdf_write_color(this, r, g, b)
65-
!! Write PDF color command
66+
!! Write PDF color command with robust validation
67+
!! Validates and clamps RGB values to [0.0, 1.0] range
68+
!! Handles NaN, infinity, and out-of-range values gracefully
6669
class(pdf_stream_writer), intent(inout) :: this
6770
real(wp), intent(in) :: r, g, b
71+
real(wp) :: r_safe, g_safe, b_safe
6872
character(len=64) :: cmd
69-
write(cmd, '(F0.3,1X,F0.3,1X,F0.3," RG")') r, g, b
73+
74+
! Validate and clamp R component
75+
if (ieee_is_nan(r) .or. .not. ieee_is_finite(r)) then
76+
r_safe = 0.0_wp ! Default to black for invalid values
77+
else
78+
r_safe = max(0.0_wp, min(1.0_wp, r)) ! Clamp to [0, 1]
79+
end if
80+
81+
! Validate and clamp G component
82+
if (ieee_is_nan(g) .or. .not. ieee_is_finite(g)) then
83+
g_safe = 0.0_wp ! Default to black for invalid values
84+
else
85+
g_safe = max(0.0_wp, min(1.0_wp, g)) ! Clamp to [0, 1]
86+
end if
87+
88+
! Validate and clamp B component
89+
if (ieee_is_nan(b) .or. .not. ieee_is_finite(b)) then
90+
b_safe = 0.0_wp ! Default to black for invalid values
91+
else
92+
b_safe = max(0.0_wp, min(1.0_wp, b)) ! Clamp to [0, 1]
93+
end if
94+
95+
! Write validated color values
96+
write(cmd, '(F0.3,1X,F0.3,1X,F0.3," RG")') r_safe, g_safe, b_safe
7097
call this%add_to_stream(trim(cmd))
7198
end subroutine pdf_write_color
7299

0 commit comments

Comments
 (0)