Created
December 16, 2019 18:24
-
-
Save cybertramp/5cd8b8897b5faca46affb631c50adf74 to your computer and use it in GitHub Desktop.
RGB_to_HSV C code
This file contains hidden or 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
| // 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