Often for batch processing purposes, it is important to ensure that color centers fit using different methods are in the same order.
Details
While you can manually specify the col_order
vector, one way to
automatically order the colors according to an external color palette (as
might be needed for batch processing) is to use the match_colors function,
although it is recommended to double-check the results.
Examples
img <- system.file("extdata/corbetti.png", package = "recolorize")
rc <- recolorize2(img, cutoff = 45)
#>
#> Using 2^3 = 8 total bins
ref_palette <- c("mediumblue", "olivedrab", "tomato2", "beige", "grey10")
col_order <- match_colors(ref_palette, rc$centers, plotting = TRUE)
rc2 <- reorder_colors(rc, col_order, plotting = FALSE)
# the colors are reordered, but not changed to match the reference palette:
plot(rc2)
# you can also change them to the reference palette:
rc2$centers <- t(grDevices::col2rgb(ref_palette) / 255)
plot(rc2)