schemr
packageschemr
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:
schemr
Install the latest CRAN version of schemr
by:
install.packages("schemr")
Alternatively, install the development version of schemr
can be installed by running:
::install_github("stuart-morrison/schemr") devtools
First we have a look at a photo of my beautiful old car.
library(OpenImageR)
library(magrittr)
# Read in the image
<- readImage(path = "images/car.jpg")
image
# Shrink down the image - Note resizeImage width and height are on array dimensions, rather than on image dimensions
<- dim(image)[1] * 0.5
new_height <- dim(image)[2] * 0.5
new_width %<>% resizeImage(image = ., width = new_height, height = new_width)
image
# 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
<- image_to_pallette(image_path = "images/car.jpg", resize_factor = 0.5,
schemr_data 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"
schemr
palettes in plotsThe 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)
schemr
also contains functions to convert colour data
both to and from the following colour spaces:
From | RGB | XYZ | Lab | HSL | HSV |
---|---|---|---|---|---|
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
<- wes_palettes$Zissou1
colour_hex
# Convert to Lab space
<- hex_to_lab(hex = colour_hex, transformation = "Adobe")
colour_lab
# Convert Lab space to XYZ space
<- lab_to_xyz(lab = colour_lab)
colour_xyz
# Convert XYZ space to RGB colour channels
<- xyz_to_rgb(xyz = colour_xyz, transformation = "Adobe") colour_rgb