Skip to content

Commit 0c6929d

Browse files
authored
Merge pull request #1503 from GenericMappingTools/ind2rgb-improv
ind2rgb accept a CPT name and internally create the CPT.
2 parents b4fc8bb + aa70d8c commit 0c6929d

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/makecpt.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,13 @@ function parse_opt_range(d::Dict, cmd::String, opt::String="")::Tuple{String, Ve
149149
else @warn("Unkown option \"$_opt\" in range option")
150150
end
151151
end
152+
elseif (n == 2)
153+
out *= "/1"
152154
end
153155
elseif (isa(val, VMr) || isa(val, GMTdataset))
154156
Tvec, out = vec(Float64.(val)), "" # In 6.5, Tvec needs to be a GMTdataset with comment = LIST
157+
elseif (isa(val, Integer)) # Accept an integer as the number of bins
158+
out = "0/$(val)/1"
155159
else
156160
out = arg2str(val) # Everything fits here if given as a string
157161
end

src/utils_types.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ function mat2img(mat::Union{GMTgrid,Matrix{<:AbstractFloat}}; x=Float64[], y=Flo
11851185
else I.colormap, I.labels, I.n_colors = zeros(Int32,3), String[], 0 # Do not inherit this from GI
11861186
end
11871187
elseif (isa(mat, GMTgrid))
1188+
(isa(cmap, Symbol) || isa(cmap, String)) && (cmap = grd2cpt(mat, E=256, C=cmap))
11881189
I = mat2img(img; x=mat.x, y=mat.y, hdr=hdr, proj4=mat.proj4, wkt=mat.wkt, cmap=cmap, is_transposed=is_transp, kw...)
11891190
else
11901191
I = mat2img(img; x=x, y=y, hdr=hdr, proj4=proj4, wkt=wkt, cmap=cmap, is_transposed=is_transp, kw...)
@@ -1194,7 +1195,7 @@ function mat2img(mat::Union{GMTgrid,Matrix{<:AbstractFloat}}; x=Float64[], y=Flo
11941195
end
11951196

11961197
"""
1197-
imagesc(mat; x=, y=, hdr=, proj4=, wkt=, GI=, clim=, cmap=, kw...)
1198+
I = imagesc(mat; x=, y=, hdr=, proj4=, wkt=, GI=, clim=, cmap=, kw...)
11981199
11991200
imagesc takes a Float matrix or a GMTgrid type and scales it (by default) to the [0, 255] interval.
12001201
In the process it creates a GMTimage type. Those types can account for coordinates and projection
@@ -1805,20 +1806,24 @@ end
18051806

18061807
# ---------------------------------------------------------------------------------------------------
18071808
"""
1808-
I = ind2rgb(I::GMTimage, cmap::GMTcpt=GMTcpt(), layout="BRPa")
1809+
I = ind2rgb(I::GMTimage, cpt::GMTcpt=GMTcpt(), layout="BRPa"; cmap=GMTcpt())
18091810
18101811
Convert an indexed image I to RGB. If `cmap` is not provided, it uses the internal colormap to do the conversion.
18111812
If neither them exists, the layer is replicated 3 times thus resulting in a gray scale image.
1813+
1814+
Use the `cmap` keyword in alternative to the `cpt` positional variable.
18121815
"""
1813-
function ind2rgb(I::GMTimage, cmap::GMTcpt=GMTcpt(), layout="BRPa")
1814-
(size(I.image, 3) >= 3) && return I # Image is already RGB(A)
1816+
function ind2rgb(I::GMTimage, cpt::GMTcpt=GMTcpt(), layout="BRPa"; cmap=GMTcpt())
1817+
(size(I.image, 3) >= 3) && return I # Image is already RGB(A)
1818+
1819+
(isempty(cpt) && isa(cmap, Symbol) || isa(cmap, String)) && (cpt = makecpt(I.range[6]-I.range[5]+1, C=cmap))
18151820

18161821
# If the CPT is shorter them maximum in I, reinterpolate the CPT
1817-
(!isempty(cmap) && (ma = maximum(I.image)) > size(cmap.colormap,1)) && (cmap = gmt("makecpt -T0/{$ma}/+n{$ma}", cmap))
1818-
_cmap = (!isempty(cmap)) ? cpt2cmap(cmap::GMTcpt, I.nodata)[1] : I.colormap
1822+
(!isempty(cpt) && (ma = maximum(I.image)) > size(cpt.colormap,1)) && (cpt = gmt("makecpt -T0/{$ma}/+n{$ma}", cpt))
1823+
_cmap = (!isempty(cpt)) ? cpt2cmap(cpt::GMTcpt, I.nodata)[1] : I.colormap
18191824

18201825
have_alpha = (length(I.colormap) / I.n_colors) == 4 && !all(I.colormap[end-Int(I.n_colors/4+1):end] .== 255)
1821-
if (I.n_colors == 0 && isempty(cmap)) # If no cmap just replicate the first layer.
1826+
if (I.n_colors == 0 && isempty(cpt)) # If no cmap just replicate the first layer.
18221827
imgRGB = repeat(I.image, 1, 1, 3)
18231828
layout = I.layout
18241829
else

0 commit comments

Comments
 (0)