* 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])
| 68 | * Reference: https://en.wikipedia.org/wiki/Color_difference. |
| 69 | */ |
| 70 | function 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. |