Skip to content

Commit

Permalink
more notes, cleanup for #318
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanbeaudette committed Sep 26, 2024
1 parent 12164ed commit afb18cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
24 changes: 18 additions & 6 deletions R/munsell2rgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,22 @@ munsell2rgb <- function(the_hue, the_value, the_chroma, alpha = 1, maxColorValue
if(length(N.idx) > 0)
the_chroma[N.idx] <- 0


## TODO: any other hue with 0 chroma should be interpreted as N


# value / chroma should be within unique set of allowed chips
valid.value <- unique(as.character(munsell$value))
valid.chroma <- unique(as.character(munsell$chroma))


## warn if non-standard notation

## TODO: should rounding be enabled by default for backwards compatibility?
## TODO: rounding is wrong with e.g. 10YR 2.6 / 3 --> closest value is 2.5
## TODO:
## * should rounding be enabled by default for backwards compatibility?
## * current rounding is wrong with e.g. 10YR 2.6 / 3 --> closest value is 2.5
## * find closest value/chroma by distance search, once 0.5 values are ready
## * why are we converting to character?

# value
if(any(! as.character(na.omit(the_value)) %in% valid.value)) {
Expand All @@ -371,15 +379,17 @@ munsell2rgb <- function(the_hue, the_value, the_chroma, alpha = 1, maxColorValue
)

## benchmarks:
# plyr::join 2x faster than base::merge
# data.table::merge (with conversion to/from) 5x faster than base::merge
# plyr::join() 2x faster than base::merge
# data.table::merge() (with conversion to/from) 5x faster than base::merge()

## TODO: maybe more efficient with keys
# round-trip through data.table is still faster
d <- data.table::as.data.table(d)
munsell <- data.table::as.data.table(munsell)

# join
res <- merge(d, munsell, by = c('hue','value','chroma'), all.x = TRUE, sort = FALSE)

# back to data.frame
res <- as.data.frame(res)

Expand All @@ -405,8 +415,10 @@ munsell2rgb <- function(the_hue, the_value, the_chroma, alpha = 1, maxColorValue
alpha <- maxColorValue
}

# convert to R color
res$soil_color <- NA # init an empy column

## convert to hex notation
# init an empty column
res$soil_color <- NA

# account for missing values if present: we have to do this because rgb() doesn't like NA
if(length(rgb.na > 0)) {
Expand Down
7 changes: 4 additions & 3 deletions misc/utils/Munsell/prepare-munsell-LUT.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,16 @@ table(m.new.chroma$C)
# * do we need multivariate interpolation?
#

.cols <- hcl.colors(length(unique(m.new.chroma$C)))
.n <- length(unique(m.new.chroma$C[m.new.chroma$H %in% c('2.5Y', '2.5YR', '2.5R')]))
.cols <- hcl.colors(n = .n, palette = 'blues3')

xyplot(
x ~ V | factor(H),
groups = C,
data = m.new.chroma,
subset = H %in% c('2.5Y', '2.5YR', '2.5R'),
type = 'l',
par.settings = tactile.theme(superpose.line = list(col = .cols)),
par.settings = tactile.theme(superpose.line = list(col = .cols, lwd = 2)),
as.table = TRUE,
scales = list(alternating = 1),
panel = function(...) {
Expand All @@ -190,7 +191,7 @@ xyplot(
data = m.new.chroma,
subset = H %in% c('2.5Y', '2.5YR', '2.5R'),
type = 'l',
par.settings = tactile.theme(superpose.line = list(col = .cols)),
par.settings = tactile.theme(superpose.line = list(col = .cols, lwd = 2)),
as.table = TRUE,
scales = list(alternating = 1),
panel = function(...) {
Expand Down

0 comments on commit afb18cc

Please sign in to comment.