POV-Ray : Newsgroups : povray.general : Simple math question Server Time
30 Jul 2024 14:31:00 EDT (-0400)
  Simple math question (Message 1 to 7 of 7)  
From: some yahoo
Subject: Simple math question
Date: 14 Feb 2009 16:58:20
Message: <49973e7c$1@news.povray.org>
Trying to render something in a sequence of colors using an animation.  Lets just say
they are web buttons. I provide 
this color cube source to illustrate that I am getting a lot of missing colors.  What
I really need is a function that 
converts a float (0..1) into an rgb value such that from 0 to 1 all colors are
represented (with enough granularity).

what I have so far is this, but the math is goofy somehow.

#declare KK=100; //number of steps total

#declare K=0;  //represents the clock value

#declare cr=pow(KK+1,1/3);  //cube root
#declare cr2=cr*cr;         //cube root squared


#fopen myData "data.csv" write

#write( myData, "Number of frames\t",KK,"\n")
#write( myData, "cube root\t",cr,"\n")
#write( myData, "\nClock\tColor\n")

//simulate clock from 0 to 1
#while(K<=1)	
	#declare R=K;
	#declare G=(K*cr)-int(K*cr);
	#declare B=(K*cr2)-int(K*cr2);

	//quantize the colors to the cube root	
	#declare R=(int(R*cr)) /cr;
	#declare G=(int(G*cr)) /cr;
	#declare B=(int(B*cr)) /cr;
	
	#declare C=<R,G,B>;
  	 	
  	#write(myData,K,"\t",C,"\n")

  	sphere{(C-0.5)*10,.5
  		texture{pigment{color C} finish{ambient 1} } 		
  	}

	#declare K=K+0.01;
#end
#fclose myData

camera {
   location  <-10,2,-20>
   look_at   <0.0, 0.0,  0.0>
   right     x*image_width/image_height
}


background{ .5 }


=================

this gives the following values (my comments added)

Number of frames	100
cube root	4.65701

Clock	Color
0	<0,0,0>                    <-good
0.01	<0,0,0.21473>
0.02	<0,0,0.42946>
0.03	<0,0,0.64419>
0.04	<0,0,0.85892>              <-never gets to 1
0.05	<0,0.21473,0>              <-green increments as blue resets.. this is right
0.06	<0,0.21473,0.21473>
0.07	<0,0.21473,0.42946>
0.08	<0,0.21473,0.64419>
0.09	<0,0.21473,0.85892>
0.1	<0,0.42946,0>              <- again
0.11	<0,0.42946,0.21473>
0.12	<0,0.42946,0.42946>
0.13	<0,0.42946,0.64419>
0.14	<0,0.64419,0>              <- wait! where is .85?
0.15	<0,0.64419,0.21473>
0.16	<0,0.64419,0.42946>
0.17	<0,0.64419,0.64419>
0.18	<0,0.64419,0.85892>
0.19	<0,0.85892,0>
0.2	<0,0.85892,0.21473>
0.21	<0,0.85892,0.42946>
0.22	<0.21473,0,0.64419>       <-whoa! blue isnt resetting but green and blue do
anyway
0.23	<0.21473,0,0.85892>
0.24	<0.21473,0,0>
0.25	<0.21473,0,0.21473>
0.26	<0.21473,0,0.42946>
0.27	<0.21473,0.21473,0.64419>
0.28	<0.21473,0.21473,0>
0.29	<0.21473,0.21473,0.21473>
0.3	<0.21473,0.21473,0.42946>
0.31	<0.21473,0.42946,0.64419>
0.32	<0.21473,0.42946,0.85892>
0.33	<0.21473,0.42946,0>
0.34	<0.21473,0.42946,0.21473>
0.35	<0.21473,0.42946,0.42946>
0.36	<0.21473,0.64419,0.64419>
0.37	<0.21473,0.64419,0>
0.38	<0.21473,0.64419,0.21473>
0.39	<0.21473,0.64419,0.42946>
0.4	<0.21473,0.85892,0.64419>
0.41	<0.21473,0.85892,0.85892>
0.42	<0.21473,0.85892,0>
0.43	<0.42946,0,0.21473>
0.44	<0.42946,0,0.42946>
0.45	<0.42946,0,0.64419>
0.46	<0.42946,0,0.85892>
0.47	<0.42946,0,0>
0.48	<0.42946,0.21473,0.21473>
0.49	<0.42946,0.21473,0.42946>
0.5	<0.42946,0.21473,0.64419>
0.51	<0.42946,0.21473,0>
0.52	<0.42946,0.21473,0.21473>
0.53	<0.42946,0.42946,0.42946>
0.54	<0.42946,0.42946,0.64419>
0.55	<0.42946,0.42946,0.85892>
0.56	<0.42946,0.42946,0>
0.57	<0.42946,0.64419,0.21473>
0.58	<0.42946,0.64419,0.42946>
0.59	<0.42946,0.64419,0.64419>
0.6	<0.42946,0.64419,0>
0.61	<0.42946,0.64419,0.21473>
0.62	<0.42946,0.85892,0.42946>
0.63	<0.42946,0.85892,0.64419>
0.64	<0.42946,0.85892,0.85892>
0.65	<0.64419,0,0>
0.66	<0.64419,0,0.21473>
0.67	<0.64419,0,0.42946>
0.68	<0.64419,0,0.64419>
0.69	<0.64419,0,0.85892>
0.7	<0.64419,0.21473,0>
0.71	<0.64419,0.21473,0.21473>
0.72	<0.64419,0.21473,0.42946>
0.73	<0.64419,0.21473,0.64419>
0.74	<0.64419,0.42946,0>
0.75	<0.64419,0.42946,0.21473>
0.76	<0.64419,0.42946,0.42946>
0.77	<0.64419,0.42946,0.64419>
0.78	<0.64419,0.42946,0.85892>
0.79	<0.64419,0.64419,0>
0.8	<0.64419,0.64419,0.21473>
0.81	<0.64419,0.64419,0.42946>
0.82	<0.64419,0.64419,0.64419>
0.83	<0.64419,0.85892,0>
0.84	<0.64419,0.85892,0.21473>
0.85	<0.64419,0.85892,0.42946>
0.86	<0.85892,0,0.64419>
0.87	<0.85892,0,0.85892>
0.88	<0.85892,0,0>
0.89	<0.85892,0,0.21473>
0.9	<0.85892,0,0.42946>
0.91	<0.85892,0.21473,0.64419>
0.92	<0.85892,0.21473,0.85892>
0.93	<0.85892,0.21473,0>
0.94	<0.85892,0.21473,0.21473>
0.95	<0.85892,0.21473,0.42946>
0.96	<0.85892,0.42946,0.64419>
0.97	<0.85892,0.42946,0>
0.98	<0.85892,0.42946,0.21473>
0.99	<0.85892,0.42946,0.42946>
1	<0.85892,0.64419,0.64419>    <- this should be somewhere near <1,1,1>


