There are many ways to measure the distance between colours. farver provides 5 different algorithms, ranging from simple euclidean distance in RGB space, to different perceptual measures such as CIE2000.

compare_colour(
  from,
  to = NULL,
  from_space,
  to_space = from_space,
  method = "euclidean",
  white_from = "D65",
  white_to = white_from,
  lightness = 2,
  chroma = 1
)

Arguments

from, to

Numeric matrices with colours to compare - the format is the same as that for convert_colour(). If to is not set from will be compared with itself and only the upper triangle will get calculated

from_space, to_space

The colour space of from and to respectively. to_space defaults to be the same as from_space.

method

The method to use for comparison. Either 'euclidean', 'cie1976', 'cie94', 'cie2000', or 'cmc'

white_from, white_to

The white reference of the from and to colour space. Will only have an effect for relative colour spaces such as Lab and luv. Any value accepted by as_white_ref() allowed.

lightness, chroma

Weight of lightness vs chroma when using CMC. Common values are 2 and 1 (default) for acceptability and 1 and 1 for imperceptibility

Value

A numeric matrix with the same number of rows as colours in from

and the same number of columns as colours in to. If to is not given, only the upper triangle will be returned.

Handling of non-finite and out of bounds values

NA, NaN, -Inf, and Inf are treated as invalid input and will result in NA values for the colour. If a given colourspace has finite bounds in some of their channels, the input will be capped before conversion, and the output will be capped before returning, so that both input and output colours are valid colours in their respective space. This means that converting back and forth between two colourspaces may result in a change in the colour if the gamut of one of the spaces is less than the other.

Examples

r <- decode_colour(rainbow(10))
h <- decode_colour(heat.colors(15))

# Compare two sets of colours
compare_colour(r, h, 'rgb', method = 'cie2000')
#>           [,1]       [,2]      [,3]     [,4]     [,5]     [,6]      [,7]
#>  [1,]  0.00000  0.9533762  3.000824  6.57770 11.40975 17.52601 24.723573
#>  [2,] 29.50083 28.5558177 26.521813 22.95539 18.11996 11.98805  4.775709
#>  [3,] 72.33606 71.3575538 69.242820 65.50814 60.39647 53.85364 46.123120
#>  [4,] 85.84698 84.7949077 82.516916 78.48359 72.95656 65.91025 57.700879
#>  [5,] 85.92110 84.8858340 82.646223 78.68939 73.29065 66.45879 58.585358
#>  [6,] 70.95853 70.2695051 68.802658 66.28933 63.01735 59.12704 54.973704
#>  [7,] 50.38091 50.6204430 51.184821 52.32043 54.12124 56.78372 60.394557
#>  [8,] 52.15698 52.7007613 53.923828 56.23309 59.66383 64.36086 69.987595
#>  [9,] 44.47475 44.9165073 45.932292 47.90510 50.91157 55.17495 60.707919
#> [10,] 30.89123 31.3674680 32.487084 34.71591 38.15422 42.98987 49.095286
#>           [,8]     [,9]    [,10]    [,11]      [,12]      [,13]    [,14]
#>  [1,] 32.67872 40.97546 49.52951 57.25740  64.299474  63.895520 60.57729
#>  [2,]  3.16786 11.38266 19.71938 27.08742  33.646908  33.488301 32.47947
#>  [3,] 37.67285 29.15780 20.92325 14.21750   9.325976   9.059491 10.49557
#>  [4,] 48.97226 40.53408 32.80674 26.89319  22.660408  22.341120 21.56970
#>  [5,] 50.32473 42.44960 35.33751 29.96925  26.166094  25.707067 23.60073
#>  [6,] 50.98220 47.53531 44.77211 42.98433  41.972721  41.436516 37.40409
#>  [7,] 64.89992 70.06575 75.76079 81.12373  86.129806  85.536298 80.31417
#>  [8,] 76.18219 82.84641 89.96539 96.53168 102.567205 101.830677 95.66170
#>  [9,] 67.30314 74.52107 82.12052 88.99246  95.209202  94.367152 87.31037
#> [10,] 56.09295 63.45764 70.99828 77.74381  83.865025  83.098194 76.75948
#>          [,15]
#>  [1,] 52.36649
#>  [2,] 32.00440
#>  [3,] 21.49275
#>  [4,] 26.48901
#>  [5,] 25.76501
#>  [6,] 28.94258
#>  [7,] 64.93634
#>  [8,] 78.99746
#>  [9,] 67.87546
#> [10,] 59.84362

