home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!network.ucsd.edu!alex!spl
- From: spl@alex.uucp (Steve Lamont)
- Newsgroups: comp.graphics
- Subject: Re: Color conversion algorithm
- Message-ID: <154289INNs76@network.ucsd.edu>
- Date: 28 Jul 92 18:04:57 GMT
- Article-I.D.: network.154289INNs76
- References: <l7apjnINN2a6@news.bbn.com>
- Organization: University of Calif., San Diego/Microscopy and Imaging Resource
- Lines: 126
- NNTP-Posting-Host: alex.ucsd.edu
-
- In article <l7apjnINN2a6@news.bbn.com> slackey@BBN.COM (Stan Lackey) writes:
- >If anyone has access to an algorithm for converting hue/brighntess/saturation
- >to r/g/b, would you be so kind as to either post it or email me? Thanks much!
-
- #include <stdio.h>
-
- /*
- * Straight out of Foley, van Dam, Feiner, and Hughes.
- */
-
- hsv2rgb( h, s, v, r, g, b )
-
- 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" );
-
- } 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@dim.ucsd.edu
- UCSD Microscopy and Imaging Resource/UCSD Med School/La Jolla, CA 92093-0608
- "Out of the ash/I rise with my red hair/And I eat men like air."
- - Sylvia Plath, "Lady Lazarus"
-