Skip to content

Commit

Permalink
move some code out of find_similar_colour()
Browse files Browse the repository at this point in the history
  • Loading branch information
stibu81 committed Jun 18, 2024
1 parent 80734ec commit af3dcce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ibawds
Type: Package
Title: Functions and Datasets for the Data Science Course at IBAW
Version: 0.6.0.9001
Version: 0.6.0.9002
Author: Stefan Lanz
Maintainer: Stefan Lanz <slanz1137@gmail.com>
Description: A collection of useful functions and datasets for the Data Science
Expand Down
20 changes: 14 additions & 6 deletions R/find_similar_colour.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ find_similar_colour <- function(colour,
cols_rgb <- grDevices::col2rgb(cols)

# compute the distances and find the most similar colour
if (distance == "euclidean") {
dist <- apply(cols_rgb, 2, function(x) sum((x - col_rgb)^2))
} else {
dist <- apply(cols_rgb, 2, function(x) sum(abs(x - col_rgb)))
}
i_min <- which.min(dist)
i_min <- which_min_dist(col_rgb, cols_rgb, distance)
sim_col <- cols[i_min]

if (verbose) {
Expand All @@ -84,3 +79,16 @@ find_similar_colour <- function(colour,
sim_col

}


# compute the distance between x and each value of y

which_min_dist <- function(x, y, method) {
dist_fun <- if (method == "euclidean") {
function(z) sum((x - z)^2)
} else {
function(z) sum(abs(x - z))
}
apply(y, 2, dist_fun) %>%
which.min()
}

0 comments on commit af3dcce

Please sign in to comment.