3
$\begingroup$
Graphics3D[
 Table[
  Style[
   Sphere[{x, y, z}, 0.5], Hue[c]], {c, 0, 1, 0.1}, {x, 5}, {y, 
   5}, {z, 5}]]

I need to generate a 10x10x10 array of spheres with RGB components ranging from 0 to 1. The spheres should be centered at integer coordinates, and should just touch each other.

The expected output is shown in the image below. Currently, I'm getting spheres with one color only. Please help.

enter image description here

$\endgroup$

4 Answers 4

1
$\begingroup$

Using Tuples:

Graphics3D[{{RGBColor[#/10], Sphere[#, 0.5]} & /@ 
   Tuples[Range[10], 3]}]

Using Array:

Graphics3D[{Array[{RGBColor[{#1/10, #2/10, #3/10}], 
     Sphere[{#1, #2, #3}, 0.5]} &, {10, 10, 10}]}]

ColorConvert from RGBColor to Hue

Graphics3D[{Array[{ColorConvert[{#1/10, #2/10, #3/10}, "HSB"], 
     Sphere[{#1, #2, #3}, 0.5]} &, {10, 10, 10}]}]

enter image description here

$\endgroup$
2
$\begingroup$
Graphics3D[
 Table[Style[Sphere[{x, y, z}, 0.5], RGBColor[x/10, y/10, z/10]], 
   {x, 10}, {y, 10}, {z, 10}]]

enter image description here

$\endgroup$
2
$\begingroup$

You must use the coordinates for the Hue. Note that Hue values must be between 0 and 1 (if you do not use ColorFunctionScaling). Here is an example:

Graphics3D[
 Table[Style[Sphere[{x, y, z}, 0.5], Hue[(x + y + z)/15]], {x, 5}, {y,
    5}, {z, 5}]]

enter image description here

If you want to use RGBColor:

Graphics3D[
 Table[Style[Sphere[{x, y, z}, 0.5], RGBColor[x/5, y/5, z/5]], {x, 
   5}, {y, 5}, {z, 5}]]

enter image description here

$\endgroup$
4
  • $\begingroup$ Your posted code does not produce the desired figure. $\endgroup$ Oct 5 at 19:30
  • $\begingroup$ @David Thank you, I copied the wrong code. Fixed it. $\endgroup$ Oct 5 at 19:33
  • $\begingroup$ Your code still does not produce what the OP requests because you are working with Hue but you must instead use RGB. $\endgroup$ Oct 5 at 19:35
  • $\begingroup$ Ummm.... how does your second solution differ from my earlier one? $\endgroup$ Oct 6 at 13:32
1
$\begingroup$

Using CoordinateBoundsArray.

Graphics3D[{RGBColor @ #, Sphere[#, .1/2]} & /@ 
  Flatten[CoordinateBoundsArray[{{0, 1}, {0, 1}, {0, 
      1}}, {.1, .1, .1}], 2]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.