Last active
August 29, 2015 14:04
-
-
Save dzonesasaki/e06cd19c1887cfd05b9e to your computer and use it in GitHub Desktop.
RGB to HSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Calc_RGB2HSV.c | |
// by Dzone 140725_14:14 | |
#include <stdio.h> | |
void Cacl_RGB2HSV( unsigned int, unsigned int , unsigned int , double *, double *, double *); | |
void Cacl_RGB2HSV( | |
unsigned int uiRed, unsigned int uiGreen, unsigned int uiBlue, | |
double *pdH, double *pdS, double *pdV) | |
{ | |
double dMax,dMin,dDiv; | |
double dBuf=0; | |
double dRed = (double)uiRed/(double)255; | |
double dGreen = (double)uiGreen/(double)255; | |
double dBlue = (double)uiBlue/(double)255; | |
double dGetR,dGetG,dGetB; | |
unsigned int uiFlagGray=0; | |
unsigned int uiMaxIndex=1;// R=1,G=2,B=3 | |
unsigned int uiMinIndex=1;// R=1,G=2,B=3 | |
//------------------------------------ | |
// search max | |
//------------------------------------ | |
dMax=dRed; | |
uiMaxIndex=1; | |
if(dMax<dGreen) | |
{ | |
if(dGreen<dBlue){ | |
dMax=dBlue; | |
uiMaxIndex=3; | |
} | |
else{ | |
dMax=dGreen; | |
uiMaxIndex=2; | |
} | |
} | |
else | |
{ | |
if(dMax<dBlue) | |
{ | |
dMax=dBlue; | |
uiMaxIndex=3; | |
} | |
} | |
//------------------------------------ | |
// search min | |
//------------------------------------ | |
dMin=dRed; | |
uiMinIndex = 1; | |
if(dMin>dGreen) | |
{ | |
if(dGreen>dBlue) | |
{ | |
dMin=dBlue; | |
uiMinIndex = 3; | |
} | |
else | |
{ | |
dMin=dGreen; | |
uiMinIndex = 2; | |
} | |
} | |
else | |
{ | |
if(dMin>dBlue) | |
{ | |
dMin=dBlue; | |
uiMinIndex = 3; | |
} | |
} | |
//------------------------------------ | |
// calc pp | |
//------------------------------------ | |
dDiv = (dMax - dMin); | |
dGetR = (dBlue - dGreen)*60/(dMax - dMin) +180; | |
dGetG = (dRed - dBlue)*60/(dMax - dMin) +300; | |
dGetB = (dGreen - dRed)*60/(dMax - dMin) +60; | |
//------------------------------------ | |
// check gray | |
//------------------------------------ | |
if( (uiRed==uiGreen)*(uiBlue==uiGreen)) | |
{ | |
uiFlagGray=1; | |
*pdH=0; | |
} | |
else | |
{ | |
switch (uiMinIndex) | |
{ | |
case 1: | |
*pdH = dGetR; | |
break; | |
case 2: | |
*pdH = dGetG; | |
break; | |
case 3: | |
*pdH = dGetB; | |
break; | |
default: | |
*pdH=0; | |
break; | |
} | |
} | |
if(*pdH > 360){ | |
*pdH = *pdH - (double)(360*(int)(*pdH / 360)); | |
} | |
if(*pdH < (-360)){ | |
*pdH = *pdH + (double)(360*(int)(*pdH / (-360))); | |
} | |
// *pdS = (dMax - dMin)/1; //cone model | |
*pdS = (dMax - dMin)/dMax; //column model | |
*pdV = dMax; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment