# RGB extremes (white, black, red, green, blue, yellow, magenta, cyan)
ctrs <- matrix(c(1, 1, 1,
0, 0, 0,
1, 0, 0,
0, 1, 0,
0, 0, 1,
1, 1, 0,
1, 0, 1,
0, 1, 1), byrow = TRUE, ncol = 3)
# plot it
recolorize::plotColorPalette(ctrs)
# create a pixel matrix of random colors
pixel_matrix <- matrix(runif(3000), ncol = 3)
# assign pixels
# see `assignPixels` function for details
reassigned <- assignPixels(ctrs, pixel_matrix, adjust_centers = TRUE)
# find residuals from original color centers
color_residuals <- colorResiduals(pixel_matrix = pixel_matrix,
pixel_assignments = reassigned$pixel_assignments,
centers = ctrs)
# compare to residuals from adjusted color centers
color_residuals_adjust <- colorResiduals(pixel_matrix = pixel_matrix,
pixel_assignments = reassigned$pixel_assignments,
centers = reassigned$centers)
# to reset graphical parameters:
current_par <- graphics::par(no.readonly = TRUE)
layout(matrix(1:2, nrow = 2))
hist(color_residuals$sq_residuals,
breaks = 30, border = NA, col = "tomato",
xlim = c(0, 1), xlab = "Squared residual",
main = "Original centers")
hist(color_residuals_adjust$sq_residuals,
breaks = 30, border = NA, col = "cornflowerblue",
xlim = c(0, 1), xlab = "Squared residual",
main = "Adjusted centers")
graphics::par(current_par)
Run the code above in your browser using DataLab