RGB 到 HSB 转换的 C++ 程序出错

Error in C++ Program for RGB to HSB conversion

我写了下面的代码,这对我来说似乎很好,但我遇到了编译错误:

error: invalid operands of types 'double' and 'int' to binary 

我的代码:

#include <iostream>
#include <cmath>
using namespace std;

class ColourChecking
{
private:
    double r, g, b;
public:
    void hsb(double a, double c, double d)
    {
        r = a/255.0; g = c/255.0; b = d/255.0;
        double cmax = max(r, max(g, b));
        double cmin = min(r, min(g, b));
        double diff = cmax - cmin;
        double h, s, b;
        // Hue Calculation
        if (cmax == cmin)
            h = 0;
        else if (cmax == r)
            h = (60 * ((g-b) / diff) + 360) % 360;
        else if (cmax == g)
            h = (60 * ((b-r) / diff) + 120) % 360;
        else if (cmax == b)
            h = (60 * ((r-g) / diff) + 240) % 360;

        // Saturation Calculation
        if (cmax == 0)
            s = 0;
        else
            s = (diff / cmax) * 100.0;

        // Brightness Calculation
        b = cmax * 100;

        cout << "Hue: " << h << endl;
        cout << "Saturation: " << s << endl;
        cout << "Brightness: " << b << endl;
    }

};

int main()
{
    ColourChecking Calculate;
//    Calculate.hsb(31, 52, 29);
    Calculate.hsb(45, 215, 0);
    return 0;
}

如何解决这个错误?

当我将“%”运算符更改为“/”运算符时,饱和度和亮度值是正确的, 但是 h 的值是错误的。

不能对浮点变量使用取模运算符。模和除法也不等价。

您可以使用 math.h 中的 fmod 函数:

#include <math.h>
h = std::fmod(60 * ((g-b) / diff) + 360, 360);