Saturday, January 25, 2014

GMT standard color palettes

GMT (Generic Mapping Tools) (http://gmt.soest.hawaii.edu/) is a great mapping tool. I'm hoping to use it more in the future, but for the meantime I wanted to recreate some of the it's standard color palettes in R. Unfortunately, I couldn't find documentation of the precise rgb color levels used, so I ended up "stealing" them from the .png images on this website: http://www.geos.ed.ac.uk/it/howto/GMT/CPT/palettes.html

Here's the result:




Here's how I extracted the color levels from the .png images:

#make palettes
library(png)
 
pal.names <- c(
"cool","copper","gebco","globe",
"gray","haxby","hot","jet",
"no_green","ocean","polar","rainbow",
"red2green","relief","sealand","seis",
"split","topo","wysiwyg"
)
 
png.urls <- paste0("http://www.geos.ed.ac.uk/it/howto/GMT/CPT/cpt-", pal.names, ".png")
 
gmt.pals <- vector(mode="list", length(pal.names)) 
names(gmt.pals) <- pal.names
for(i in seq(gmt.pals)){
 myurl <- png.urls[i]
 tmp <- tempfile()
 download.file(myurl,tmp,mode="wb")
 pic <- readPNG(tmp)
 file.remove(tmp) # cleanup
 
 Colors <- NA * seq(20)
 row <- round(dim(pic)[1]/2)
 breaks <- seq(1, dim(pic)[2],,21)
 cols <- round(breaks[-1] - ((breaks[2]-breaks[1])/2))
 gmt.pals[[i]] <- rev(rgb(pic[row,cols,1], pic[row,cols,2], pic[row,cols,3])) # reverses colors to put low color values first in the vector
 
}
 
#plot of palettes
png("gmt_palettes.png", width=6, height=10, units="in", res=400)
op <- par(mar=c(0.25,0.25,1.25,0.25), mfrow=c(length(gmt.pals), 1), ps=10)
for(i in seq(gmt.pals)){
 image(matrix(seq(gmt.pals[[i]]), length(gmt.pals[[i]])), 1, col=gmt.pals[[i]], axes=FALSE)
 box()
 mtext(names(gmt.pals)[i], side=3, line=0.1)
}
par(op)
dev.off()
Created by Pretty R at inside-R.org


Finally, I created a function ('gmtColors') that allows me to call these color levels by GMT palette name:

#function 'gmtColors' to call color levels.
#palette name `pal.name` is one of the following:
#"cool", "copper", "gebco", "globe", "gray",
#"haxby", "hot", "jet", "no_green", "ocean",
#"polar", "rainbow", "red2green", "relief", "sealand",
#"seis", "split", "topo", "wysiwyg"
gmtColors <- function(pal.name="relief"){
 tmp <- structure(list(cool = c("#00FFFF", "#0DF2FF", "#19E6FF", "#26D9FF", 
"#33CCFF", "#3FBFFF", "#4CB3FF", "#59A6FF", "#6699FF", "#738CFF", 
"#7F7FFF", "#8C73FF", "#9966FF", "#A659FF", "#B24DFF", "#BF3FFF", 
"#CC33FF", "#D926FF", "#E619FF", "#F20DFF"), copper = c("#000000", 
"#100906", "#1F130D", "#301E13", "#40281A", "#50321F", "#603C26", 
"#70462D", "#805033", "#905A3A", "#A06440", "#B06E46", "#C0784D", 
"#D08253", "#E08C5A", "#F09660", "#FFA066", "#FFAA6D", "#FFB473", 
"#FFBE7A"), gebco = c("#00F0FF", "#00F0FF", "#00F0FF", "#23FFFF", 
"#23FFFF", "#23FFFF", "#5AFFFF", "#5AFFFF", "#5AFFFF", "#8CFFE6", 
"#8CFFE6", "#8CFFE6", "#A5FFD7", "#A5FFD7", "#A5FFD7", "#C3FFD7", 
"#C3FFD7", "#C3FFD7", "#D2FFD7", "#E6FFF0"), globe = c("#9900FF", 
"#9900FF", "#7722FF", "#5544FF", "#3366FF", "#1188FF", "#1BA4FF", 
"#51BAFF", "#86D0FF", "#BCE6FF", "#336600", "#F3CA89", "#D9A627", 
"#A49019", "#9F7B0D", "#996600", "#B27676", "#C2B0B0", "#E5E5E5", 
"#FFFFFF"), gray = c("#000000", "#0D0D0D", "#191919", "#262626", 
"#333333", "#3F3F3F", "#4C4C4C", "#595959", "#666666", "#737373", 
"#7F7F7F", "#8C8C8C", "#999999", "#A6A6A6", "#B2B2B2", "#BFBFBF", 
"#CCCCCC", "#D9D9D9", "#E6E6E6", "#F2F2F2"), haxby = c("#090079", 
"#280096", "#0009C8", "#0019D4", "#1A66F0", "#19AFFF", "#32BEFF", 
"#61E1F0", "#6AECE1", "#8AECAE", "#CDFFA2", "#DFF68D", "#F8D768", 
"#FFBD57", "#F4754B", "#FF5A5A", "#FF7C7C", "#F6B3AE", "#FFC4C4", 
"#FFECEC"), hot = c("#000000", "#220000", "#440000", "#660000", 
"#880000", "#AA0000", "#CC0000", "#EE0000", "#FF1100", "#FF3300", 
"#FF5500", "#FF7700", "#FF9900", "#FFBB00", "#FFDD00", "#FFFF00", 
"#FFFF33", "#FFFF66", "#FFFF99", "#FFFFCC"), jet = c("#00007F", 
"#0000B2", "#0000E5", "#0019FF", "#004DFF", "#007FFF", "#00B2FF", 
"#00E5FF", "#FFFFF2", "#FFFFD9", "#FFFFBF", "#FFFFA5", "#FFFF8C", 
"#FFE500", "#FFB300", "#FF7F00", "#FF4C00", "#FF1900", "#E50000", 
"#B20000"), no_green = c("#1F60FF", "#1F60FF", "#1F9FFF", "#1FBFFF", 
"#00CFFF", "#2AFFFF", "#2AFFFF", "#55FFFF", "#7FFFFF", "#AAFFFF", 
"#FFFF54", "#FFFF54", "#FFF000", "#FFBF00", "#FFA800", "#FF8A00", 
"#FF8A00", "#FF7000", "#FF4D00", "#FF0000"), ocean = c("#000000", 
"#000209", "#000413", "#00061E", "#000728", "#000932", "#002650", 
"#00426E", "#005E8C", "#007AAA", "#0096C8", "#22A9C2", "#45BCBB", 
"#67CFB5", "#8AE2AE", "#ACF6A8", "#BCF8B9", "#CBF9CA", "#DBFBDC", 
"#EBFDED"), polar = c("#0000FF", "#1919FF", "#3333FF", "#4C4CFF", 
"#6666FF", "#7F7FFF", "#9999FF", "#B2B2FF", "#CCCCFF", "#E6E6FF", 
"#FFFFFF", "#FFE5E5", "#FFCCCC", "#FFB2B2", "#FF9999", "#FF7F7F", 
"#FF6666", "#FF4C4C", "#FF3333", "#FF1A1A"), rainbow = c("#FF00FF", 
"#BF00FF", "#7F00FF", "#3F00FF", "#0000FF", "#003FFF", "#007FFF", 
"#00BFFF", "#00FFFF", "#00FFBF", "#00FF7F", "#00FF3F", "#00FF00", 
"#3FFF00", "#7FFF00", "#BFFF00", "#FFFF00", "#FFBF00", "#FF7F00", 
"#FF3F00"), red2green = c("#FF0000", "#FF1919", "#FF3333", "#FF4C4C", 
"#FF6666", "#FF7F7F", "#FF9999", "#FFB2B2", "#FFCCCC", "#FFE6E6", 
"#FFFFFF", "#E5FFE5", "#CCFFCC", "#B2FFB2", "#99FF99", "#7FFF7F", 
"#66FF66", "#4CFF4C", "#33FF33", "#1AFF1A"), relief = c("#000000", 
"#000413", "#000728", "#002650", "#005E8C", "#0096C8", "#45BCBB", 
"#8AE2AE", "#BCF8B9", "#DBFBDC", "#467832", "#887438", "#B19D48", 
"#DBC758", "#FAE769", "#FAEB7E", "#FCED93", "#FCF1A7", "#FCF6C1", 
"#FDFAE0"), sealand = c("#8C66FF", "#6A66FF", "#6684FF", "#66A7FF", 
"#66CAFF", "#66ECFF", "#66FFF0", "#66FFCE", "#66FFAB", "#66FF88", 
"#66FF66", "#88FF66", "#ABFF66", "#CEFF66", "#FFEEA6", "#FFD3A6", 
"#FFB8A6", "#FFAAB0", "#FFB5CB", "#FFC0E1"), seis = c("#AA0000", 
"#D00000", "#F70000", "#FF1D00", "#FF4400", "#FF6A00", "#FF9000", 
"#FFB700", "#FFDD00", "#FFFF00", "#FFFF00", "#FFFF00", "#BDFF0C", 
"#73FF1A", "#3FFA36", "#16F45A", "#00D08B", "#0087CD", "#0048FA", 
"#0024E3"), split = c("#7F7FFF", "#6666E6", "#4D4DCC", "#3333B3", 
"#1A1A99", "#00007F", "#000066", "#00004D", "#000033", "#00001A", 
"#000000", "#1A0000", "#330000", "#4D0000", "#660000", "#7F0000", 
"#991A1A", "#B33333", "#CC4D4D", "#E66666"), topo = c("#C977D9", 
"#A18AE6", "#8AA2E6", "#8BD1E7", "#8AF3CF", "#85F38E", "#BDF385", 
"#EDE485", "#F0B086", "#DE9F8B", "#74A3B3", "#99CC70", "#DCD68E", 
"#EDDFAD", "#F7E8CA", "#FFF9F3", "#FFF9F6", "#FFFBF9", "#FFFCFA", 
"#FFFEFD"), wysiwyg = c("#3F003F", "#3F003F", "#3F00BF", "#003FFF", 
"#00A0FF", "#3FBFFF", "#3FBFFF", "#40E0FF", "#3FFFBF", "#3FFF3F", 
"#7FFF3F", "#BFFF3F", "#BFFF3F", "#FFE040", "#FFE040", "#FF6040", 
"#FF1F40", "#FF60C0", "#FFA0FF", "#FFA0FF")), .Names = c("cool", 
"copper", "gebco", "globe", "gray", "haxby", "hot", "jet", "no_green", 
"ocean", "polar", "rainbow", "red2green", "relief", "sealand", 
"seis", "split", "topo", "wysiwyg"))
 
 tmp[[match(pal.name, names(tmp))]]
}
Created by Pretty R at inside-R.org


3 comments:

  1. The GMT colour palettes are defined in $SRC/share/cpt/ (where $SRC is the folder where you unzip the gmt 5 source code). Names of the palettes are in $SRC/share/conf/gmt_cpt.conf.

    The CPT files only seem to specify colours at the break, and not the interpolation method. Is it nearest colour, linear interpolation in RGB, linear in HSV? Other? Answer must bein thesource code somewhere...

    ReplyDelete
    Replies
    1. Thanks for the info Barry. I'm going to look into this further and see if I can get more information from the files you mention. Cheers, Marc

      Delete
  2. This comment has been removed by the author.

    ReplyDelete