.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/colormap_channels.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_feature_demo_colormap_channels.py: Colormap Channels ================= Example demonstrating colormaps in 4 modes: grayscale, gray+alpha, RGB, RGBA. .. GENERATED FROM PYTHON SOURCE LINES 7-65 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_colormap_channels_001.webp :alt: colormap channels :srcset: /_gallery/feature_demo/images/sphx_glr_colormap_channels_001.webp :class: sphx-glr-single-img .. code-block:: Python import numpy as np import pygfx as gfx import pylinalg as la group = gfx.Group() geometry = gfx.torus_knot_geometry(1, 0.3, 128, 32) geometry.texcoords = gfx.Buffer(geometry.texcoords.data[:, 0]) camera = gfx.OrthographicCamera(16, 3) def create_object(tex, xpos): material = gfx.MeshPhongMaterial(map=tex) obj = gfx.Mesh(geometry, material) obj.local.x = xpos group.add(obj) # === 1-channel colormap: grayscale cmap1 = np.array([(1,), (0,), (0,), (1,)], np.float32) tex1 = gfx.Texture(cmap1, dim=1) create_object(tex1, -6) # ==== 2-channel colormap: grayscale + alpha cmap2 = np.array([(1, 1), (0, 1), (0, 0), (1, 0)], np.float32) tex1 = gfx.Texture(cmap2, dim=1) create_object(tex1, -2) # === 3-channel colormap: RGB cmap3 = np.array([(1, 1, 0), (0, 1, 0), (0, 1, 0), (1, 1, 0)], np.float32) tex1 = gfx.Texture(cmap3, dim=1) create_object(tex1, +2) # === 4-channel colormap: RGBA cmap4 = np.array([(1, 1, 0, 1), (0, 1, 0, 1), (0, 1, 0, 0), (1, 1, 0, 0)], np.float32) tex1 = gfx.Texture(cmap4, dim=1) create_object(tex1, +6) def animate(): rot = la.quat_from_euler((0.0071, 0.01), order="XY") for obj in group.children: obj.local.rotation = la.quat_mul(rot, obj.local.rotation) if __name__ == "__main__": disp = gfx.Display() disp.before_render = animate disp.show(group) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.939 seconds) .. _sphx_glr_download__gallery_feature_demo_colormap_channels.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: colormap_channels.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: colormap_channels.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: colormap_channels.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_ .. only:: html Interactive example ------------------- Try this example in your browser using Pyodide. Might not work with all examples and all devices. Check the output and your browser's console for details. .. raw:: html