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
)

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.

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 10.52597 18.003563 15.958871 15.623504 23.06674 5.030710 11.74396 #> [2,] 0 0.00000 9.732855 12.581267 7.948242 12.62015 9.568638 21.39777 #> [3,] 0 0.00000 0.000000 5.729119 1.877060 14.58716 16.089339 22.78038 #> [4,] 0 0.00000 0.000000 0.000000 5.253523 18.88463 16.297394 19.60278 #> [5,] 0 0.00000 0.000000 0.000000 0.000000 14.46503 14.540410 21.56755 #> [6,] 0 0.00000 0.000000 0.000000 0.000000 0.00000 21.444871 37.66022 #> [7,] 0 0.00000 0.000000 0.000000 0.000000 0.00000 0.000000 14.69552 #> [8,] 0 0.00000 0.000000 0.000000 0.000000 0.00000 0.000000 0.00000 #> [9,] 0 0.00000 0.000000 0.000000 0.000000 0.00000 0.000000 0.00000 #> [10,] 0 0.00000 0.000000 0.000000 0.000000 0.00000 0.000000 0.00000 #> [,9] [,10] #> [1,] 3.885606 5.174003 #> [2,] 13.496209 6.515862 #> [3,] 15.389917 13.626292 #> [4,] 12.801400 14.321552 #> [5,] 13.988362 12.037620 #> [6,] 29.707650 18.989681 #> [7,] 8.957990 3.701201 #> [8,] 11.098901 17.436529 #> [9,] 0.000000 8.718964 #> [10,] 0.000000 0.000000
# 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