@@ -158,8 +158,8 @@ function common_plot_xyz(cmd0::String, arg1, caller::String, first::Bool, is3D::
158
158
cmd = replace (cmd, opt_J => " -JX" * split (DEF_FIG_SIZE, ' /' )[1 ] * " /0" ) # If projected, it's a axis equal for sure
159
159
end
160
160
if (is3D && isempty (opt_JZ) && length (collect (eachmatch (r" /" , opt_R))) == 5 )
161
- if O opt_JZ = (CTRL. pocket_J[3 ] != " " ) ? CTRL. pocket_J[3 ][1 : 4 ] : " -JZ"
162
- else opt_JZ = CTRL. pocket_J[3 ] = (is_gridtri) ? " -JZ5c" : " -JZ6c" # Arbitrary and not satisfactory for all cases.
161
+ if (O) opt_JZ = (CTRL. pocket_J[3 ] != " " ) ? CTRL. pocket_J[3 ][1 : 4 ] : " -JZ"
162
+ else opt_JZ = CTRL. pocket_J[3 ] = (is_gridtri) ? " -JZ5c" : " -JZ6c" # Arbitrary and not satisfactory for all cases.
163
163
end
164
164
cmd *= opt_JZ # Default -JZ
165
165
end
@@ -339,6 +339,7 @@ function common_plot_xyz(cmd0::String, arg1, caller::String, first::Bool, is3D::
339
339
end
340
340
341
341
_cmd = fish_bg (d, _cmd) # See if we have a "pre-command"
342
+ _cmd = fish_pagebg (d, _cmd, autoJZ= (is3D && axis_equal)) # Last arg tells if JZ was computed automatically
342
343
343
344
isa (arg1, GDtype) && plt_txt_attrib! (arg1, d, _cmd) # Function barrier to plot TEXT attributed labels (in case)
344
345
@@ -584,23 +585,111 @@ function with_xyvar(d::Dict, arg1::GMTdataset, no_x::Bool=false)::Union{GMTdatas
584
585
end
585
586
586
587
# ---------------------------------------------------------------------------------------------------
588
+ """
589
+ cmd = fish_pagebg(d::Dict, cmd::Vector{String}) -> Vector{String}
590
+
591
+ Check if using a background image to replace the page color.
592
+
593
+ This function checks for the presence of a `pagebg` option that sets the page background image.
594
+ Note that this different from the `background` or `bg` option that sets the plotting canvas background color.
595
+
596
+ - `pagebg`: a NamedTuple with the following members
597
+ - `image`: the image name or a GMTimage/GMTgrid object
598
+ - `width`: the width of the background image in percentage of the page width (default: 0.8)
599
+ - `offset`: the offset of the background image in percentage of the page width (default: (0.0,0.0))
600
+ If only one value is provided it is used for the X offset only.
601
+
602
+ OR
603
+
604
+ - `pagebg`: an image file name or a GMTimage/GMTgrid object
605
+ In this case the above defaults for the _width_ and _offset_ parameters are used
606
+ """
607
+ function fish_pagebg (d:: Dict , cmd:: Vector{String} ; autoJZ:: Bool = true ):: Vector{String}
608
+ ((val = find_in_dict (d, [:pagebg ])[1 ]) === nothing ) && return cmd
609
+ width:: Float64 = 0.8 ; off_X:: Float64 = 0.0 ; off_Y:: Float64 = 0.0 # The off's are offsets from the center
610
+ if isa (val, NamedTuple)
611
+ ! haskey (val, :image ) && error (" pagebg: NamedTuple must contain the member 'image'" )
612
+ fname = helper_fish_bgs (val[:image ]) # Get the image name or set it under the hood if input is a GMTimage
613
+ haskey (val, :width ) && (width = val[:width ])
614
+ (width <= 0 || width > 1 ) && error (" pagebg: width is a normalized value, must be between 0 and 1" )
615
+ if (haskey (val, :offset ) || haskey (val, :off ))
616
+ off = (haskey (val, :offset )) ? val[:offset ] : val[:off ]
617
+ isa (off, Real) ? (off_X = off) : length (off) == 2 ? (off_X = off[1 ]; off_Y = off[2 ]) :
618
+ error (" pagebg: offset must be a Real or a two elements Array/Tuple" )
619
+ end
620
+ else # Here, val is just the file name or a GMTimage
621
+ fname = helper_fish_bgs (val) # Get the image name or set it under the hood if input is a GMTimage
622
+ end
623
+
624
+ if contains (CTRL. pocket_J[2 ], " /" ) Wt, Ht = split (CTRL. pocket_J[2 ], ' /' )
625
+ else Wt = CTRL. pocket_J[2 ]; Ht = " /0"
626
+ end
627
+ isletter (Wt[end ]) ? (cw= Wt[end ]; Wt = Wt[1 : end - 1 ]) : (cw = ' c' )
628
+ isletter (Ht[end ]) ? (ch= Ht[end ]; Ht = Ht[1 : end - 1 ]) : (ch = ' c' ); (Ht == " 0" ) && (Ht = " /0" )
629
+ W = parse (Float64, Wt)
630
+ if (Ht != " " && Ht != " /0" ) # User gave an explicit height
631
+ H = parse (Float64, Ht) * width # r = H / W; H = r * width * W; = H * width
632
+ Ht = @sprintf (" /%.4g%c" , H, ch)
633
+ end
634
+
635
+ off_XY = @sprintf (" -X%.4g%c" , off_X + (1 - width)/ 2 * W, cw)
636
+ (off_Y != 0.0 ) && (off_XY *= @sprintf (" -Y%.4g%c" , off_Y, cw))
637
+ opt_J = scan_opt (cmd[1 ], " -J" , true )
638
+ new_J = string (opt_J[1 : 4 ], width * W, cw, Ht)
639
+
640
+ # If the 'bg' option is also set it sits in cmd[1] and then we want to modify cmd[2].
641
+ ind_cmd = startswith (cmd[1 ], " grdimage" ) ? 2 : 1 # The presence of 'grdimage' says that 'bg' was used.
642
+
643
+ cmd[ind_cmd] = replace (cmd[ind_cmd], opt_J => new_J * off_XY)
644
+ if (autoJZ && (opt_JZ = scan_opt (cmd[1 ], " -JZ" , true )) != " " ) # Only do this for JZ that was set automatically
645
+ z = parse (Float64, isletter (opt_JZ[end ]) ? opt_JZ[5 : end - 1 ] : opt_JZ[5 : end ]) * width
646
+ CTRL. pocket_J[3 ] = @sprintf (" -JZ%.4g%c" , z, cw)
647
+ cmd[ind_cmd] = replace (cmd[ind_cmd], opt_JZ => CTRL. pocket_J[3 ])
648
+ end
649
+ proggy = IamModern[1 ] ? " image " : " psimage "
650
+ [proggy * fname * CTRL. pocket_J[1 ] * CTRL. pocket_R[1 ] * " -Dx0/0+w" * Wt* cw, cmd... ]
651
+ end
652
+
653
+ # ---------------------------------------------------------------------------------------------------
654
+ """
655
+ cmd = fish_bg(d::Dict, cmd::Vector{String}) -> Vector{String}
656
+
657
+ Check if a background image in a plot area is requested.
658
+
659
+ Check if the background image is used and if yes insert a first command that calls grdimage to fill
660
+ the canvas with that bg image. The BG image can be a file name, the name of one of the pre-defined
661
+ functions, or a GMTgrid/GMTimage object. By default we use a trimmed gray scale (between ~64 & 240)
662
+ but if user wants to control the colormap then the option's argument can be a tuple where the second
663
+ element is cpt name or a GMTcpt obj.
664
+
665
+ ### Example
666
+
667
+ ```julia
668
+ plot(rand(8,2), bg=(:somb, :turbo), show=1)
669
+
670
+ # To revert the sense of the color progression prefix the cpt name or of the pre-def function with a '-'
671
+
672
+ plot(rand(8,2), bg="-circ", show=1)
673
+ ```
674
+ """
587
675
function fish_bg (d:: Dict , cmd:: Vector{String} ):: Vector{String}
588
- # Check if the background image is used and if yes insert a first command that calls grdimage to fill
589
- # the canvas with that bg image. The BG image can be a file name, the name of one of the pre-defined
590
- # functions, or a GMTgrid/GMTimage object.
591
- # By default we use a trimmed gray scale (between ~64 & 240) but if user wants to control the colormap
592
- # then the option's argument can be a tuple where the second element is cpt name or a GMTcpt obj.
593
- # Ex: plot(rand(8,2), bg=(:somb, :turbo), show=1)
594
- # To revert the sense of the color progression prefix the cpt name or of the pre-def function with a '-'
595
- # Ex: plot(rand(8,2), bg="-circ", show=1)
596
676
((val = find_in_dict (d, [:bg :background ])[1 ]) === nothing ) && return cmd
677
+ fname = helper_fish_bgs (val)
678
+
679
+ opt_p = scan_opt (cmd[1 ], " -p" , true ); opt_c = scan_opt (cmd[1 ], " -c" , true )
680
+ opt_D = (IamModern[1 ]) ? " -Dr " : " -D " # Found this difference by experience. It might break in future GMTs
681
+ [" grdimage" * opt_D * fname * CTRL. pocket_J[1 ] * opt_p * opt_c, cmd... ]
682
+ end
683
+
684
+ function helper_fish_bgs (val):: String
597
685
arg1, arg2 = isa (val, Tuple) ? val[:] : (val, nothing )
598
- (arg2 != = nothing && (! isa (arg2, GMTcpt) && ! isa (arg2, StrSymb))) && error (" When a Tuple is used in argument of the background image option, the second element must be a string or a GMTcpt object." )
686
+ (arg2 != = nothing && (! isa (arg2, GMTcpt) && ! isa (arg2, StrSymb))) &&
687
+ error (" When a Tuple is used in argument of the background image option, the second element must be a string or a GMTcpt object." )
599
688
gotfname, fname:: String , opt_I:: String = false , " " , " "
600
689
if (isa (arg1, StrSymb))
601
- if (splitext (string (arg1):: String )[2 ] != " " ) # Assumed to be an image file name
690
+ if (splitext (string (arg1):: String )[2 ] != " " ) # Assumed to be an image file name
602
691
fname, gotfname = arg1, true
603
- else # A pre-set fun name
692
+ else # A pre-set fun name
604
693
fun:: String = string (arg1)
605
694
(fun[1 ] == ' -' ) && (fun = fun[2 : end ]; opt_I = " -I" )
606
695
I:: GMTimage = imagesc (mat2grid (fun))
@@ -617,12 +706,8 @@ function fish_bg(d::Dict, cmd::Vector{String})::Vector{String}
617
706
image_cpt! (I, C)
618
707
CTRL. pocket_call[3 ] = I # This signals finish_PS_module() to run _cmd first
619
708
end
620
-
621
- opt_p = scan_opt (cmd[1 ], " -p" ); (opt_p != " " ) && (opt_p = " -p" * opt_p)
622
- opt_c = scan_opt (cmd[1 ], " -c" ); (opt_c != " " ) && (opt_c = " -c" * opt_c)
623
- # (opt_c == "" && contains(cmd[1], " -c ")) && (opt_c = " -c") # Because of a scan_opt() desing error (but causes error)
624
- opt_D = (IamModern[1 ]) ? " -Dr " : " -D " # Found this difference by experience. It might break in future GMTs
625
- [" grdimage" * opt_D * fname * CTRL. pocket_J[1 ] * opt_p * opt_c, cmd... ]
709
+ FIG_MARGIN[1 ] = 0
710
+ return fname
626
711
end
627
712
628
713
# ---------------------------------------------------------------------------------------------------
0 commit comments