[comp.graphics] Color conversion

milo@ndmath.UUCP (Greg Corson) (10/02/87)

Does anyone have a formulia for converting color from Red-Green-Blue to
hue-saturation-intensity and back again?

Greg Corson
{pur-ee,rutgers,uunet}!iuvax!ndmath!milo

Truett_Lee_Smith@cup.portal.com (10/07/87)

In the "Raster Graphics Handbook", CONRAC Corporation, (Van Nostrand, 1985),
ISBN 0-442-21608-4, Appendix III (Color Programming), pp. 335ff, the HSI-RGB
conversion formulas are given.

I hope this reference is helpful.

Truett Lee Smith
Sunntyvale, CA
<truett@cup.portal.com>

pf@diab.UUCP (Per Fogelstrom) (10/07/87)

In article <337@ndmath.UUCP> milo@ndmath.UUCP (Greg Corson) writes:
>Does anyone have a formulia for converting color from Red-Green-Blue to
>hue-saturation-intensity and back again?

See "BYTE" september 1984, article about selecting colors.
Sorry don't remember the page.

alastair@geovision.UUCP (Alastair Mayer) (10/08/87)

In article <337@ndmath.UUCP> milo@ndmath.UUCP (Greg Corson) writes:
>Does anyone have a formulia for converting color from Red-Green-Blue to
>hue-saturation-intensity and back again?
>
>Greg Corson
>{pur-ee,rutgers,uunet}!iuvax!ndmath!milo


See Foley and Van Dam's _Fundamentals_of_Interactive_Computer_Graphics_
(Addison-Wesley, 1982. ISBN 0-201-14468-9).  The last chapter
discusses color models for raster graphics, and there are listings
(in Pascal) for two procedures to convert between Red-Green-Blue
and Hue-Saturation-Value (pp. 615,616).
-- 
 Alastair JW Mayer     BIX: al
                      UUCP: ...!utzoo!dciem!nrcaer!cognos!geovision!alastair

 "What we really need is a good 5-cent/gram launch vehicle."

saponara@batcomputer.tn.cornell.edu (John Saponara) (10/11/87)

In article <337@ndmath.UUCP> milo@ndmath.UUCP (Greg Corson) writes:
>Does anyone have a formulia for converting color from Red-Green-Blue to
>hue-saturation-intensity and back again?

Attached is a hunk of code from netlib, written by Eric Grosse.  If you don't
know about netlib, check "Communications of the ACM", May 1987, p.403-7.
It's a public domain software distribution point, run for free (however, as
they say, anything free comes with no guarantee).  For more information
(come to think of it, this is easier than checking out the article),
write "send index" to either netlib@anl-mcs.arpa or research!netlib (the
second one is more up to date, I think, due to the person running the arpa
netlib being in Europe for the summer).  Netlib is mostly numerical analysis
stuff, but there are a few bits of interest to graphics people.  My favorite
library is "Polyhedra", which has some 142 polyhedra described by vertices,
faces, edges, and more.

Also, netlib's just started a graphics library: try "send index from graphics".
I think the only contribution so far is my "Standard Procedural Database"
package, which I posted to USENET just before SIGGRAPH '87.  If you missed it
back then, check out netlib now. While I'm on this topic, if anyone has
tested their ray-tracer or hidden-surface hardware using this set of benchmark
environments, I would like to know your results and, of as much interest, your
opinions.  The version on netlib is slightly more up-to-date.

You may have already seen some of these databases, by the way: the
AT&T Pixel Machine people were using three of them (recursive tetrahedron,
recursive spheres, and fractal mountain) at their booth at SIGGRAPH '87 (at
incredibly impressive times).  Also, Jeff Goldsmith has tested these on a
Caltech hypercube.  I'd like to see more results for other algorithms and
hardware.  Final note: the images of the databases will be published in the
November issue of "IEEE Computer Graphics & Applications."

