chroma.js
chroma.js copied to clipboard
Find a colour that is resembles based on an array of colours
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"
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])
}
Will give it a try and report back. Thank you @Artoria2e5