title: Assign colors to each points in points3d using mayavi python
body: <p>I have some points in 3D space. Each point has a color which is calculated using the following formulation-</p>
  
  <pre><code>// pack r/g/b into rgb
  uint8_t r = 255, g = 0, b = 0;    // Example: Red color
  uint32_t rgb = ((uint32_t)r &lt;&lt; 16 | (uint32_t)g &lt;&lt; 8 | (uint32_t)b);
  </code></pre>
  
  <p>As shown above, the RGB color is packed into one value. I am trying to visualize these points using mayavi python. Please see below the code snippet-</p>
  
  <pre><code>from mayavi.mlab import *
  import numpy as np
  
  N = 10
  # generate random points and colors (just for debugging)
  (x, y, z) = np.random.random((3, N))
  colors = np.random.random(N)
  
  nodes = points3d(x, y, z, scale_factor=0.1)
  nodes.glyph.scale_mode = 'scale_by_vector'
  nodes.mlab_source.dataset.point_data.scalars = colors
  
  show()
  </code></pre>
  
  <p>The above code is using random colors and it shows the following output-</p>
  
  <p><a href="https://i.stack.imgur.com/gshJn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gshJn.png" alt="output with random color"></a></p>
  
  <p>However, I want to specify colors instead of using random values. Please note that each point has a color. In this post, in order to make it easier, I am generating same colored points using the following function-</p>
  
  <pre><code>def pack_rgb(r, g, b):
      rgb = (r&lt;&lt;16) + (g&lt;&lt;8) + b
      return rgb
  
  colors = [pack_rgb(0, 255, 0) for _ in range(N)]
  </code></pre>
  
  <p>This generates red colored points, instead of green colored points as shown below-</p>
  
  <p><a href="https://i.stack.imgur.com/ivVdZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ivVdZ.png" alt="specified colors"></a></p>
  
  <p>What's going on here? <strong>My goal is to visualize colored points in mayavi python where each point has an RGB color.</strong></p>