rgb转化为hsv的C++程序代码
#include "stdafx.h"
#include
void main(float R, float G, float B, float& H, float& S, float&V)
{
// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
float min, max, delta,tmp;
tmp = R>G?G:R;
min = tmp>B?B:tmp;
tmp = R>G?R:G;
max = tmp>B?tmp:B;
V = max; // v
delta = max - min;
if( max != 0 )
S = delta / max; // s
else
{
// r = g = b = 0 // s = 0, v is undefined
S = 0;
H = 0;
return;
}
if (delta == 0){
H = 0;
return;
}
else if(R == max){
if (G >= B)
H = (G - B) / delta; // between yellow & magenta
else
H = (G - B) / delta + 6;
}
else if( G == max )
H = 2 + ( B - R ) / delta; // between cyan & yellow
else if (B == max)
H = 4 + ( R - G ) / delta; // between magenta & cyan
H *= 60; // degrees
}
1