A python module for scientific visualization, analysis of 3D objects and point clouds.
Color management system including color conversion functions, colormap application, palette generation, and visual styling utilities for enhancing 3D visualizations. This module provides comprehensive tools for managing the visual appearance of vedo objects.
Functions for converting between different color representations and managing color specifications.
def get_color(rgb=None, hsv=None):
"""
Convert color specifications to RGB format.
Parameters:
- rgb: tuple, str, or int, optional
RGB color specification
- hsv: tuple, optional
HSV color specification
Returns:
tuple: RGB color values (0-1 range)
"""
def get_color_name(c):
"""
Get color name from RGB values.
Parameters:
- c: tuple or array-like
RGB color values
Returns:
str: Closest color name
"""
def hsv2rgb(hsv):
"""
Convert HSV color to RGB.
Parameters:
- hsv: list or tuple
HSV color values [hue, saturation, value]
Returns:
list: RGB color values
"""
def rgb2hsv(rgb):
"""
Convert RGB color to HSV.
Parameters:
- rgb: list or tuple
RGB color values
Returns:
list: HSV color values
"""
def rgb2hex(rgb):
"""
Convert RGB color to hexadecimal.
Parameters:
- rgb: list or tuple
RGB color values
Returns:
str: Hexadecimal color string
"""
def hex2rgb(hx):
"""
Convert hexadecimal color to RGB.
Parameters:
- hx: str
Hexadecimal color string
Returns:
list: RGB color values
"""Functions for applying colormaps to scalar data and generating color palettes.
def color_map(value, name="jet", vmin=None, vmax=None):
"""
Apply colormap to scalar values.
Parameters:
- value: float, array-like
Scalar value(s) to map
- name: str, default "jet"
Colormap name (matplotlib or vedo colormaps)
- vmin: float, optional
Minimum value for color mapping
- vmax: float, optional
Maximum value for color mapping
Returns:
tuple or array: RGB color(s) corresponding to input values
"""
def build_palette(color1, color2, n, hsv=True):
"""
Create color palette between two colors.
Parameters:
- color1: str or tuple
Starting color
- color2: str or tuple
Ending color
- n: int
Number of colors in palette
- hsv: bool, default True
Interpolate in HSV space instead of RGB
Returns:
numpy.ndarray: Array of RGB colors
"""
def build_lut(
colorlist,
vmin=None,
vmax=None,
below_color=None,
above_color=None,
nan_color=None,
below_alpha=None,
above_alpha=None,
nan_alpha=None,
interpolate=True
):
"""
Build lookup table for color mapping.
Parameters:
- colorlist: list
List of colors for the lookup table
- vmin: float, optional
Minimum scalar value
- vmax: float, optional
Maximum scalar value
- below_color: str or tuple, optional
Color for values below vmin
- above_color: str or tuple, optional
Color for values above vmax
- nan_color: str or tuple, optional
Color for NaN values
- below_alpha: float, optional
Alpha for below_color
- above_alpha: float, optional
Alpha for above_color
- nan_alpha: float, optional
Alpha for nan_color
- interpolate: bool, default True
Interpolate colors between control points
Returns:
vtkLookupTable: VTK lookup table object
"""Enhanced printing functions with color and formatting support for terminal output.
def printc(
*strings,
c=None,
bc=None,
bold=False,
italic=False,
blink=False,
underline=False,
strike=False,
dim=False,
invert=False,
box="",
end="\\n",
flush=False
):
"""
Print colored and formatted text to terminal.
Parameters:
- *strings: variable arguments
Text strings to print
- c: str or int, optional
Text color name or code
- bc: str or int, optional
Background color name or code
- bold: bool, default False
Use bold text
- italic: bool, default False
Use italic text
- blink: bool, default False
Use blinking text
- underline: bool, default False
Use underlined text
- strike: bool, default False
Use strikethrough text
- dim: bool, default False
Use dim text
- invert: bool, default False
Invert foreground/background colors
- box: str, default ""
Box style for text ("", "=", "-", etc.)
- end: str, default "\\n"
String appended after the last value
- flush: bool, default False
Forcibly flush the stream
"""
def printd(*strings, q=False):
"""
Print debug information with special formatting.
Parameters:
- *strings: variable arguments
Debug strings to print
- q: bool, default False
Suppress output if True
"""Classes for managing advanced visual properties and lighting.
class LightKit:
"""
Advanced lighting system for 3D scenes.
Parameters:
- key_light_intensity: float, default 1.0
Intensity of key light
- key_light_warmth: float, default 0.6
Warmth of key light color
- fill_light_intensity: float, default 0.3
Intensity of fill light
- fill_light_warmth: float, default 0.4
Warmth of fill light color
- back_light_intensity: float, default 0.1
Intensity of back light
- back_light_warmth: float, default 0.5
Warmth of back light color
"""
def __init__(
self,
key_light_intensity=1.0,
key_light_warmth=0.6,
fill_light_intensity=0.3,
fill_light_warmth=0.4,
back_light_intensity=0.1,
back_light_warmth=0.5
): ...Predefined color names and colormap options available in vedo.
# Common color names (subset of available colors)
COLOR_NAMES = [
"red", "green", "blue", "yellow", "cyan", "magenta", "white", "black",
"gray", "orange", "pink", "purple", "brown", "lime", "olive", "navy",
"teal", "silver", "gold", "crimson", "indigo", "violet", "turquoise",
"salmon", "khaki", "plum", "orchid", "tan", "coral", "azure", "ivory"
]
# Available colormaps (subset of available maps)
COLORMAPS = [
# Sequential colormaps
"viridis", "plasma", "inferno", "magma", "cividis",
"Blues", "Greens", "Reds", "Oranges", "Purples",
"hot", "cool", "spring", "summer", "autumn", "winter",
# Diverging colormaps
"coolwarm", "bwr", "seismic", "RdBu", "RdYlBu", "RdYlGn",
"Spectral", "PiYG", "PRGn", "BrBG", "PuOr", "RdGy",
# Qualitative colormaps
"Set1", "Set2", "Set3", "Pastel1", "Pastel2", "Dark2",
"Accent", "tab10", "tab20", "tab20b", "tab20c",
# Special vedo colormaps
"jet", "rainbow", "terrain", "gist_earth", "ocean",
"flag", "prism", "hsv", "nipy_spectral", "gist_ncar"
]import vedo
import numpy as np
# Basic color specifications
sphere_red = vedo.Sphere(c='red')
sphere_rgb = vedo.Sphere(c=(0.2, 0.8, 0.3))
sphere_hex = vedo.Sphere(c='#ff5733')
# Color conversions
rgb_color = vedo.get_color('blue')
hex_color = vedo.rgb2hex([1, 0, 0])
hsv_color = vedo.rgb2hsv([0.5, 0.3, 0.8])
# Apply colormaps to data
x = np.linspace(0, 10, 100)
y = np.sin(x)
scalar_data = y + np.random.normal(0, 0.1, 100)
# Create mesh with scalar coloring
points = np.column_stack([x, y, np.zeros(100)])
mesh = vedo.Points(points)
mesh.cmap('viridis', scalar_data)
# Custom color palette
colors = vedo.build_palette('blue', 'red', 10)
multicolor_spheres = []
for i, color in enumerate(colors):
sphere = vedo.Sphere(pos=(i, 0, 0), c=color, r=0.3)
multicolor_spheres.append(sphere)
# Advanced colormap with custom lookup table
lut = vedo.build_lut(
['blue', 'cyan', 'yellow', 'red'],
vmin=0, vmax=1,
below_color='black',
above_color='white',
interpolate=True
)
# Apply to volume data
volume_data = np.random.rand(50, 50, 50)
volume = vedo.Volume(volume_data)
volume.cmap(lut)
# Enhanced terminal output
vedo.printc("Success!", c='green', bold=True)
vedo.printc("Warning:", c='yellow', bc='black', box='-')
vedo.printc("Error occurred", c='red', bold=True, blink=True)
# Debug printing
vedo.printd("Debug info:", np.mean(scalar_data), "processed")
# Advanced lighting setup
light_kit = vedo.LightKit(
key_light_intensity=1.2,
key_light_warmth=0.8,
fill_light_intensity=0.4
)
# Visualization with custom lighting
plt = vedo.Plotter()
plt.add(mesh)
plt.add_light_kit(light_kit)
plt.show()
# Color analysis
mesh_colors = mesh.colors() # Get current colors
color_names = [vedo.get_color_name(c) for c in mesh_colors[:5]]
vedo.printc("First 5 colors:", color_names, c='blue')
# Create gradient visualization
gradient_data = np.linspace(0, 1, 1000).reshape(20, 50)
gradient_image = vedo.Image(gradient_data)
gradient_image.cmap('rainbow')
vedo.show(gradient_image, title="Color Gradient")Install with Tessl CLI
npx tessl i tessl/pypi-vedo