The world’s leading publication for data science, AI, and ML professionals.

Generating Color Palettes in R

List of R packages to generate color palettes from images

Photo by Andrew Ridley on Unsplash
Photo by Andrew Ridley on Unsplash

Join Medium with my referral link – Abhinav Malasi

For the past couple of months, I was trying my hands on generative art. And the majority of my art ended up in shades of gray. I never was able to spin my head around to get my art to look as pretty as you see on the web from other artists.

So, I started using color palette extractor tools from the web. And this made me think, do I even have these options in R itself. As copying the hex code from the online editor is painstakingly boring. I searched and found a bunch of R packages with the power to extract color palettes from images and also give ample control over the outcome.

To show the functioning of the packages, I will be using an image from the imgpalr package as a reference. And for consistency, the size of the color palette is fixed to 10. To reproduce your work don’t forget to set the seed. Let’s begin.


paletteR

Author: Andrea Cirillo

A simple yet powerful tool to extract colors and create a palette from an image. The color extractor uses a k-means algorithm.

The package uses create_palette() for palette creation. The basic arguments to get things going are providing the file path, the size of the palette, and set type of variable to categorical. Further fine-tuning can be done by providing arguments on the cut-offs for brightness and saturation.

Extracted color palette (L), the extracted palette used for plotting with ggplot (R). (Image by author)
Extracted color palette (L), the extracted palette used for plotting with ggplot (R). (Image by author)

The variant for the above palette is achieved by setting the values of the argument filter_on_low_brightness and filter_on_saturation to FALSE.

Extracted variant color palette (L), the variant extracted palette used for plotting with ggplot (R). (Image by author)
Extracted variant color palette (L), the variant extracted palette used for plotting with ggplot (R). (Image by author)

Even the gods approve the package.

imgpalr

Author: Matthew Leonawicz

Imgpalr also uses the k-means algorithm to create color palettes. The image_pal() function for palette generator gives much wider control on creating palettes from images. The basic arguments cover the file path and the number of colors to be identified. By setting the ranges for arguments such as saturation, brightness, and bw (for black and white pixels) the generated palette can be tuned. The argument type helps define sequential, qualitative, or divergent palettes. Further, the sequential palette can be sorted either by hsv, svh, or vhs using the seq_by argument. Further, the k-means cluster size argument can also be tuned.

The below palette is generated with default values.

Palette generated with default values (L), the palette is used for plotting purposes (R). (Image by author)
Palette generated with default values (L), the palette is used for plotting purposes (R). (Image by author)

Just by changing the cut-off saturation range, the palette can be tuned as seen below.

Palette generated with tuning the saturation range(L), the palette is used for plotting purposes (R). (Image by author)
Palette generated with tuning the saturation range(L), the palette is used for plotting purposes (R). (Image by author)

colorfindr

Author: David Zumbach and Mara Averick

The package uses get_colors() function to extract colors. Apart from defining the size of the palette using the argument top_n which is frequency-based. The palette can also be created by defining the color share using min_share argument. The value range for the min_share argument lies between 0–1. It also hosts the option to exclude colors from the palette using arguments exclude_col and exclude_rad. The make_palette() function creates the palette. It has the option to set either of the two popular techniques of clustering: k-means and median cut. The choice for the cluster can be set using the argument clust_method.

The palette is generated using the default settings with clust_method argument set to kmeans.

Palette generation using k-means. (Image by author)
Palette generation using k-means. (Image by author)

Here is the palette generated when the argument for clust_method is set to median cut.

Palette generation using median cut. (Image by author)
Palette generation using median cut. (Image by author)

RImagePalette

Author: Joel Carlson

The package uses the median cut algorithm to extract the dominant colors from the image. The image_palette() function is used to extract colors. The arguments as usual are the image path, number of colors for the palette, choice argument that defines the function of how colors are chosen.

The package has other cool functionalities that integrate with ggplot2. The functions scale_color_image() and scale_fill_image() can directly use the generated color palettes in ggplot2.

The below palette is generated using the default settings with the number of colors palette set to 10.

Palette generated using default settings. (Image by author)
Palette generated using default settings. (Image by author)

The plot below is generated using scale_color_image() and scale_fill_image() functions to generate the palettes. The variation is achieved by setting the choice argument to min.

Palette variant generated by setting the choice argument to min. (Image by author)
Palette variant generated by setting the choice argument to min. (Image by author)

magick

Author: rOpenSci

Magick is the goto package for image processing in R. The package has a function called image_quantize() which does the trick for us but is not a straightforward path. The blog by Chisato explains it beautifully.


Code used:


These are some of the packages I found very useful for generating personalized color palettes in R. I hope this is helpful to some of you.

Get an email whenever Abhinav Malasi publishes.

References

  1. https://github.com/AndreaCirilloAC/paletter
  2. https://github.com/leonawicz/imgpalr
  3. https://github.com/zumbov2/colorfindr
  4. https://cran.r-project.org/web/packages/magick/vignettes/intro.html
  5. https://www.r-bloggers.com/2019/01/extracting-colours-from-your-images-with-image-quantization/

_You can connect with me on LinkedIn and Twitter to follow my data science and Data Visualization journey. Or you can follow me on Medium to stay updated on new articles on data visualization with R and ggplot2 package._


Related Articles