MCPcopy Create free account
hub / github.com/adobe/react-spectrum / colorDistance

Function colorDistance

packages/dev/s2-docs/src/searchUtils.tsx:70–82  ·  view source on GitHub ↗

* Calculates the color distance between two RGB colors using weighted Euclidean distance. * Uses weights that better approximate human color perception. * Reference: https://en.wikipedia.org/wiki/Color_difference.

(rgb1: [number, number, number], rgb2: [number, number, number])

Source from the content-addressed store, hash-verified

68 * Reference: https://en.wikipedia.org/wiki/Color_difference.
69 */
70function colorDistance(rgb1: [number, number, number], rgb2: [number, number, number]): number {
71 const [r1, g1, b1] = rgb1;
72 const [r2, g2, b2] = rgb2;
73 const rMean = (r1 + r2) / 2;
74 const dr = r1 - r2;
75 const dg = g1 - g2;
76 const db = b1 - b2;
77 const weightR = 2 + rMean / 256;
78 const weightG = 4;
79 const weightB = 2 + (255 - rMean) / 256;
80
81 return Math.sqrt(weightR * dr * dr + weightG * dg * dg + weightB * db * db);
82}
83
84/**
85 * Finds the closest colors to a given hex code from the color map.

Callers 1

findClosestColorsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected