home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!spool.mu.edu!agate!netsys!news.cerf.net!network.ucsd.edu!ivem.ucsd.edu!spl
- From: spl@ivem.ucsd.edu (Steve Lamont)
- Newsgroups: comp.graphics
- Subject: Re: RGB<->HSV conversion
- Date: 12 Jan 1993 15:17:51 GMT
- Organization: University of Calif., San Diego/Microscopy and Imaging Resource
- Lines: 134
- Message-ID: <1iunevINNi3f@network.ucsd.edu>
- References: <C0ox1H.2K9@lysator.liu.se> <C0qx5u.G8M@lysator.liu.se>
- NNTP-Posting-Host: ivem.ucsd.edu
-
- In article <C0qx5u.G8M@lysator.liu.se> marvil@lysator.liu.se (Martin Vilcans) writes:
- >I'd like to know how to convert from/to RGB (Red Green Blue) colour
- >coordinates to/from HSV (Hue Saturation Value) coordinates, as I'm
- >working on a picture processing program. Any pointers appreciated,
- >C code will do, but I prefer the algorithm. Thanks.
-
- See Foley, van Dam, Feiner, and Hughes, _Computer Graphics: Principles
- and Practice, Second Edition_, pp 590-593.
-
- Here's my code to convert from HSV to RGB. I've never needed to go
- the other way, so I've never implemented the inverse operation.
-
- - - - chop here - - -
- #include <stdio.h>
-
- /*
- * Lifted straight from Foley, van Dam, Feiner, and Hughes, _Computer
- * Graphics: Principles and Practice, Second Edition_.
- *
- * h varies between 0 and 360, s and v vary between 0 and 1.
- * returns r, g, and b between 0 and 1.
- */
-
- void hsv2rgb( double h, double s, double v,
- double *r, double *g, double *b )
-
- {
-
- if ( s == 0.0 ) {
-
- if ( h < 0.0 ) {
-
- *r = v;
- *g = v;
- *b = v;
-
- } else {
-
- fprintf( stderr, "hsv2rgb error -- invalid combination.\n" );
- exit( 1 );
-
- }
-
- } else {
-
- int i;
- double f;
- double p;
- double q;
- double t;
-
- h = fmod( h, 360.0 );
- if ( h < 0.0 )
- h += 360.0;
-
- h /= 60.0;
-
- i = floor( h );
- f = h - i;
- p = v * ( 1.0 - s );
- q = v * ( 1.0 - ( s * f ) );
- t = v * ( 1.0 - ( s * ( 1.0 - f ) ) );
-
- switch ( i ) {
-
- case 0: {
-
- *r = v;
- *g = t;
- *b = p;
- break;
-
- }
- case 1: {
-
- *r = q;
- *g = v;
- *b = p;
- break;
-
- }
- case 2: {
-
- *r = p;
- *g = v;
- *b = t;
- break;
-
- }
- case 3: {
-
- *r = p;
- *g = q;
- *b = v;
- break;
-
- }
- case 4: {
-
- *r = t;
- *g = p;
- *b = v;
- break;
-
- }
- case 5: {
-
- *r = v;
- *g = p;
- *b = q;
- break;
-
- }
-
- default: {
-
- fprintf( stderr, "hsv2rgb error: Invalid case! case = %d\n",
- i );
- abort();
-
- }
-
- }
-
- }
-
- return;
-
- }
- --
- Steve Lamont, SciViGuy -- (619) 534-7968 -- spl@szechuan.ucsd.edu
- UCSD Microscopy and Imaging Resource/UCSD Med School/La Jolla, CA 92093-0608
- "Personally, I don't care whether someone is cool enough to quote Doug
- Gwyn--I only care whether Doug Gwyn is cool enough to quote." - Larry Wall
-