# Compare a set of colours with itself
compare_colour(r, from_space = 'rgb', method = 'cmc')
#>       [,1]     [,2]     [,3]      [,4]       [,5]     [,6]      [,7]      [,8]
#>  [1,]    0 30.66001 79.59275 103.37370 101.509242 91.48686  97.00427 107.02585
#>  [2,]    0  0.00000 50.42953  75.88074  77.339032 81.22327 110.77720 128.40117
#>  [3,]    0  0.00000  0.00000  16.64463  21.009428 41.12636  80.26399  97.81056
#>  [4,]    0  0.00000  0.00000   0.00000   8.359694 34.63378  76.14760  93.97196
#>  [5,]    0  0.00000  0.00000   0.00000   0.000000 28.07909  71.85226  90.44183
#>  [6,]    0  0.00000  0.00000   0.00000   0.000000  0.00000  55.60517  78.29659
#>  [7,]    0  0.00000  0.00000   0.00000   0.000000  0.00000   0.00000  19.91406
#>  [8,]    0  0.00000  0.00000   0.00000   0.000000  0.00000   0.00000   0.00000
#>  [9,]    0  0.00000  0.00000   0.00000   0.000000  0.00000   0.00000   0.00000
#> [10,]    0  0.00000  0.00000   0.00000   0.000000  0.00000   0.00000   0.00000
#>            [,9]    [,10]
#>  [1,]  88.69588 47.25557
#>  [2,] 111.75460 72.07381
#>  [3,]  89.44199 69.07941
#>  [4,]  87.83719 72.00177
#>  [5,]  85.01006 70.79239
#>  [6,]  75.70598 69.06744
#>  [7,]  26.36408 45.50214
#>  [8,]  16.45858 39.30074
#>  [9,]   0.00000 24.97282
#> [10,]   0.00000  0.00000

# Compare colours from different colour spaces
h_luv <- convert_colour(h, 'rgb', 'luv')
compare_colour(r, h_luv, 'rgb', 'luv')
#>            [,1]      [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#>  [1,] 269.75047 266.98290 261.3195 252.2761 241.5891 230.1999 219.2811 209.6951
#>  [2,] 206.44011 205.64789 204.2222 202.5385 201.6224 202.1931 204.6754 209.0740
#>  [3,] 174.78715 176.24962 179.5202 185.5461 194.0407 204.9786 217.7871 231.7625
#>  [4,]  88.47164  92.42388 100.6683 114.3188 131.3838 151.0651 172.0906 193.4469
#>  [5,] 115.57637 117.88566 122.9627 132.0420 144.3498 159.5508 176.6664 194.7251
#>  [6,] 237.53853 237.90584 238.8600 240.9830 244.5478 249.8338 256.7524 264.9852
#>  [7,] 235.28781 233.18662 228.9612 222.4461 215.1867 208.1223 202.2425 198.1545
#>  [8,] 278.98265 275.69390 268.8986 257.8385 244.3546 229.3136 213.9318 199.1665
#>  [9,] 317.10351 313.89311 307.2456 296.3769 283.0180 267.9184 252.1564 236.5575
#> [10,] 290.89512 287.69780 281.0874 270.3128 257.1382 242.3644 227.1189 212.2718
#>           [,9]    [,10]    [,11]    [,12]    [,13]    [,14]    [,15]
#>  [1,] 201.9461 196.0358 192.4455 190.7461 188.9878 176.3276 158.9800
#>  [2,] 215.1081 222.7265 230.9081 239.6480 238.3923 229.6493 219.0854
#>  [3,] 246.2732 261.4841 275.8258 289.7788 288.8310 282.3895 275.4135
#>  [4,] 214.4548 235.5892 254.9104 273.2789 272.3119 265.7935 259.1322
#>  [5,] 212.9741 231.6944 249.0506 265.7198 265.9122 268.6680 280.6467
#>  [6,] 274.1305 284.2483 294.2037 304.2199 305.9190 320.0701 352.0550
#>  [7,] 196.0771 195.9797 197.6374 200.7253 203.1255 222.6780 264.5882
#>  [8,] 185.6644 173.3355 163.4112 155.4158 158.3398 181.6162 229.2526
#>  [9,] 221.6549 207.1638 194.4319 182.8782 185.3136 205.1393 247.5072
#> [10,] 198.3929 185.2913 174.2208 164.6657 165.4737 173.6139 198.0978