如何近似颜色

How to approximate colors

我们有一个这样的列表 https://htmlcolorcodes.com/color-names/,其中列出了一些颜色。我想要一个函数来游戏一些随机的十六进制颜色值并将其近似为那些命名颜色之一。我怎样才能做到这一点?

当我理解正确时,您想要一个函数:

  1. 生成随机颜色

  2. 在链表中寻找最接近的颜色

是否正确? 如果是这样,我会建议使用 RGB 值,然后我会创建自己的指标来测量两种颜色之间的距离,例如:

int distance(Color a, Color b) {
    int redDistance = math.abs( a.red - b.red );
    int greenDistance = math.abs( a.green - b.green );
    int blueDistance = math.abs( a.blue - b.blue );

    return redDistance + greenDistance + blueDistance;
}

之后我将遍历所有链接的颜色并找到随机生成的“最近”颜色。