Learn R Programming

:rainbow: schemr package

Convert photos into useable colour schemes

schemr is an R package for turning your photos into usable colour schemes for R visualisations.

The key driver is the image_to_pallette function, which:

  • reads in images;
  • finds colour blobs within the image, representing the key highlighting colours; and
  • uses affinity propagation clustering to condense the set of key colours.

:hammer: Installing schemr

Install the latest CRAN version of schemr by:

install.packages("schemr")

Alternatively, install the development version of schemr can be installed by running:

devtools::install_github("stuart-morrison/schemr")

:camera: Photo example

First we have a look at a photo of my beautiful old car.

library(OpenImageR)
library(magrittr)

# Read in the image
image <- readImage(path = "images/car.jpg")

# Shrink down the image - Note resizeImage width and height are on array dimensions, rather than on image dimensions
new_height <- dim(image)[1] * 0.5
new_width <- dim(image)[2] * 0.5
image %<>% resizeImage(image = ., width = new_height, height = new_width)

# Plot
plot(as.raster(image))

We see big blobs of blue, yellow and orange. Using schemr to extract these, we get:

library(schemr)

# Extract key colours from image
# I use the median to extract the centre of each 'blob' - but any function summary function, eg, mean, max, min, will all work
schemr_data <- image_to_pallette(image_path = "images/car.jpg", resize_factor = 0.5,
                                  verbose = FALSE, summary_method = median)

# Plot the image
plot(schemr_data)

We can see the palette of colours found in the image by using the palette method on the schemr data.

palette(schemr_data)

In addition, printing the class, shows the vector of hex RGB codes that make up the clustered data:

schemr_data
## [1] "#010101" "#201c16" "#524511" "#8a8664" "#f1c01e"
## [6] "#997520" "#463316" "#3f4b49" "#848aa7" "#5d4e55"
## [11] "#10211e" "#964227" "#ffffff" "#297d7f"

:bar_chart: Using schemr palettes in plots

The evaluated palette is easy to apply immediately into data visualisation by access through the palette attribute.

library(ggplot2)
# Example plot using the iris data set
ggplot() +
    geom_point(data = iris,
               aes(x = Petal.Length, y = Petal.Width,
                   col = Species),
               size = 4) +
    scale_color_manual(name = "Species",
                       values = schemr_data$palette[c(5, 12, 14)]) +
    labs(x = "Petal length", y = "Petal width") +
    theme_bw(base_size = 18)

:raised_hands: Colour space conversions

schemr also contains functions to convert colour data both to and from the following colour spaces:

From\ToRGBXYZLabHSLHSV
RGB:white_check_mark::white_check_mark::white_check_mark::white_check_mark:
XYZ:white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Lab:white_check_mark::white_check_mark::white_check_mark::white_check_mark:
HSL:white_check_mark::white_check_mark::white_check_mark::white_check_mark:
HSV:white_check_mark::white_check_mark::white_check_mark::white_check_mark:

Colour conversion constants and functions are provided for sRGB and Adobe 1998 RGB spaces, with user ability to apply other conversions for other RGB spaces.

For example, using excellent colours from the wesanderson package:

library(wesanderson)

# Extract some lovely Zissou colours
colour_hex <- wes_palettes$Zissou1

# Convert to Lab space
colour_lab <- hex_to_lab(hex = colour_hex, transformation = "Adobe")

# Convert Lab space to XYZ space
colour_xyz <- lab_to_xyz(lab = colour_lab)

# Convert XYZ space to RGB colour channels
colour_rgb <- xyz_to_rgb(xyz = colour_xyz, transformation = "Adobe")

Copy Link

Version

Install

install.packages('schemr')

Monthly Downloads

314

Version

0.3.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Stuart Morrison

Last Published

January 22nd, 2025

Functions in schemr (0.3.1)

lab_to_hsl

Convert Lab to HSL
palette,schemr-method

Plot the colour palette
hsv_to_xyz

Convert HSV to XYZ
xyz_to_hex

Convert from XYZ space into hex RGB colour values.
schemr-class

Create the schemr class, which holds the palette and image data
image_to_pallette

Develop a usable colour palette form an image.
rgb_to_hsl

Convert RGB space into HSL space
xyz_to_lab

Convert from XYZ colour channels to Lab space.
rgb_to_hsv

Convert RGB to HSV
rgb_to_hex

Convert RGB colour channels to hex colour codes.
xyz_to_rgb

Convert from RGB colour channels to XYZ space.
plot,schemr,ANY-method

Plot the clustered image data
rgb_to_lab

Convert from RGB colour channels to Lab space.
rgb_to_xyz

Convert from RGB colour channels to XYZ space.
xyz_to_hsv

Convert XYZ to HSV
xyz_to_hsl

Convert XYZ to HSL
hsv_to_lab

Convert HSV to Lab
hsl_to_lab

Convert HSL to Lab
hsv_to_rgb

Convert HSV to RGB
hsl_to_xyz

Convert HSL to XYZ
hex_to_xyz

Convert hex RGB values to XYZ space.
hsl_to_hsv

Convert HSL to HSV
hsv_to_hsl

Convert HSV to HSL
hex_to_lab

Convert hex RGB values to Lab space.
hex_to_rgb

Convert hexadecimal colours to RGB colour channels.
hsl_to_rgb

Convert HSL space into RGB space
lab_to_rgb

Convert from Lab space into RGB colour channels.
lab_to_hsv

Convert Lab to HSv
lab_to_xyz

Convert from Lab space to XYZ colour channels.
lab_to_hex

Convert from Lab space into hex RGB colour values.