C ++ RGB到HSV转换

时间:2018-07-01 13:53:11

标签: c++ colors rgb hsv

我正在尝试编写将RGB颜色空间转换为HSV空间的程序。它基于转换algorithm described here。我搜索的大多数地点都有类似的转化算法。

my input to the program: 67 42 96
output: 267.778 , 0.5625, 96
                          ^^^^^ // it should be 38 as below
expected output: 268 56% 38%
                          ^^^^^^ 

对于其他输入,我也可以看到HS的值是应该的,但是V的值是不同的。可能是什么原因?

#include<iostream>
#include <algorithm>    // std::max

using namespace std;


 void rgb2hsv(float r, float g, float b) {

     float h = 0.0;
     float s = 0.0;
     float v = 0.0;

     float min = std::min( std::min(r, g), b );
     float max = std::max( std::max(r, g), b );
     v = max;               // v

     float delta = max - min;

     if( max != 0.0 )
         s = delta / max;       // s
     else {
         // r = g = b = 0       // s = 0, v is undefined
         s = 0.0;
         h = -1.0;
         cout<<h<<" , "<<s<<" , "<<v<<endl;
     }
     if( r == max )
         h = ( g - b ) / delta;     // between yellow & magenta
     else if( g == max )
         h = 2.0 + ( b - r ) / delta;   // between cyan & yellow
     else
         h = 4.0 + ( r - g ) / delta;   // between magenta & cyan

     h = h * 60.0;              // degrees

     if( h < 0.0 )
         h += 360.0;

     cout<<h<<" , "<<s<<" , "<<v<<endl;
 }


int main(){
while(1){
float r,g,b;

cin>>r>>g>>b;

rgb2hsv(r,g,b);
}

return 0;
}

0 个答案:

没有答案