Skip to content

Commit

Permalink
Add "VisualAxes" class.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Oct 24, 2023
1 parent c5559d5 commit 4572df2
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 13 deletions.
6 changes: 6 additions & 0 deletions colour_visuals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import colour
import numpy as np

from .axes import (
VisualAxes,
)
from .diagrams import (
VisualChromaticityDiagram,
VisualChromaticityDiagramCIE1931,
Expand Down Expand Up @@ -48,6 +51,9 @@
__status__ = "Production"

__all__ = [
"VisualAxes",
]
__all__ += [
"VisualSpectralLocus2D",
"VisualSpectralLocus3D",
"VisualChromaticityDiagram",
Expand Down
179 changes: 179 additions & 0 deletions colour_visuals/axes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# !/usr/bin/env python
"""
Axes Visuals
============
Defines the axes visuals:
- :class:`colour_visuals.VisualAxes`
"""

from __future__ import annotations

import numpy as np
import pygfx as gfx
from colour.hints import LiteralColourspaceModel
from colour.models import COLOURSPACE_MODELS, COLOURSPACE_MODELS_AXIS_LABELS
from colour.plotting import (
CONSTANTS_COLOUR_STYLE,
colourspace_model_axis_reorder,
)
from colour.utilities import as_int_array, validate_method

from colour_visuals.common import (
DEFAULT_FLOAT_DTYPE_WGPU,
)

__author__ = "Colour Developers"
__copyright__ = "Copyright 2023 Colour Developers"
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
__maintainer__ = "Colour Developers"
__email__ = "[email protected]"
__status__ = "Production"

__all__ = ["VisualAxes"]


class VisualAxes(gfx.Group):
"""
Create an axes visual.
Parameters
----------
model
Colourspace model, see :attr:`colour.COLOURSPACE_MODELS` attribute for
the list of supported colourspace models.
size
Size of the axes.
Examples
--------
>>> import os
>>> from colour.utilities import suppress_stdout
>>> from wgpu.gui.auto import WgpuCanvas
>>> with suppress_stdout():
... canvas = WgpuCanvas(size=(960, 540))
... scene = gfx.Scene()
... scene.add(
... gfx.Background(
... None, gfx.BackgroundMaterial(np.array([0.18, 0.18, 0.18]))
... )
... )
... visual = VisualAxes("CIE Lab")
... camera = gfx.PerspectiveCamera(50, 16 / 9)
... camera.show_object(visual, up=np.array([0, 0, 1]), scale=1.25)
... scene.add(visual)
... if os.environ.get("CI") is None:
... gfx.show(scene, camera=camera, canvas=canvas)
...
.. image:: ../_static/Plotting_VisualAxes.png
:align: center
:alt: visual-grid
"""

def __init__(
self,
model: LiteralColourspaceModel | str = "CIE xyY",
size: int = 1,
):
super().__init__()

size = int(size)
model = validate_method(model, tuple(COLOURSPACE_MODELS))

axes_positions = np.array(
[
[0, 0, 0],
[1, 0, 0],
[0, 0, 0],
[0, 1, 0],
[0, 0, 0],
[0, 0, 1],
],
dtype=DEFAULT_FLOAT_DTYPE_WGPU,
)
axes_positions *= size

axes_colours = np.array(
[
[1, 0, 0, 1],
[1, 0, 0, 1],
[0, 1, 0, 1],
[0, 1, 0, 1],
[0, 0, 1, 1],
[0, 0, 1, 1],
],
dtype=DEFAULT_FLOAT_DTYPE_WGPU,
)

self._axes_helper = gfx.Line(
gfx.Geometry(positions=axes_positions, colors=axes_colours),
gfx.LineSegmentMaterial(color_mode="vertex", thickness=2),
)
self.add(self._axes_helper)

labels = np.array(COLOURSPACE_MODELS_AXIS_LABELS[model])[
as_int_array(colourspace_model_axis_reorder([0, 1, 2], model))
]

def latex_to_text(text):
return (
text.replace("prime", "'")
.replace("^", "")
.replace("_", "")
.replace("{", "")
.replace("}", "")
.replace("$", "")
)

self._x_text = gfx.Text(
gfx.TextGeometry(
latex_to_text(labels[0]),
font_size=CONSTANTS_COLOUR_STYLE.font_size.medium * 2,
screen_space=True,
anchor="Middle-Center",
),
gfx.TextMaterial(color=np.array([1, 0, 0])), # pyright: ignore
)
self._x_text.local.position = np.array([1.1, 0, 0])
self.add(self._x_text)

self._y_text = gfx.Text(
gfx.TextGeometry(
latex_to_text(labels[1]),
font_size=CONSTANTS_COLOUR_STYLE.font_size.medium * 2,
screen_space=True,
anchor="Middle-Center",
),
gfx.TextMaterial(color=np.array([0, 1, 0])), # pyright: ignore
)
self._y_text.local.position = np.array([0, 1.1, 0])
self.add(self._y_text)

self._z_text = gfx.Text(
gfx.TextGeometry(
latex_to_text(labels[2]),
font_size=CONSTANTS_COLOUR_STYLE.font_size.medium * 2,
screen_space=True,
anchor="Middle-Center",
),
gfx.TextMaterial(color=np.array([0, 0, 1])), # pyright: ignore
)
self._z_text.local.position = np.array([0, 0, 1.1])
self.add(self._z_text)


if __name__ == "__main__":
scene = gfx.Scene()

scene.add(
gfx.Background(
None, gfx.BackgroundMaterial(np.array([0.18, 0.18, 0.18]))
)
)

visual_1 = VisualAxes(model="CIE Lab")
scene.add(visual_1)

gfx.show(scene, up=np.array([0, 0, 1]))
Binary file added docs/_static/Plotting_VisualAxes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 14 additions & 13 deletions docs/colour_visuals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ Chromaticity Diagram Visuals
VisualChromaticityDiagramCIE1960UCS
VisualChromaticityDiagramCIE1976UCS

Grid Visuals
------------

``colour_visuals``

.. currentmodule:: colour_visuals

.. autosummary::
:toctree: generated/
:template: class.rst

VisualGrid

Pointer's Gamut Visuals
-----------------------

Expand Down Expand Up @@ -86,6 +73,20 @@ Rösch-MacAdam Visuals

VisualRoschMacAdam

Helper Visuals
--------------

``colour_visuals``

.. currentmodule:: colour_visuals

.. autosummary::
:toctree: generated/
:template: class.rst

VisualAxes
VisualGrid

Common Utilities
----------------

Expand Down
2 changes: 2 additions & 0 deletions utilities/generate_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from colour.io import write_image
from wgpu.gui.offscreen import WgpuCanvas

from colour_visuals.axes import VisualAxes
from colour_visuals.diagrams import (
VisualChromaticityDiagram,
VisualChromaticityDiagramCIE1931,
Expand Down Expand Up @@ -74,6 +75,7 @@ def generate_documentation_plots(output_directory: str):
canvas.request_draw(lambda: renderer.render(scene, camera))

for visual_class, arguments in [
(VisualAxes, {"model": "CIE Lab"}),
(VisualSpectralLocus2D, {}),
(VisualSpectralLocus3D, {}),
(VisualChromaticityDiagram, {}),
Expand Down

0 comments on commit 4572df2

Please sign in to comment.