svg3d.shaders

class svg3d.shaders.DiffuseShader(base_style, light_direction=array([1., 1., 0.5]), absorbance=0.6)[source]

Bases: Shader

Shade Mesh objects with Lambertian (dot product diffuse) lighting.

__call__(face_index, mesh)[source]

Compute the shaded style for a face in a mesh.

Parameters:
  • face_index (int) – Index of the face in the mesh.

  • mesh (Mesh) – An svg3d mesh object.

  • absorbance (float, optional) – The “absorbance” of the mesh surface. Should fall in the range [0.0, 1.0), with larger values equating to darker shading. Default is 0.6.

Returns:

A dictionary containing the SVG style attributes for the shaded face.

Return type:

dict

__init__(base_style, light_direction=array([1., 1., 0.5]), absorbance=0.6)[source]

Initialize the diffuse shader.

Parameters:
  • base_color (str, optional) – A hexadecimal-formatted color string for the mesh. Default is “#71618D”.

  • light_direction (iterable of float, optional) – A 3-element array specifying the direction of the light source. Default is (1.0, 1.0, 0.5).

  • base_style (dict | None, optional) – The style dict for the Shader.

property base_color

Get or set the base color for the mesh from a hexadecimal string.

Type:

dict

property diffuse_light_direction

A 3-element array representing the direction of the light source.

Type:

np.ndarray

classmethod from_color(base_color)[source]

Create a Shader instance with a specified base color.

Parameters:

base_color (str) – The base color as a hexadecimal string (e.g., #FFFFFF).

classmethod from_color_and_direction(base_color, light_direction)[source]

Create a Shader instance with a specified base color and light direction.

Parameters:
  • base_color (str) – The base color as a hexadecimal string (e.g., #FFFFFF).

  • light_direction (array or list of float) – A 3-element iterable specifying the diffuse light direction.

classmethod from_style_dict(style, light_direction=array([1., 1., 0.5]))[source]

Create a Shader instance with a style dictionary.

Parameters:
  • style (dict) – The style dict for the Shader

  • light_direction (array or list of float, optional.) – A 3-element iterable specifying the diffuse light direction. Default value: (1.0, 1.0, 0.5)

class svg3d.shaders.Shader(base_style=None)[source]

Bases: ABC

Abstract base class for shaders.

abstract __call__(face_index, mesh)[source]

Compute the shaded style for a face in a mesh.

Abstract method to be implemented in subclasses.

Parameters:
Return type:

dict

__init__(base_style=None)[source]

Initialize the shader.

Parameters:

base_style (dict | None, optional) – The style attribute dict for the Shader.

property base_style

Get or set the style attribute dict for the object.

Type:

dict

class svg3d.shaders.UniformShader(base_style=None)[source]

Bases: Shader

Shade all faces of a Mesh with a single, uniform color.

This shader is useful in figure generation and when simplicity and clarity are maximally important.

__call__(face_index, mesh)[source]

Render face index i in a mesh based on the shader’s style dict.

Parameters:
Return type:

dict

__init__(base_style=None)[source]

Initialize the diffuse shader.

Parameters:

base_style (dict | None, optional) – The style dict for the Shader.

svg3d.shaders.diffuse_lighting(face_index, mesh, light_direction=None, base_style=None, base_color='#71618D')[source]

Apply Lambertian (dot product diffuse) shading to a face in an Mesh.

This is a convenience function for backwards compatibility. The full-featured Shader class should be used in most instances.

svg3d.shaders.hex2rgb(hexc)[source]

Convert a hexadecimal color string to an RGB array normalized to [0, 1].

Parameters:

hexc (str) – A hexadecimal color string, with or without a leading #.

Returns:

A NumPy array containing RGB values normalized to the range [0, 1].

Return type:

\((3,)\) numpy.ndarray

Examples

>>> hex2rgb("#FFFFFF")
array([1., 1., 1.])
>>> hex2rgb("000000")
array([0., 0., 0.])
svg3d.shaders.rgb2hex(rgb)[source]

Convert an RGB color array to a hexadecimal color string.

Parameters:

rgb (\((3,)\) numpy.ndarray: The RGB values to convert.)

Returns:

str

Return type:

A hexadecimal color string in uppercase format, prefixed with #.

Examples

>>> rgb2hex(np.array([1.0, 1.0, 1.0]))
'#FFFFFF'
>>> rgb2hex(np.array([0.0, 0.0, 0.0]))
'#000000'