所以我试图弄清楚如何计算颜色由HSL值表示的多个对象的平均色调.值得庆幸的是,我偶然发现了
this Stack Overflow post,并开始实现顶部答案中提供的算法(我在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}; //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; }
而不是3的预期答案(因为355在这个方案中应该相当于-5),我得到86.9951.
有人可以指出我做错了什么吗?这看起来非常基本.