Post a reply to this message

From: triple r
Subject: Re: Simple math question
Date: 14 Feb 2009 17:20:00
Message: <web.49974350240b718bef2b9ba40@news.povray.org>
some_yahoo <209### [at] gmailcom> wrote:
> Trying to render something in a sequence of colors using an animation.  Lets just
say they are web buttons. I provide

> this color cube source to illustrate that I am getting a lot of missing colors. 
What I really need is a function tha
t
> converts a float (0..1) into an rgb value such that from 0 to 1 all colors are
represented (with enough granularity).


Is there a reason the HSV or HSL color model would not work?  That way you could
use a nested loop to achieve a range of all colors, all saturations, and all
brightnesses in a very controlled fashion.

http://www.povray.org/documentation/view/3.6.1/434/

 - Ricky


Post a reply to this message

From: some yahoo
Subject: Re: Simple math question
Date: 14 Feb 2009 18:45:47
Message: <499757ab$1@news.povray.org>
triple_r wrote:
> some_yahoo <209### [at] gmailcom> wrote:
>> Trying to render something in a sequence of colors using an animation.  Lets just
say they are web buttons. I provide
> 
>> this color cube source to illustrate that I am getting a lot of missing colors. 
What I really need is a function tha
> t
>> converts a float (0..1) into an rgb value such that from 0 to 1 all colors are
represented (with enough granularity).
> 
> 
> Is there a reason the HSV or HSL color model would not work?  That way you could
> use a nested loop to achieve a range of all colors, all saturations, and all
> brightnesses in a very controlled fashion.
> 
> http://www.povray.org/documentation/view/3.6.1/434/
> 
>  - Ricky
> 
> 


no - I am doing a different color in each frame.  I only have a clock variable to work
with.  There are no nested loops


Post a reply to this message

From: triple r
Subject: Re: Simple math question
Date: 14 Feb 2009 19:40:01
Message: <web.49976357240b718bef2b9ba40@news.povray.org>
some_yahoo <209### [at] gmailcom> wrote:
> no - I am doing a different color in each frame.  I only have a clock variable to
work with.  There are no nested loo
ps

