Gar*_*ckW 6 c++ math hsl colors
所以我试图弄清楚如何计算颜色由HSL值表示的多个对象的平均色调.值得庆幸的是,我偶然发现了这个Stack Overflow帖子,并开始实现顶部答案中提供的算法(我在C++中工作).
不幸的是,我的实现似乎不起作用.在这里,它是完整的; 请注意,虽然我写了"Hue",但我按照初始实现使用角度(以度为单位)(从0-360角度切换到0-256色调,一旦我知道我的代码有效,就不应该很难).
#include <iostream>
#include <vector>
#include <cmath>
#define PI (4*atan(1))
int main()
{
///
/// Calculations adapted from this source:
/// https://stackoverflow.com/questions/8169654/how-to-calculate-mean-and-standard-deviation-for-hue-values-from-0-to-360
std::vector<double> Hues = {355, 5, 5, 5, 5};
//These will be used to store the sum of the angles
double X = 0.0;
double Y = 0.0;
//Loop through all H values
for (int hue = 0; hue < Hues.size(); ++hue)
{
//Add the X and Y values to the sum X and Y
X += cos(Hues[hue] / 180 * PI);
Y += sin(Hues[hue] / 180 * PI);
}
//Now average the X and Y values
X /= Hues.size();
Y /= Hues.size();
//Get atan2 of those
double AverageColor = atan2(X, Y) * 180 / PI;
std::cout << "Average: " << AverageColor << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而不是3的预期答案(因为355在这个方案中应该相当于-5),我得到86.9951.
有人可以指出我做错了什么吗?这看起来非常基本.
atan2
以相反的顺序获取其参数.我知道,很烦人!所以尝试:
double AverageColor = atan2(Y, X) * 180 / PI;
Run Code Online (Sandbox Code Playgroud)
它现在给出的答案是3.00488.