POV-Ray : Newsgroups : povray.general : Coloring spheres based on their specified size Server Time
15 May 2024 15:52:00 EDT (-0400)
  Coloring spheres based on their specified size (Message 1 to 8 of 8)  
From: esfoster
Subject: Coloring spheres based on their specified size
Date: 1 May 2024 09:05:00
Message: <web.66323db4d7093489c8fc9e4964e035f0@news.povray.org>
Hello,

I have a set of spheres defined with x, y, z, and size.

I want to add a color to them based on their size. e.g larger particles green
and smaller particles blue and there would be a gradient assigned to fill the
particles in between.

I have tried gradients and functions but have not yet gotten it to work.


Post a reply to this message


Attachments:
Download 'pout.txt' (883 KB)

From: jr
Subject: Re: Coloring spheres based on their specified size
Date: 1 May 2024 09:55:00
Message: <web.66324923d66fa131686e436cde94f1@news.povray.org>
hi,

"esfoster" <nomail@nomail> wrote:
> I have a set of spheres defined with x, y, z, and size.
>
> I want to add a color to them based on their size. ...

suggest re-structuring your data, separate centres and radii.  eg.
#declare spharr = array [N] {
  array mixed [2] {<0.385010,0.768357,0.476123>,0.019470},
  ...
};

and a '#for()' loop to make the union of spheres from that.  then you can
"re-use" the radius as spline point perhaps, to retrieve a corresponding colour.

<https://wiki.povray.org/content/Reference:Spline>


regards, jr.


Post a reply to this message

From: Bald Eagle
Subject: Re: Coloring spheres based on their specified size
Date: 1 May 2024 10:20:00
Message: <web.66324ebcd66fa13b959fb4725979125@news.povray.org>
Welcome to the forum.  :)


"jr" <cre### [at] gmailcom> wrote:

> suggest re-structuring your data, separate centres and radii.  eg.
> #declare spharr = array [N] {
>   array mixed [2] {<0.385010,0.768357,0.476123>,0.019470},
>   ...
> };

Defnitely agree with jr that an array is the way to go.
No need to use mixed, you can just do x, y, z, r - but whatever way suits your
needs best

> and a '#for()' loop to make the union of spheres from that.  then you can
> "re-use" the radius as spline point perhaps, to retrieve a corresponding colour.

Don't know why you need the union, but you should just color your spheres at
each instantiation by using a macro that performs a linear interpolation between
green and blue based on the range defined by the smallest and largest spheres in
the array.

Depending on how you want your intermediate colors to look, you can do the
interpolation in different color spaces.

So, you write a macro
#macro ColorLERP (radius)
....
#end

then you just loop through your array and do sphere {<x, y, z> r pigment
{ColorLERP (r)}}

Hope that makes sense and you can figure most of that out.   If not, maybe
someone can chime in or I can take another look later today.

- BE


Post a reply to this message

From: ingo
Subject: Re: Coloring spheres based on their specified size
Date: 1 May 2024 12:00:00
Message: <web.663265fcd66fa1317bac71e8ffb8ce3@news.povray.org>
"esfoster" <nomail@nomail> wrote:

> I want to add a color to them based on their size. e.g larger particles green
> and smaller particles blue and there would be a gradient assigned to fill the
> particles in between.

divide the radius of every sphere by that of the largest sphere, that gives a
value between 1 and very small. The range depends on the sphere variation. Use
the value you get as an index to pick a colour, as jr suggest from a spline.

or use one of the interpolation (lerp) macro's to find an index between 1 and
zero from the radii.

ingo


Post a reply to this message

From: Bald Eagle
Subject: Re: Coloring spheres based on their specified size
Date: 1 May 2024 12:15:00
Message: <web.66326a77d66fa13b959fb4725979125@news.povray.org>
"ingo" <nomail@nomail> wrote:

> divide the radius of every sphere by that of the largest sphere, that gives a
> value between 1 and very small. The range depends on the sphere variation. Use
> the value you get as an index to pick a colour, as jr suggest from a spline.
>
> or use one of the interpolation (lerp) macro's to find an index between 1 and
> zero from the radii.
>
> ingo

pigment {rgb <0, Index, 1-Index>}


Post a reply to this message

From: Bald Eagle
Subject: Re: Coloring spheres based on their specified size
Date: 1 May 2024 13:40:00
Message: <web.66327e01d66fa13e573981225979125@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:

> Depending on how you want your intermediate colors to look, you can do the
> interpolation in different color spaces.

Just to expand on that and give you a better idea of what that looks like:

https://wiki.povray.org/content/User:Le_Forgeron#Colour_space_interpolation

https://stackoverflow.com/questions/13488957/interpolate-from-one-color-to-another


Post a reply to this message

From: kurtz le pirate
Subject: Re: Coloring spheres based on their specified size
Date: 2 May 2024 10:43:27
Message: <6633a68f@news.povray.org>
On 01/05/2024 15:03, esfoster wrote:
> Hello,
> 
> I have a set of spheres defined with x, y, z, and size.
> 
> I want to add a color to them based on their size. e.g larger particles green
> and smaller particles blue and there would be a gradient assigned to fill the
> particles in between.
> 
> I have tried gradients and functions but have not yet gotten it to work.
> 

What I would do :

* First of all : apply jr's recommendation.

* Then :
- find minimum and maximum radius values
- calc the interval for radii : dRad = rMax-rMin
- calc the difference from one color to other color :
  dCol = Color1 - Color2
- clac the ColorStep = dCol/dRad


* finally in the loop of all spheres :
- calc the color :
  theColor = Color1 + ColorStep*(AllSpheres[i][1]-rMin)
- draw it
  sphere {
    AllSpheres[i][0], // sphere position
    AllSpheres[i][1] // sphere radius
    pigment { color theColor }
  }



/just a thought. untested/

-- 
Kurtz le pirate
Compagnie de la Banquise


Post a reply to this message

From: esfoster
Subject: Re: Coloring spheres based on their specified size
Date: 2 May 2024 20:20:00
Message: <web.66342c84d66fa13c8fc9e4964e035f0@news.povray.org>
kurtz le pirate <kur### [at] gmailcom> wrote:
> On 01/05/2024 15:03, esfoster wrote:
> > Hello,
> >
> > I have a set of spheres defined with x, y, z, and size.
> >
> > I want to add a color to them based on their size. e.g larger particles green
> > and smaller particles blue and there would be a gradient assigned to fill the
> > particles in between.
> >
> > I have tried gradients and functions but have not yet gotten it to work.
> >
>
> What I would do :
>
> * First of all : apply jr's recommendation.
>
> * Then :
> - find minimum and maximum radius values
> - calc the interval for radii : dRad = rMax-rMin
> - calc the difference from one color to other color :
>   dCol = Color1 - Color2
> - clac the ColorStep = dCol/dRad
>
>
> * finally in the loop of all spheres :
> - calc the color :
>   theColor = Color1 + ColorStep*(AllSpheres[i][1]-rMin)
> - draw it
>   sphere {
>     AllSpheres[i][0], // sphere position
>     AllSpheres[i][1] // sphere radius
>     pigment { color theColor }
>   }
>
>
>
> /just a thought. untested/
>
> --
> Kurtz le pirate
> Compagnie de la Banquise

Thank you to everyone who helped. I ended up making a Matlab script to
interpolate rgb triplets and applying them to my diameters after they were
sorted. Took some work but I loved  the result.


Post a reply to this message


Attachments:
Download 'pout.txt' (130 KB)

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