chroma.js icon indicating copy to clipboard operation
chroma.js copied to clipboard

Find a colour that is resembles based on an array of colours

Open lonelydatum opened this issue 5 years ago • 2 comments

Say I have the colour "red" and an array of colours ["black", "white", "burgundy"]. Is it possible to do this:

const resembles = chroma.resembles("red", ["black", "white", "burgundy"] ) // resembles === "burgundy"

lonelydatum avatar Feb 27 '19 18:02 lonelydatum

Isn't that just

function closest(needle, haystack) {
  return haystack
  .map((straw) => [chroma.distance(needle, straw), straw]) // become [distance, color]
  .sort((a, b) => a[0]-b[0]))[0][1] // pick first (closest), pick the color part ([1])
}

Artoria2e5 avatar Aug 15 '19 00:08 Artoria2e5

Will give it a try and report back. Thank you @Artoria2e5

lonelydatum avatar Aug 15 '19 03:08 lonelydatum