Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #' Create ternary maps based on the HSV or HCL color space
  2. #'
  3. #' Creates a map based on HSV or HCL color space and returns an image stack
  4. #' with three bands containing the HSV/HCL colors converted back to RGB
  5. #' to easier plotting
  6. #'
  7. #' @param H Hue raster usually representing angles in the HSV color wheel and within
  8. #' the [0,1] interval
  9. #' @param S Saturation raster for HSV and within [0,1] interval
  10. #' @param V Value raster for HSV and within [0,1] interval
  11. #'
  12. #' @param hue Hue raster usually representing angles in the HCL color wheel and within
  13. #' the [0,360] interval
  14. #' @param chroma Chroma raster for HCL and within [0,100] interval
  15. #' @param luminance Luminance raster for HCL and within [0,100] interval
  16. #'
  17. #' @param colorspace Color space to use. Either 'HSV' (default) or 'HCL'
  18. #' @param rescale Re-scale values of S and V or chroma and luminance to 0 - 1 range?
  19. #' Rescaling is performed as: (x-min(x)) / (max(x)-min(x))? (default: FALSE)
  20. #' @param hueUnits Units of angle in the H (HSV) or hue (HCL) component?
  21. #' Either 'deg' (degrees) or 'rad' (radians)
  22. #' @param verbose Print progress messages? (default: TRUE)
  23. #' @param filename A filename to which write the output RGB raster stack (default: NULL).
  24. #' @param ... Extra parameters to pass in writeRaster
  25. #'
  26. #' @return A RasterStack object with three bands (for R,G, and B values in the 0-255 range)
  27. #'
  28.  
  29. ternaryMaps <- function(H=NULL, S=NULL, V=NULL, hue=NULL, chroma=NULL, luminance=NULL, colorspace="HSV",
  30. rescale=FALSE, hueUnits="deg", verbose=TRUE, filename=NULL, ...){
  31.  
  32. require(raster)
  33.  
  34. if(colorspace=="HSV"){
  35. if(any(c(is.null(H), is.null(S), is.null(V)))){
  36. stop("H, S and V must be non-null! Please check input arguments.")
  37. }
  38. c1 <- H
  39. c2 <- S
  40. c3 <- V
  41. }
  42. if(colorspace=="HCL"){
  43. if(any(c(is.null(hue), is.null(chroma), is.null(luminance)))){
  44. stop("hue, chroma and luminance must be non-null! Please check input arguments.")
  45. }
  46. c1 <- hue
  47. c2 <- chroma
  48. c3 <- luminance
  49. }
  50.  
  51. if(!inherits(c1,"RasterLayer") || !inherits(c2,"RasterLayer") || !inherits(c3,"RasterLayer")){
  52. stop("H/S/V or hue/chroma/luminance must be RasterLayer objects!")
  53. }
  54.  
  55. if(!compareRaster(c1,c2,c3,stopiffalse = FALSE)){
  56. stop("Rasters in H/S/V or hue/chroma/luminance are different! Please check input arguments.")
  57. }
  58.  
  59. if(rescale){
  60.  
  61. if(verbose) message("Standardizing raster data....")
  62.  
  63. rescale01 <- function(x, ...){(x - cellStats(x, 'min', ...)) / (cellStats(x, 'max', ...) - cellStats(x, 'min', ...))}
  64. #c1 <- rescale01(c1)
  65. if(hueUnits=="deg"){
  66. c1 <- c1 / 360
  67. }else if(hueUnits=="rad"){
  68. c1 <- c1 / (2*pi)
  69. }else{
  70. stop("Invalid units in hueUnits! Either 'deg' (degrees) or 'rad' (radians) options are valid.")
  71. }
  72. c2 <- rescale01(c2)
  73. c3 <- rescale01(c3)
  74.  
  75. if(verbose) message("Finished!")
  76. }
  77.  
  78.  
  79. if(verbose) message("Stacking and loading raster data....")
  80. rstDF <- values(stack(c1,c2,c3))
  81. if(verbose) message("Finished!")
  82.  
  83.  
  84. if(verbose) message("Calculating colors....")
  85. ind <- complete.cases(rstDF)
  86.  
  87. if(colorspace=="HSV"){
  88. # Generate HSV color codes
  89. colVec <- hsv(h = rstDF[ind,1], s = rstDF[ind,2], v = rstDF[ind,3])
  90. # Convert color code to RGB and transpose the color matrix from wide to long
  91. rgbCol <- t(col2rgb(colVec))
  92. }
  93.  
  94. if(colorspace=="HCL"){
  95. # Generate HCL color codes
  96. colVec <- hcl(h = rstDF[ind,1]*360, c = rstDF[ind,2]*100, l = rstDF[ind,3]*100)
  97. # Convert color code to RGB and transpose the color matrix from wide to long
  98. rgbCol <- t(col2rgb(colVec))
  99. }
  100. if(verbose) message("Finished!")
  101.  
  102.  
  103. if(verbose) message("Attributing colors to the output RGB raster....")
  104.  
  105. r <- c1
  106. values(r) <- NA
  107. values(r)[ind] <- rgbCol[,1] # red component
  108.  
  109. g <- c1
  110. values(g) <- NA
  111. values(g)[ind] <- rgbCol[,2] # green component
  112.  
  113. b <- c1
  114. values(b) <- NA
  115. values(b)[ind] <- rgbCol[,3] # blue component
  116.  
  117. rstOut <- stack(r,g,b)
  118.  
  119. if(verbose) message("Finished!")
  120.  
  121. if(!is.null(filename)){
  122. if(verbose) message("Writing raster data to file....")
  123.  
  124. writeRaster(rstOut, filename = filename, ...)
  125.  
  126. if(verbose) message("Finished!")
  127. }
  128. return(rstOut)
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement