Skip to content

Instantly share code, notes, and snippets.

@thoolihan
Last active March 30, 2017 12:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoolihan/5a2b4d3fbe3808de93ffc7569f278d04 to your computer and use it in GitHub Desktop.
Save thoolihan/5a2b4d3fbe3808de93ffc7569f278d04 to your computer and use it in GitHub Desktop.
color distance
library(dplyr)
n <- 1000000
data <- data.frame(id = 1:n,
red = sample(0:255, size = n, replace = TRUE),
green = sample(0:255, size = n, replace = TRUE),
blue = sample(255, size = n, replace = TRUE))
query <- list(red = 80, green = 90, blue = 255)
results <- data %>%
mutate(eu_dist = sqrt((red - query$red)^2 +
(blue - query$blue)^2 +
(green - query$green)^2)) %>%
arrange(eu_dist)
print(head(results))
# output
# > print(head(results))
# id red green blue eu_dist
# 1 850076 81 91 255 1.414214
# 2 245256 81 88 255 2.236068
# 3 255701 79 89 253 2.449490
# 4 864183 78 89 254 2.449490
# 5 126499 78 90 253 2.828427
# 6 904071 79 90 252 3.162278
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment