• Stars
    star
    112
  • Rank 301,093 (Top 7 %)
  • Language
    R
  • License
    Other
  • Created about 6 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

High Performance Colourspace Manipulation in R

farver

R-CMD-check CRAN_Status_Badge CRAN_Download_Badge Codecov test coverage

The goal of farver is to provide very fast, vectorised functions for conversion of colours between different colour spaces, colour comparisons (distance between colours), encoding/decoding, and channel manipulation in colour strings. To this end it provides an interface to a modified version of the ColorSpace C++ library developed by Berendea Nicolae.

Installation

farver can be installed from CRAN using install.packages('farver'). The development version can be installed from Github using devtools:

# install.packages('devtools')
devtools::install_github('thomasp85/farver')

Use

farver provides an alternative to the grDevices::rgb() and grDevices::col2rgb() for encoding and decoding colours strings. The farver functions are superficially equivalent but provides a uniform output format, and the option to encode and decode directly from/to other colour spaces.

library(farver)

codes <- rainbow(10)
codes
#>  [1] "#FF0000" "#FF9900" "#CCFF00" "#33FF00" "#00FF66" "#00FFFF" "#0066FF"
#>  [8] "#3300FF" "#CC00FF" "#FF0099"

spectrum <- decode_colour(codes)
spectrum
#>         r   g   b
#>  [1,] 255   0   0
#>  [2,] 255 153   0
#>  [3,] 204 255   0
#>  [4,]  51 255   0
#>  [5,]   0 255 102
#>  [6,]   0 255 255
#>  [7,]   0 102 255
#>  [8,]  51   0 255
#>  [9,] 204   0 255
#> [10,] 255   0 153

encode_colour(spectrum)
#>  [1] "#FF0000" "#FF9900" "#CCFF00" "#33FF00" "#00FF66" "#00FFFF" "#0066FF"
#>  [8] "#3300FF" "#CC00FF" "#FF0099"

It also provides an alternative to grDevices::convertColor() to switch between colours spaces. If the origin is a colour string it is possible to decode directly into the given colour space. Conversely, if the endpoint is a colour string it is also possible to encode directly from a given colour space.

spectrum_lab <- convert_colour(spectrum, 'rgb', 'lab')
spectrum_lab
#>              l         a           b
#>  [1,] 53.24079  80.09246   67.203197
#>  [2,] 72.26072  30.16539   77.224482
#>  [3,] 93.60533 -41.94504   90.274226
#>  [4,] 88.07403 -83.10813   83.593379
#>  [5,] 88.19634 -80.27943   57.926987
#>  [6,] 91.11322 -48.08753  -14.131186
#>  [7,] 47.90478  35.19678  -82.006104
#>  [8,] 33.81896  79.70044 -105.279006
#>  [9,] 51.90416  90.99470  -74.834222
#> [10,] 55.65103  86.52861   -9.719051

decode_colour(codes, to = 'lab')
#>              l         a           b
#>  [1,] 53.24079  80.09246   67.203197
#>  [2,] 72.26072  30.16539   77.224482
#>  [3,] 93.60533 -41.94504   90.274226
#>  [4,] 88.07403 -83.10813   83.593379
#>  [5,] 88.19634 -80.27943   57.926987
#>  [6,] 91.11322 -48.08753  -14.131186
#>  [7,] 47.90478  35.19678  -82.006104
#>  [8,] 33.81896  79.70044 -105.279006
#>  [9,] 51.90416  90.99470  -74.834222
#> [10,] 55.65103  86.52861   -9.719051

encode_colour(spectrum_lab, from = 'lab')
#>  [1] "#FF0000" "#FF9900" "#CCFF00" "#33FF00" "#00FF66" "#00FFFF" "#0066FF"
#>  [8] "#3300FF" "#CC00FF" "#FF0099"

If colours are given as strings, manipulation of channels will normally require decoding, conversion to the correct colour space, manipulation of the given channel, converting back to rgb and the encoding to string. farver provides a range of functions that allow you to change any channel in the supported spaces directly in colour strings:

