Skip to content

Commit

Permalink
Merge pull request #325 from ncss-tech/col2Munsell
Browse files Browse the repository at this point in the history
Testing new aqp::col2Munsell() replacing aqp::rgb2munsell()
  • Loading branch information
brownag authored Dec 13, 2023
2 parents a5375d4 + d5af69c commit 3ec5c19
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/R-CMD-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck, any::remotes, terra=?ignore-before-r=4.1.0, local::.
extra-packages: any::rcmdcheck, any::remotes, terra=?ignore-before-r=4.1.0, local::., brownag/soilDBdata
# upgrade: 'TRUE' ## NB: required to force building of source packages
needs: check

- name: Install soilDBdata off GitHub
run: remotes::install_github("brownag/soilDBdata", dependencies = FALSE, build = FALSE)
shell: Rscript {0}

- name: Install aqp off GitHub (devel only)
if: matrix.config.r == 'devel'
run: |
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ importFrom(aqp,SoilTextureLevels)
importFrom(aqp,`depths<-`)
importFrom(aqp,`site<-`)
importFrom(aqp,checkHzDepthLogic)
importFrom(aqp,col2Munsell)
importFrom(aqp,hzDistinctnessCodeToOffset)
importFrom(aqp,munsell2rgb)
importFrom(aqp,parseMunsell)
importFrom(aqp,pbindlist)
importFrom(aqp,plotSPC)
importFrom(aqp,rgb2munsell)
importFrom(curl,curl_download)
importFrom(curl,has_internet)
importFrom(curl,new_handle)
Expand Down
13 changes: 7 additions & 6 deletions R/estimateColorMixture.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#'
#' @param wt fractional weights, usually area of hz face
#'
#' @param backTransform logical, should the mixed sRGB representation of soil color be transformed to closest Munsell chips? This is performed by [aqp::rgb2munsell()] default: `FALSE`
#' @param backTransform logical, should the mixed sRGB representation of soil color be transformed to closest Munsell chips? This is performed by [aqp::col2Munsell()] default: `FALSE`
#'
#' @return A data.frame containing estimated color mixture
#' @export estimateColorMixture
#'
#'
#' @importFrom aqp col2Munsell
#'
estimateColorMixture <- function(x, wt = 'pct', backTransform = FALSE) {

## TODO: account for `backTransform == TRUE`, different return structure
Expand Down Expand Up @@ -68,10 +70,9 @@ estimateColorMixture <- function(x, wt = 'pct', backTransform = FALSE) {
# performance penalty due to color distance eval against entire Munsell library
if(backTransform) {

# convert with best available metric
## TODO: once aqp 2.0.2 is on CRAN use col2Munsell()
# m <- col2Munsell(mixed.color[, c('r', 'g', 'b')])
m <- rgb2munsell(mixed.color[, c('r', 'g', 'b')])
# convert sRGB -> Munsell
# requires >= aqp 2.0.2
m <- col2Munsell(mixed.color[, c('r', 'g', 'b')])

# adjust names to match NASIS
names(m) <- c("colorhue", "colorvalue", "colorchroma", "sigma")
Expand Down
16 changes: 8 additions & 8 deletions R/simplifyColorData.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
#' @param wt a character vector with the name of the column containing color
#' weights for mixing
#' @param bt logical, should the mixed sRGB representation of soil color be
#' transformed to closest Munsell chips? This is performed by `aqp::rgb2munsell`
#' transformed to closest Munsell chips? This is performed by [aqp::col2Munsell()]
#' @author D.E. Beaudette
#' @keywords manip
#' @export
#' @importFrom grDevices rgb
#' @importFrom aqp munsell2rgb rgb2munsell
#' @importFrom aqp munsell2rgb col2Munsell
simplifyColorData <- function(d, id.var = 'phiid', wt = 'colorpct', bt = FALSE) {

# sanity check: must contain at least 1 row
Expand Down Expand Up @@ -112,9 +112,9 @@ simplifyColorData <- function(d, id.var = 'phiid', wt = 'colorpct', bt = FALSE)
mixed.dry <- dry.colors[dry.mix.idx, .SD, .SDcols = c(id.var, mix.vars)]
mixed.dry <- mixed.dry[, estimateColorMixture(.SD, wt = wt, backTransform = bt), by = id.var]

# back-transform mixture to Munsell using best-available method
## TODO: once aqp 2.0.2 is on CRAN use col2Munsell()
m <- aqp::rgb2munsell(as.data.frame(mixed.dry[, .SD, .SDcols = c('r', 'g', 'b')]))
# convert sRGB -> Munsell
# requires >= aqp 2.0.2
m <- col2Munsell(as.data.frame(mixed.dry[, .SD, .SDcols = c('r', 'g', 'b')]))

# adjust names to match NASIS
names(m) <- c("colorhue", "colorvalue", "colorchroma", "sigma")
Expand Down Expand Up @@ -151,9 +151,9 @@ simplifyColorData <- function(d, id.var = 'phiid', wt = 'colorpct', bt = FALSE)
mixed.moist <- moist.colors[moist.mix.idx, .SD, .SDcols = c(id.var, mix.vars)]
mixed.moist <- mixed.moist[, estimateColorMixture(.SD, wt = wt, backTransform = bt), by = id.var]

# back-transform mixture to Munsell using best-available method
## TODO: once aqp 2.0.2 is on CRAN use col2Munsell()
m <- rgb2munsell(as.data.frame(mixed.moist[, .SD, .SDcols = c('r', 'g', 'b')]))
# convert sRGB -> Munsell
# requires >= aqp 2.0.2
m <- col2Munsell(as.data.frame(mixed.moist[, .SD, .SDcols = c('r', 'g', 'b')]))

# adjust names to match NASIS
names(m) <- c("colorhue", "colorvalue", "colorchroma", "sigma")
Expand Down
2 changes: 1 addition & 1 deletion man/estimateColorMixture.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/simplifyColorData.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ec5c19

Please sign in to comment.