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

Function findClosestColors

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

* Finds the closest colors to a given hex code from the color map. * Returns an array of color names sorted by distance (closest first).

(
  targetRgb: [number, number, number],
  colorHexMap: Record<string, [number, number, number]>,
  maxResults = 10
)

Source from the content-addressed store, hash-verified

86 * Returns an array of color names sorted by distance (closest first).
87 */
88function findClosestColors(
89 targetRgb: [number, number, number],
90 colorHexMap: Record<string, [number, number, number]>,
91 maxResults = 10
92): Array<{name: string; distance: number}> {
93 const results: Array<{name: string; distance: number}> = [];
94
95 for (const [name, rgb] of Object.entries(colorHexMap)) {
96 const distance = colorDistance(targetRgb, rgb as [number, number, number]);
97 results.push({name, distance});
98 }
99
100 // Sort by distance and return top results
101 return results.sort((a, b) => a.distance - b.distance).slice(0, maxResults);
102}
103
104export interface SearchableItem {
105 name: string;

Callers 1

useFilteredColorsFunction · 0.85

Calls 4

colorDistanceFunction · 0.85
pushMethod · 0.80
sliceMethod · 0.80
sortMethod · 0.65

Tested by

no test coverage detected