MORE@INTELLICORP.COM (Nitin More) (08/15/89)
I need the formulae for converting from HSB (Hue, Saturation, Brightness) color values to RGB (Red, Green, Blue) color values and vice versa. Thanks, - Nitin. more@intellicorp.com -------
dcmartin@lisp.eng.sun.com (David C. Martin) (08/15/89)
Just in case anyone else wants to play with this stuff... dcm ---- cut here ---- /* * Convert red/green/blue to hue/saturation/value. */ rgb_to_hsv(r, g, b, hp, sp, vp) u_char r, g, b; int *hp, *sp, *vp; { int cmax = max(max(r, g), b); int cmin = min(min(r, g), b); int rc, gc, bc; *vp = cmax; if (cmax != 0) *sp = (cmax - cmin) * 256 / cmax; else *sp = 0; if (*sp == 0) *hp = 0; else { rc = (cmax - r) * 256 / (cmax - cmin); gc = (cmax - g) * 256 / (cmax - cmin); bc = (cmax - b) * 256 / (cmax - cmin); if (r == cmax) *hp = bc - gc; else if (g == cmax) *hp = 2*256 + rc - bc; else *hp = 4*256 + gc - rc; *hp = (*hp * 60) / 256; if (*hp < 0) *hp += 360; } } /* * Convert hue/saturation/value to red/green/blue. */ hsv_to_rgb(h, s, v, rp, gp, bp) register int h, s, v; u_char *rp, *gp, *bp; { register int i, f; u_char p, q, t; if (s == 0) *rp = *gp = *bp = v; else { if (h == 360) h = 0; h = h * 256 / 60; i = h / 256 * 256; f = h - i; p = v * (256 - s) / 256; q = v * (256 - (s*f)/256) / 256; t = v * (256 - (s * (256 - f))/256) / 256; switch (i/256) { case 0: *rp = v; *gp = t; *bp = p; break; case 1: *rp = q; *gp = v; *bp = p; break; case 2: *rp = p; *gp = v; *bp = t; break; case 3: *rp = p; *gp = q; *bp = v; break; case 4: *rp = t; *gp = p; *bp = v; break; case 5: *rp = v; *gp = p; *bp = q; break; } } } -- ----- David C. Martin arpa: dcmartin@cs.wisc.edu University of Wisconsin - Madison uucp: uunet!ucbarpa!dcmartin Computer Sciences Department at&t: 608/262-6624 (O)