From: Colin Wilson Subject: Re: HSL to RGB? Date: 11 May 2000 00:00:00 GMT Message-ID: Content-Transfer-Encoding: 8bit References: <391812C0.E9AE5F8B@mjwcorp.com> <8f94iu$k9h1@bornews.borland.com> <39195014.E2FF8596@ibm.net> Content-Type: text/plain; charset=iso-8859-1 Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Reply-To: colin@wilsonc.demon.co.uk Newsgroups: borland.public.delphi.graphics const RGBMAX = 255; HLSMAX=240; UNDEFINED = HLSMAX * 2 div 3; function HueToRGB(n1,n2,hue : Integer) : word; begin while hue > hlsmax do Dec (hue, HLSMAX); while hue < 0 do Inc (hue, HLSMAX); if hue < HLSMAX div 6 then result := ( n1 + (((n2-n1)*hue+(HLSMAX div 12)) div (HLSMAX div 6)) ) else if hue < HLSMAX div 2 then result := n2 else if hue < (HLSMAX*2) div 3 then result := n1 + (((n2-n1)*(((HLSMAX*2) div 3)-hue)+(HLSMAX div 12)) div (HLSMAX div 6)) else result := n1 end; procedure iHLSToRGB (hue, lum, sat : Integer; var r, g, b : Integer); var Magic1,Magic2 : Integer; begin if sat = 0 then begin r := (lum * RGBMAX) div HLSMAX; g := r; b := r; end else begin if lum <= HLSMAX div 2 then Magic2 := (lum*(HLSMAX + sat) + (HLSMAX div 2)) div HLSMAX else Magic2 := lum + sat - ((lum*sat) + (HLSMAX div 2)) div HLSMAX; Magic1 := 2*lum-Magic2; r := (HueToRGB (Magic1,Magic2,hue+(HLSMAX div 3))*RGBMAX + (HLSMAX div 2)) div HLSMAX; g := (HueToRGB (Magic1,Magic2,hue)*RGBMAX + (HLSMAX div 2)) div HLSMAX; b := (HueToRGB (Magic1,Magic2,hue-(HLSMAX div 3))*RGBMAX + (HLSMAX div 2)) div HLSMAX; end; end; Colin e-mail :colin@wilsonc.demon.co.uk web: http://www.wilsonc.demon.co.uk/delphi.htm