Weird.  I thought I responded already, but it didn't show up.  Oh well.

#declare x=floor(clock*nx)/(nx-1);
#declare y=floor(clock*nx*ny)/(ny-1);
#declare z=floor(clock*nx*ny*nz)/(nz-1);
#while (x>1+epsilon) #declare x=x-1; #end
#while (y>1+epsilon) #declare y=y-1; #end
#while (z>1+epsilon) #declare z=z-1; #end

Then map the clock onto a nested loop.  This may be what you've done already,
but the above code does it also, for x, y, z in [0,1] with nx, ny, and nz
discrete steps (you'll have to change x,y,z to something else, of course).  You
could use mod(), but the while loops avoid looping back to zero, as you ran
into, for epsilon < 1/min(dx,dy,dz).  Of course you could also just map it onto
RGB coordinates, but HSV seems somehow more appropriate.

 - Ricky


Post a reply to this message

From: some yahoo
Subject: Re: Simple math question
Date: 14 Feb 2009 22:47:10
Message: <4997903e@news.povray.org>
Sorry I cant really use this unless you tell me where the variables nx, ny, nz,
epsilon came from.

triple_r wrote:
> some_yahoo <209### [at] gmailcom> wrote:
>> no - I am doing a different color in each frame.  I only have a clock variable to
work with.  There are no nested loo
> ps
> 
> Weird.  I thought I responded already, but it didn't show up.  Oh well.
> 
> #declare x=floor(clock*nx)/(nx-1);
> #declare y=floor(clock*nx*ny)/(ny-1);
> #declare z=floor(clock*nx*ny*nz)/(nz-1);
> #while (x>1+epsilon) #declare x=x-1; #end
> #while (y>1+epsilon) #declare y=y-1; #end
> #while (z>1+epsilon) #declare z=z-1; #end
> 
> Then map the clock onto a nested loop.  This may be what you've done already,
> but the above code does it also, for x, y, z in [0,1] with nx, ny, and nz
> discrete steps (you'll have to change x,y,z to something else, of course).  You
> could use mod(), but the while loops avoid looping back to zero, as you ran
> into, for epsilon < 1/min(dx,dy,dz).  Of course you could also just map it onto
> RGB coordinates, but HSV seems somehow more appropriate.
> 
>  - Ricky
> 
> 
>


Post a reply to this message

From: triple r
Subject: Re: Simple math question
Date: 15 Feb 2009 01:00:00
Message: <web.4997aeb1240b718bef2b9ba40@news.povray.org>
some_yahoo <209### [at] gmailcom> wrote:
> Sorry I cant really use this unless you tell me where the variables nx, ny, nz,
epsilon came from.

Sorry.  nx is the number of steps you take in the 'x' variable.  That is, (0,
0.25, 0.5, 0.75, 1.0) would correspond to nx=5.  The same goes for ny and nz.
As I said, I tested this in Matlab, so you would have to change 'x,' 'y,' and
'z,' to another name since those are reserved in POV-Ray.  Other than that, the
syntax, I believe, should be okay.  epsilon is  just any sufficiently small
number.  1.0e-5, for example.  This is because, as you ran into, 1.0 > 1.0 may
be true or false in floating point arithmetic, but 1.0 > 1.0+1e-5  will always
be false in floating point, given sufficient precision.  Sorry for the
confusion.

 - Ricky


Post a reply to this message

From: Mike Williams
Subject: Re: Simple math question
Date: 15 Feb 2009 01:09:39
Message: <HRFnSJNFG7lJFwHy@econym.demon.co.uk>
Wasn't it some_yahoo who wrote:
>Trying to render something in a sequence of colors using an animation.
>Lets just say they are web buttons. I provide this color cube source to
>illustrate that I am getting a lot of missing colors.  What I really
>need is a function that converts a float (0..1) into an rgb value such
>that from 0 to 1 all colors are represented (with enough granularity).
>
>what I have so far is this, but the math is goofy somehow.

One thing that makes the math goofy is that cr and cr2 are not integers,
so the colours flop over at different points. I suspect that the only
way to fix that might be to choose KK to be one less than a perfect
cube.
  #declare KK=63; //number of steps total
And then adjust your K increment to match
  #declare K=K+1/(KK+1);

That gives you a results where each colour cycles through the values
  0.00
  0.25
  0.50
  0.75
but you really want to span the colour space 0-1 rather then 0-0.75, so
you need to multiply by cr/(cr-1)
  #declare C=<R,G,B>*cr/(cr-1);

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

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