POV-Ray : Newsgroups : povray.general : ANN: RGB<->HLS macros : Re: ANN: RGB<->HLS macros Server Time
9 Aug 2024 19:40:59 EDT (-0400)
  Re: ANN: RGB<->HLS macros  
From: David Fontaine
Date: 24 May 2000 18:56:16
Message: <392C5D2A.3233D985@faricy.net>
Peter Popov wrote:

> Sounds very, very useful (though I've *almost* learned to do this in
> my head :) )

Really? Well, HLS to RGB is easy, but the RGB back ot HLS is a real bizatch
to get the algorithm for. I basically just looked for patterns in the RGB
values for different HLS inputs.


The lightness (chroma) is easy, (max(RGB)+min(RGB))/2


The saturation (value) is (min(RGB)-max(RGB))/(2|L-0.5|-1)
if we let the unit square from (0,0) to (1,1) represent (S,L) values from
(0,.5)-(1,0), we can use the equations
y=1-n/x, where n is the value (max(RGB)-min(RGB)), line is the locus of all
(S,L) values satisfying that n value
y=2|0.5-L|, using the previously calculated L value, line represents the
locus of that particular L value.
Solving for the set we get:
x=-(max(RGB)-min(RGB))/(2|0.5-L|-1), x corresponding to saturation.


The hue is easy compared to all that.
first find where the maximum and minimum values in the RGB vector are and
use the following lookup chart:
   M=max, m=min,
   RGB n
   -----
   M-m 0
   -Mm 1
   mM- 2
   m-M 3
   -mM 4
   Mm- 5
Then the hue is
n+mod(n,2)-2(mod(n,2)-0.5)*(med(RGB)-min(RGB))/(max(RGB)-min(RGB))

--
David Fontaine     <dav### [at] faricynet>     ICQ 55354965
Please visit my website: http://www.faricy.net/~davidf/


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.