c – 平均圆形值(特别是HSL颜色方案中的色调)

前端之家收集整理的这篇文章主要介绍了c – 平均圆形值(特别是HSL颜色方案中的色调)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我试图弄清楚如何计算颜色由HSL值表示的多个对象的平均色调.值得庆幸的是,我偶然发现了 this Stack Overflow post,并开始实现顶部答案中提供的算法(我在C中工作).

不幸的是,我的实现似乎不起作用.在这里,它是完整的;请注意,虽然我写了“Hue”,但我按照初始实现使用角度(以度为单位)(从0-360角度切换到0-256色调,一旦我知道我的代码有效,就不应该很难).

  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. #define PI (4*atan(1))
  6.  
  7. int main()
  8. {
  9. ///
  10. /// Calculations adapted from this source:
  11. /// https://stackoverflow.com/questions/8169654/how-to-calculate-mean-and-standard-deviation-for-hue-values-from-0-to-360
  12.  
  13. std::vector<double> Hues = {355,5,5};
  14.  
  15. //These will be used to store the sum of the angles
  16. double X = 0.0;
  17. double Y = 0.0;
  18.  
  19. //Loop through all H values
  20. for (int hue = 0; hue < Hues.size(); ++hue)
  21. {
  22. //Add the X and Y values to the sum X and Y
  23. X += cos(Hues[hue] / 180 * PI);
  24. Y += sin(Hues[hue] / 180 * PI);
  25. }
  26.  
  27. //Now average the X and Y values
  28. X /= Hues.size();
  29. Y /= Hues.size();
  30.  
  31. //Get atan2 of those
  32. double AverageColor = atan2(X,Y) * 180 / PI;
  33.  
  34. std::cout << "Average: " << AverageColor << "\n";
  35. return 0;
  36. }

而不是3的预期答案(因为355在这个方案中应该相当于-5),我得到86.9951.

有人可以指出我做错了什么吗?这看起来非常基本.

解决方法

atan2以相反的顺序获取其参数.我知道,很烦人!所以尝试:
  1. double AverageColor = atan2(Y,X) * 180 / PI;

它现在给出的答案是3.00488.

猜你在找的C&C++相关文章