create_pal creates a palette using colors extracted from an image.

create_pal(
  image,
  n,
  resize = 0.1,
  method = "kmeans",
  colorblind = FALSE,
  sort = "none",
  show.pal = TRUE,
  title = "",
  ...
)

Arguments

image

a file path/URL to the raster image (JPG, JPEG, PNG, TIFF) from which the colors are to be extracted.

n

a positive integer. The number of colors to be returned in the palette.

resize

a number between 0 and 1. This indicates the fraction by which the width and height (in pixels) of the original image is resized through magick::image_resize. Default to 0.1.

method

the classification method for grouping the image pixels based on the RGB values. Options are "kmeans" and "Gaussian_mix". Default to "kmeans". See 'Details' section for more information.

colorblind

logical. Whether to render the palette colorblind-friendly. Default to FALSE. See 'Details' section for more information.

sort

a character indicating how the colors in the palette should be sorted. Available options are "none", "hue", "saturation", and "value". Default to "none"(unsorted). See 'Details' section for more information.

show.pal

logical. Whether to display the palette or not. Default to TRUE.

title

a character string giving the title of the displayed palette.

...

additional arguments passed to ggplot2::theme.

Value

A vector of hexadecimal color codes.

Details

Two clustering methods are available. For method = "kmeans", image pixels are partitioned into clusters using kmeans, and the RGB values of the cluster centers are converted into the corresponding hexadecimal color codes. For method = "Gaussian_mix", pixel components are identified via multivariate Gaussian mixture modeling using ClusterR::GMM, and the RGB values of the component centroids are converted into the corresponding hexadecimal color codes.

If "colorblind = TRUE", the original colors returned in the palette are converted into their colorblind-friendly counterparts using colorBlindness::replacePlotColor.

The colors returned in the palette can be ordered in the HSV color space. If sort = "hue", the colors are sorted by hue in an ascending order. If sort = "saturation", the colors are sorted by saturation in a descending order. If sort = "value", the colors are sorted by value in a descending order.

Examples

if (FALSE) { library(PalCreatoR) image_path <- system.file("Mountain.JPG", package = "PalCreatoR") My_pal <- create_pal(image = image_path, n = 5, resize = 0.1, method = "kmeans", colorblind = FALSE, sort = "value", show.pal = TRUE, title = "My Palette") print(My_pal)}