# Add a value to the channel
add_to_channel(codes, channel = 'l', value = 1:10, space = 'lab')
#>  [1] "#FF0C03" "#FF9E0E" "#D5FF1C" "#48FF20" "#33FF74" "#3CFFFF" "#3D77FF"
#>  [8] "#5A25FF" "#E839FF" "#FF41B4"

# Set a channel to a specific value
set_channel(codes, 'alpha', c(0.3, 0.7))
#>  [1] "#FF00004C" "#FF9900B2" "#CCFF004C" "#33FF00B2" "#00FF664C" "#00FFFFB2"
#>  [7] "#0066FF4C" "#3300FFB2" "#CC00FF4C" "#FF0099B2"

# Limit a channel to a given value
cap_channel(codes, 'r', 200)
#>  [1] "#C80000" "#C89900" "#C8FF00" "#33FF00" "#00FF66" "#00FFFF" "#0066FF"
#>  [8] "#3300FF" "#C800FF" "#C80099"

Lastly, farver also provides utilities for calculating the distance between colours, based on a range of different measures

spectrum2 <- t(col2rgb(heat.colors(10)))

compare_colour(spectrum, spectrum2, 'rgb', method = 'cie2000')[1:6, 1:6]
#>          [,1]     [,2]      [,3]     [,4]     [,5]     [,6]
#> [1,]  0.00000  1.95065  7.130898 15.53837 27.08237 39.88958
#> [2,] 29.50083 27.56585 22.402612 13.98117  2.41602 10.31341
#> [3,] 72.33606 70.32974 64.926436 55.98592 43.59987 30.24747
#> [4,] 85.84698 83.68842 77.854648 68.19997 55.06314 41.59064
#> [5,] 85.92110 83.79762 78.073545 68.67184 56.07682 43.42965
#> [6,] 70.95853 69.55274 65.907013 60.35739 53.72218 47.94387

Supported colour spaces

farver currently supports the following colour spaces:

  • CMY
  • CMYK
  • HSL
  • HSB
  • HSV
  • CIE L*AB
  • Hunter LAB
  • OK LAB
  • LCH(ab)
  • LCH(uv)
  • LCH(OK)
  • LUV
  • RGB
  • XYZ
  • YXY

Supported distance measures

farver supports the following colour distance metrics

  • Euclidean
  • CIE1976
  • CIE94
  • CIE2000
  • CMC

White References

farver allows you to set the white point for relative colour spaces, either based on a standard illuminant (A-F series supported) or by specifying chromaticity coordinates or tristimulus values directly

Benchmark

farver is faster than its grDevices counterpart but less so than it was at its first release, as the colour conversion in grDevices has been improved since.

library(ggplot2)
test <- matrix(runif(300000, min = 0, max = 255), ncol = 3)
timing <- bench::mark(
  farver = convert_colour(test, 'rgb', 'lab'),
  grDevices = convertColor(test, 'sRGB', 'Lab', scale.in = 255), 
  check = FALSE,
  min_iterations = 100
)
#> Warning: Some expressions had a GC in every iteration; so filtering is disabled.
plot(timing, type = 'ridge')

Still, if the start- and/or endpoint are colour strings the ability to decode and encode directly from/to any colour space will give a huge speed up.

colour_strings <- colours()
timing <- bench::mark(
  farver = decode_colour(colour_strings, to = 'lab'),
  grDevices = convertColor(t(col2rgb(colour_strings)), 'sRGB', 'Lab', scale.in = 255), 
  check = FALSE,
  min_iterations = 100
)
plot(timing, type = 'ridge')

Code of Conduct

Please note that the β€˜farver’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

More Repositories

1

patchwork

The Composer of ggplots
R
2,354
star
2

gganimate

A Grammar of Animated Graphics
R
1,920
star
3

ggraph

Grammar of Graph Graphics
R
1,032
star
4

ggforce

Accelerating ggplot2
R
898
star
5

tidygraph

A tidy API for graph manipulation
R
529
star
6

ggplot2_workshop

Material for "Drawing Anything with ggplot2" workshop
481
star
7