Post-final note: netlib is looking for public domain graphics algorithms (of
fairly reasonable quality):  for those of you who've been around awhile and
collected some good graphics comp.sources, please tell Eric Grosse of netlib
about them. (I'd also be interested - I just got this USENET account two days
ago so am pretty out of touch with what has been going on in this group).

    All for now,

    Eric Haines, 3D/Eye Inc, 410 E. Upland Rd, Ithaca, NY 14850
    work: (607)-257-1381
    [better to write me at hpfcla!hpfcrs!eye!erich@hplabs.HP.COM - the account
    I used to post this msg is a housemate's]

-----------------hsv to rgb C code follows--------------------------
/*   rainbow(h, s, v, r, g, b)
     double h, s, v, *r, *g, *b;

 This routine computes colors suitable for use in color level plots.
 Typically s=v=1 and h varies from 0 (red) to 1 (blue) in
 equally spaced steps.  (h=.5 gives green; 1<h<1.5 gives magenta.)
 To convert for frame buffer, use   R = floor(255.999*pow(*r,1/gamma))  etc.
 To get tables calibrated for other devices or to report complaints,
 contact  Eric Grosse   research!ehg    201-582-5828.
*/

#include <stdio.h>
#include <math.h>
double huettab[] = {
 0.0000, 0.0062, 0.0130, 0.0202, 0.0280, 0.0365, 0.0457, 0.0559, 0.0671, 0.0796,
 0.0936, 0.1095, 0.1275, 0.1482, 0.1806, 0.2113, 0.2393, 0.2652, 0.2892, 0.3119,
 0.3333, 0.3556, 0.3815, 0.4129, 0.4526, 0.5060, 0.5296, 0.5501, 0.5679, 0.5834,
 0.5970, 0.6088, 0.6191, 0.6281, 0.6361, 0.6430, 0.6490, 0.6544, 0.6590, 0.6631,
 0.6667, 0.6713, 0.6763, 0.6815, 0.6873, 0.6937, 0.7009, 0.7092, 0.7190, 0.7308,
 0.7452, 0.7631, 0.7856, 0.8142, 0.8621, 0.9029, 0.9344, 0.9580, 0.9755, 0.9889,
 1.0000
 };


#define	NUM_INTERVALS	7

main(argc,argv)
int argc; char *argv[];
{
    int i ;
    double h,s,v,r,g,b;
    s = v = 1 ;
    for ( i = 0 ; i < NUM_INTERVALS ; ++i ) {
	h = (double) i / (double) ( NUM_INTERVALS - 1 ) ;
	rainbow( h, s, v, &r, &g, &b ) ;
	printf( "%3d: %g %g %g\n", i, r,g,b ) ;
    }
}

  /* computed from the FMC-1 color difference formula */
  /* Barco monitor, max(r,g,b)=1, n=61 magenta,  2 Jan 1986 */

rainbow(h, s, v, r, g, b)
double h, s, v, *r, *g, *b;
{
  int i;
  double modf(), trash;
  h = 60*modf(h/1.5,&trash);
  i = floor(h);
  h = huettab[i] + (huettab[i+1]-huettab[i])*(h-i);
  dhsv2rgb(h,s,v,r,g,b);
}

dhsv2rgb(h, s, v, r, g, b)    /*...hexcone model...*/
double h, s, v, *r, *g, *b;    /* all variables in range [0,1[ */
  /* here, h=.667 gives blue, h=0 or 1 gives red. */
{  /* see Alvy Ray Smith, Color Gamut Transform Pairs, SIGGRAPH '78 */
  int i;
  double f, m, n, k;
  double modf(), trash;
  h = 6*modf(h,&trash);
  i = floor(h);
  f = h-i;
  m = (1-s);
  n = (1-s*f);
  k = (1-(s*(1-f)));
  switch(i){
    case 0: *r=1; *g=k; *b=m; break;
    case 1: *r=n; *g=1; *b=m; break;
    case 2: *r=m; *g=1; *b=k; break;
    case 3: *r=m; *g=n; *b=1; break;
    case 4: *r=k; *g=m; *b=1; break;
    case 5: *r=1; *g=m; *b=n; break;
    default: fprintf(stderr,"bad i: %f %d",h,i); exit(1);
  }
  f = *r;
  if( f < *g ) f = *g;
  if( f < *b ) f = *b;
  f = v / f;
  *r *= f;
  *g *= f;
  *b *= f;
}