Try now in the
Open Cloud »
(no sign-in required)

# Color Cube

Visualize red-green-blue color space.

Run the code to make a swatch of color by specifying RGB components. Try other color components:

Note: use color component values between 0 and 1.

Use RGBColor to specify a color from its red, green, and blue components.

This gives pure red because the green and blue components are zero:

 In[1]:= XRGBColor[{1, 0, 0}]
 Out[1]=

Mixing red and green gives yellow:

 In[2]:= XRGBColor[{1, 1, 0}]
 Out[2]=

Color component values lie between 0 and 1.

When all of the color components are 0, you get black:

 In[3]:= XRGBColor[{0, 0, 0}]
 Out[3]=

When all of the color components are 1, you get white:

 In[4]:= XRGBColor[{1, 1, 1}]
 Out[4]=

Mixing different amounts of each component gives different colors:

 In[5]:= XRGBColor[{.9, .7, .5}]
 Out[5]=
 In[6]:= XRGBColor[{.5, .7, .9}]
 Out[6]=

 In[1]:= XRGBColor[{.9, .7, .5}]
 Out[1]=

Make a color sphere. Try other colors:

This draws a sphere:

 In[1]:= XGraphics3D[Sphere[]]
 Out[1]=

This colors the sphere red:

 In[2]:= XGraphics3D[{RGBColor[{1, 0, 0}], Sphere[]}]
 Out[2]=

 In[1]:= XGraphics3D[{RGBColor[{1, 0, 0}], Sphere[]}]
 Out[1]=

Make a 5×5×5 array of spheres (each of radius 1/2). Try other sizes of arrays, and other radii:

This draws a sphere at coordinate {1,1,1} with a radius of 1/2 (PlotRange specifies the extent of the drawing area; you can ignore it for now):

 In[1]:= XGraphics3D[ Sphere[{1, 1, 1}, 1/2], PlotRange -> {{0, 6}, {0, 6}, {0, 6}}]
 Out[1]=

Change the coordinate to draw the sphere somewhere else:

 In[2]:= XGraphics3D[ Sphere[{2, 4, 3}, 1/2], PlotRange -> {{0, 6}, {0, 6}, {0, 6}}]
 Out[2]=

Table makes arrays of things. Use Table to draw a whole array of spheres.

This makes an array of 3 numbers:

 In[3]:= XTable[10 i, {i, 3}]
 Out[3]=

With two indices, you get a two-dimensional array (an array of arrays):

 In[4]:= XTable[10 i +j, {i, 3}, {j, 3}]
 Out[4]=

This uses Table to make a one-dimensional array of spheres:

 In[5]:= XGraphics3D[ Table[Sphere[{i, 1, 1}, 1/2], {i, 5}], PlotRange -> {{0, 6}, {0, 6}, {0, 6}}]
 Out[5]=

Add an index to get a two-dimensional array of spheres:

 In[6]:= XGraphics3D[ Table[Sphere[{i, j, 1}, 1/2], {i, 5}, {j, 5}], PlotRange -> {{0, 6}, {0, 6}, {0, 6}}]
 Out[6]=

Add a third index to get a three-dimensional array of spheres:

 In[7]:= XGraphics3D[ Table[Sphere[{i, j, k}, 1/2], {i, 5}, {j, 5}, {k, 5}], PlotRange -> {{0, 6}, {0, 6}, {0, 6}}]
 Out[7]=

You can remove the PlotRange; without it, the drawing area is automatically made just large enough to contain the spheres:

 In[8]:= XGraphics3D[Table[Sphere[{i, j, k}, 1/2], {i, 5}, {j, 5}, {k, 5}]]
 Out[8]=

 In[1]:= XGraphics3D[Table[Sphere[{i, j, k}, 1/2], {i, 5}, {j, 5}, {k, 5}]]
 Out[1]=

Make the spheres different colors. Try reordering the color componentsfor example, {j,i,k}:

Note: rotate the cube to see other orientations.

This gives a one-dimensional array of colors that vary from almost black to red (dividing by 5 keeps the color components between 0 and 1):

 In[1]:= XTable[RGBColor[{i, 0, 0}/5], {i, 5}]
 Out[1]=

This gives a two-dimensional array of colors:

 In[2]:= XTable[RGBColor[{i, j, 0}/5], {i, 5}, {j, 5}]
 Out[2]=

Use the same strategy to color the spheres in an array:

 In[3]:= XGraphics3D[ Table[{RGBColor[{i, j, k}/5], Sphere[{i, j, k}, 1/2]}, {i, 5}, {j, 5}, {k, 5}]]
 Out[3]=

 In[1]:= XGraphics3D[ Table[{RGBColor[{i, j, k}/5], Sphere[{i, j, k}, 1/2]}, {i, 5}, {j, 5}, {k, 5}]]
 Out[1]=

Manipulate the radius of each sphere. Drag the slider to change the sizes of the spheres:

Note: rotate the color cube to see it from a different angle.

Manipulate puts an interactive interface onto a piece of code.

This draws a sphere with radius 1/2:

 In[1]:= XGraphics3D[Sphere[{0, 0, 0}, 1/2], PlotRange -> 1]
 Out[1]=

Make the radius interactive by wrapping the code with Manipulate, replacing 1/2 with the variable r, and specifying that r goes from 0 to 1. Drag the slider to change the radius of the sphere:

 In[2]:= XManipulate[ Graphics3D[Sphere[{0, 0, 0}, r], PlotRange -> 1], {r, 0, 1} ]
 Out[2]=

Do the same thing with the array of spheres:

 In[3]:= XManipulate[ Graphics3D[ Table[{RGBColor[{i, j, k}/5], Sphere[{i, j, k}, r]}, {i, 5}, {j, 5}, {k, 5}]], {r, .1, .5}]
 Out[3]=

 In[1]:= XManipulate[ Graphics3D[ Table[{RGBColor[{i, j, k}/5], Sphere[{i, j, k}, r]}, {i, 5}, {j, 5}, {k, 5}]], {r, .1, .5}]
 Out[1]=

Share ItMake an interactive color cube website:

Deploy the Manipulate to the Wolfram Cloud where anyone with a browser can use it:

 In[1]:= XCloudDeploy[ Manipulate[ Graphics3D[ Table[{RGBColor[{i, j, k}/5], Sphere[{i, j, k}, r]}, {i, 5}, {j, 5}, {k, 5}]], {r, .1, .5}], Permissions -> "Public" ]
 Out[1]=

Click the link in the output to visit the site.

Share the link by right-clicking it and choosing Copy Address. Paste the link into an email, tweet, or other message.

 In[1]:= XCloudDeploy[ Manipulate[ Graphics3D[ Table[{RGBColor[{i, j, k}/5], Sphere[{i, j, k}, r]}, {i, 5}, {j, 5}, {k, 5}]], {r, .1, .5}], Permissions -> "Public" ]
 Out[1]=