Skip to content

Instantly share code, notes, and snippets.

@cybertramp
Created December 16, 2019 18:24
Show Gist options
  • Select an option

  • Save cybertramp/5cd8b8897b5faca46affb631c50adf74 to your computer and use it in GitHub Desktop.

Select an option

Save cybertramp/5cd8b8897b5faca46affb631c50adf74 to your computer and use it in GitHub Desktop.
RGB_to_HSV C code
// r, g, b 값은 0 ~ 1 사이
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v )
{
float min, max, delta;
min = MIN( r, g, b );
max = MAX( r, g, 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 = -1;
return;
}
if( r == max )
*h = ( g - b ) / delta; // between yellow & magenta
else if( g == max )
*h = 2 + ( b - r ) / delta; // between cyan & yellow
else
*h = 4 + ( r - g ) / delta; // between magenta & cyan
*h *= 60; // degrees
if( *h < 0 )
*h += 360;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment