c# finding similar colors

后端 未结 6 802
滥情空心
滥情空心 2021-01-05 16:39

I want to call a method with argument color. But there are a lot of colors which differ only by a shade. How can I find the colors which differ from my color only by little,

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 17:11

    I found this routine here:

    Color nearest_color = Color.Empty;
    foreach (object o in WebColors)
    {
        // compute the Euclidean distance between the two colors
        // note, that the alpha-component is not used in this example
        dbl_test_red = Math.Pow(Convert.ToDouble(((Color)o).R) - dbl_input_red, 2.0);
        dbl_test_green = Math.Pow(Convert.ToDouble
            (((Color)o).G) - dbl_input_green, 2.0);
        dbl_test_blue = Math.Pow(Convert.ToDouble
            (((Color)o).B) - dbl_input_blue, 2.0);
    
        temp = Math.Sqrt(dbl_test_blue + dbl_test_green + dbl_test_red);
        // explore the result and store the nearest color
        if(temp == 0.0)
        {
            nearest_color = (Color)o;
            break;
        }
        else if (temp < distance)
        {
            distance = temp;
            nearest_color = (Color)o;
        }
    }
    

提交回复
热议问题