lime

Local Interpretable Model-Agnostic Explanations (R port of original Python package)
R
478
star
8

scico

Palettes for R based on the Scientific Colour-Maps
R
401
star
9

tweenr

Interpolate your data
R
395
star
10

fiery

A flexible and lightweight web server
R
239
star
11

shinyFiles

A shiny extension for server side file access
JavaScript
188
star
12

ggfx

Filters and Shaders for 'ggplot2'
R
161
star
13

densityClust

Clustering by fast search and find of density peaks
R
149
star
14

particles

A particle simulation engine based on a port of d3-force
R
118
star
15

transformr

Smooth Polygon Transformations
R
116
star
16

ambient

A Generator of Multidimensional Noise
R
91
star
17

euclid

Exact Computation Geometry Framework Based on 'CGAL'
C++
82
star
18

hierarchicalSets

Scalable Set Visualization using Hierarchies
R
54
star
19

Hr

Easy Access to Uppercase H
R
53
star
20

routr

Routing of Web Requests in R
R
53
star
21

reqres

Powerful classes for http requests and responses
R
36
star
22

curry

Partial Function Application with %<%, %-<%, and %><%
R
30
star
23

FindMyFriends

Fast alignment-free pangenome creation and exploration
R
27
star
24

pipeplotter

Syntactic ggplot2 Sugar for a Tidy World
R
26
star
25

fawkes

An R Interface to the AxiDraw plotter
R
25
star
26

pearls

Operations on Lists of Data Frames
R
18
star
27

ink

The Modern, High-Performant, Graphic Device for R
C++
17
star
28

PanVizGenerator

Create your own PanViz visualizations
R
17
star
29

ggplot2_mechanics

The Mechanics of ggplot2
TeX
16
star
30

plotting_benchmark

Investigating R graphics performance
HTML
16
star
31

grid

personal devel version of grid
R
15
star
32

PanViz

D3 based visualisation for comparative genomics
JavaScript
14
star
33

boundaries

Algorithms for Working With and Modifying Polygon Boundaries
C++
13
star
34

data_imaginist

data_imaginist source
HTML
12
star
35

nanodev

Graphic Devices for R based on NanoVG
C
10
star
36

MSGFgui

A gui overlay and extension for MSGFplus
R
10
star
37

orion

Spatial Searching for Euclid
C++
9
star
38

web_dev_in_R

Web Development for R Users
TeX
9
star
39

heroku-fiery-demo

A demo fiery application for deployment on Heroku
R
8
star
40

marquee

Markdown Parser and Renderer for R Graphics
C
8
star
41

unmeshy

A Vector Based 3D Renderer
C++
7
star
42

MSsary

Mass spectrometry data in R
R
7
star
43

RcppSNAP

'Rcpp' Integration for the SNAP Network Library
C++
6
star
44

tidy_graph_analysis

Tidy Network Analysis in R
TeX
6
star
45

polyclid

Polygon Support for Euclid
C++
6
star
46

mzID

An mzIdentML parser for R
R
6
star
47

MSGFplus

An MSGF+ interface for R
R
6
star
48

thomasp85.github.io

The source for data-imaginist.com
HTML
6
star
49

mvpcran

CRAN on a stick
R
5
star
50

shady

Compile and Execute Shaders from R
C++
5
star
51

anomaly

Detecting those outliers
R
4
star
52

d3Disco

A showcase for Shiny and D3 integration
JavaScript
4
star
53

firedock

Dockerfiles for fiery
R
4
star
54

phd_dissertation

Pangenome Tools for Rapid, Large-Scale Analysis of Bacterial Genomes
TeX
4
star
55

pepmaps

R package for quantitative peptidomics
R
2
star
56

masochist

For some reason I’m doing all of this from my phone
R
2
star
57

firedock_test

R
1
star
58

firesafety

Security for fiery apps
1
star
59

Biotools

Scripts for CMG-Biotools
Perl
1
star
60

circosScripts

Perl scripts to automate circos plots
Perl
1
star
61

CHtools

A list of diverse functions for CH
R
1
star