加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:40:32 所属栏目:百科 来源:网络整理
导读:所以我试图弄清楚如何计算颜色由HSL值表示的多个对象的平均色调.值得庆幸的是,我偶然发现了 this Stack Overflow post,并开始实现顶部答案中提供的算法(我在C中工作). 不幸的是,我的实现似乎不起作用.在这里,它是完整的;请注意,虽然我写了“Hue”,但我按照初
所以我试图弄清楚如何计算颜色由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.

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

解决方法

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

它现在给出的答案是3.00488.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读