POV-Ray : Newsgroups : povray.beta-test : Colors.inc-macros incomplete : Re: Colors.inc-macros incomplete Server Time
2 May 2024 06:39:31 EDT (-0400)
  Re: Colors.inc-macros incomplete  
From: Michael Andrews
Date: 11 Sep 2001 09:26:44
Message: <3B9E12AB.3B0CDA0@reading.ac.uk>
Rune,

I have some old HSV <-> RGB macros ... which I've just updated to the
same standard as yours - I think they're right and I've added some
checks, but test them :-)

I include your test code, just changed HSL to HSV, and it seems to work.

Rune wrote:
> 
> I have tested the CHSL2RGB macro and fixed some bugs in it and I've also
> coded a CRGB2HSL macro that converts the other way. The conversion back and
> forth is perfect at least in the tests I've made. Here are the two macros
> and some code that shows that multiple conversions back and forth produce
> consistent results.
> 
> I will include these macros in colors.inc. The other color formats will be
> dropped.
> 
> Please feel free to make lots of tests, as there might still be some bugs
> left!

#macro CRGB2HSV ( Pvec )
  #local vec = colour Pvec;
  #local V = max(vec.x, max(vec.y, vec.z));
  #if (V <= 0.0)
    #local R2H_col = colour rgbft <0, 0, 0, vec.filter, vec.transmit>;
  #else
    #local Vp = min(vec.x, min(vec.y, vec.z));
    #local S = (V-Vp)/V;
    #if (S <= 0.0)
      #local R2H_col = colour rgbft <0, 0, V, vec.filter, vec.transmit>;
    #else
      #local P = (V-vec)/(V-Vp);
      #if (vec.x = V & vec.y = Vp)
        #local H = (5 + P.z)/6;
      #else
        #if (vec.x = V & vec.y != Vp)
          #local H = (1 - P.y)/6;
        #else
          #if (vec.y = V & vec.z = Vp)
            #local H = (1 + P.x)/6;
          #else
            #if (vec.y = V & vec.z != Vp)
              #local H = (3 - P.z)/6;
            #else
              #if (vec.x = V)
                #local H = (3 + P.y)/6;
              #else
                #local H = (5 - P.x)/6;
              #end
            #end
          #end
        #end
      #end
      #local R2H_col = colour rgbft <H, S, V, vec.filter, vec.transmit>;
    #end
  #end
  <R2H_col.x,R2H_col.y,R2H_col.z,R2H_col.filter,R2H_col.transmit>
#end
      
#macro CHSV2RGB ( Pvec )
  #local vec = colour Pvec;
  #local H = mod(vec.x*6, 6);
  #local H = ((H < 0)? H+6: H);
  #local P = floor(H);
  #local S = H - P;
  #local Y = min(1, max(vec.y, 0));
  #local Z = min(1, max(vec.z, 0));
  #local A = (1 - Y)*Z;
  #local B = (1 - (S*Y))*Z; 
  #local C = (1 - ((1-S)*Y))*Z;
  #switch (P)
    #case (0)
      #local H2R_col = colour rgbft <Z, C, A, vec.filter, vec.transmit>;
    #break
    #case (1)
      #local H2R_col = colour rgbft <B, Z, A, vec.filter, vec.transmit>;
    #break
    #case (2)
      #local H2R_col = colour rgbft <A, Z, C, vec.filter, vec.transmit>;
    #break
    #case (3)
      #local H2R_col = colour rgbft <A, B, Z, vec.filter, vec.transmit>;
    #break
    #case (4)
      #local H2R_col = colour rgbft <C, A, Z, vec.filter, vec.transmit>;
    #break
    #case (5)
      #local H2R_col = colour rgbft <Z, A, B, vec.filter, vec.transmit>;
    #break
  #end
  <H2R_col.x,H2R_col.y,H2R_col.z,H2R_col.filter,H2R_col.transmit>
#end

#declare Xm = 20;
#declare Ym = 20;
#declare X = 0;
#while (X<=Xm)
   #declare Y = 0;
   #while (Y<=Ym)
      #declare Xv = (X/Xm);
      #declare Yv = (Y/Ym);
      sphere {
         <Xv,Yv,0>-0.6*x, 0.1
         pigment {color CHSV2RGB(<0,Xv,Yv>)}
         finish {ambient 1 diffuse 0}
      }
      sphere {
         <Xv,Yv,0>+0.6*x, 0.1
         pigment {color CHSV2RGB(CRGB2HSV(CHSV2RGB(<0,Xv,Yv>)))}
         finish {ambient 1 diffuse 0}
      }
      #declare Y=Y+1;
   #end
   #declare X=X+1;
#end
camera {translate <0.5,0.5,-2>}
background {color rgb 0.5}


Post a reply to this message

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