Skip to content

Commit

Permalink
Added support for colormap inversion
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRaybaut committed Mar 7, 2024
1 parent 738af55 commit 4ef2dae
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 311 deletions.
8 changes: 6 additions & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ omit =
*/qwt/*
*/guidata/*

exclude_lines =
[report]
exclude_also =
def __repr__
raise AssertionError
raise NotImplementedError
if __name__ == .__main__.:
if TYPE_CHECKING:
if TYPE_CHECKING:
17 changes: 15 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
# Changelog #

## Version 2.2.1 ##
## Version 2.3.0 ##

Changes:
In this release, test coverage is 75%.

New features:

* Added support for colormap inversion:
* The user can now invert the colormap of an image item:
* From the image parameters dialog ("Invert colormap" checkbox)
* From the plot context menu (right-click on the image item)
* `BaseImageItem.set_color_map` method takes a new `invert` parameter (which
defaults to `None`, meaning that the behavior is unchanged)
* New `ReverseColormapTool`: registered by default in the plot widget, like the
`ColormapTool` (add the "Invert colormap" entry in the context menu of the image)

Other changes:

* Image plot items deserialization:
* When an image plot item is deserialized, and needs to be reloaded from a file,
Expand Down
1 change: 1 addition & 0 deletions doc/features/tools/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The `tools` module provides the following tools:
* :py:class:`.tools.ItemListPanelTool`
* :py:class:`.tools.ContrastPanelTool`
* :py:class:`.tools.ColormapTool`
* :py:class:`.tools.ReverseColormapTool`
* :py:class:`.tools.XCSPanelTool`
* :py:class:`.tools.YCSPanelTool`
* :py:class:`.tools.CrossSectionTool`
Expand Down
2 changes: 2 additions & 0 deletions doc/features/tools/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ Image tools
:members:
.. autoclass:: plotpy.tools.ColormapTool
:members:
.. autoclass:: plotpy.tools.ReverseColormapTool
:members:
.. autoclass:: plotpy.tools.XCSPanelTool
:members:
.. autoclass:: plotpy.tools.YCSPanelTool
Expand Down
2 changes: 1 addition & 1 deletion plotpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
.. _GitHub: https://github.com/PierreRaybaut/plotpy
"""

__version__ = "2.2.1"
__version__ = "2.3.0"
__VERSION__ = tuple([int(number) for number in __version__.split(".")])

# --- Important note: DATAPATH and LOCALEPATH are used by guidata.configtools
Expand Down
8 changes: 7 additions & 1 deletion plotpy/items/image/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,15 @@ def set_background_color(self, qcolor: QColor | str) -> None:
else:
self.lut = (a, b, np.uint32(QG.QColor(qcolor).rgb() & 0xFFFFFF), cmap)

def set_color_map(self, name_or_table: str | EditableColormap) -> None:
def set_color_map(
self, name_or_table: str | EditableColormap, invert: bool | None = None
) -> None:
"""Set colormap
Args:
name_or_table: Colormap name or colormap
invert: True to invert colormap, False otherwise (Default value = None,
i.e. do not change the default behavior)
"""
if name_or_table is self.cmap_table:
# This avoids rebuilding the LUT all the time
Expand All @@ -429,6 +433,8 @@ def set_color_map(self, name_or_table: str | EditableColormap) -> None:
table = get_cmap(name_or_table)
else:
table = name_or_table
if invert is not None:
table.invert = invert
self.cmap_table = table
self.cmap = table.colorTable(FULLRANGE)
cmap_a = self.lut[3]
Expand Down
9 changes: 6 additions & 3 deletions plotpy/items/image/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from plotpy.interfaces import IItemType
from plotpy.items import RawImageItem, XYImageItem
from plotpy.styles import ImageFilterParam, ItemParameters
from plotpy.widgets.colormap.widget import EditableColormap


# ==============================================================================
Expand Down Expand Up @@ -188,18 +189,20 @@ def move_with_selection(self, delta_x: float, delta_y: float) -> None:
self.border_rect.move_with_selection(delta_x, delta_y)

def set_color_map(
self, name_or_table: str | qwt.color_map.QwtLinearColorMap
self, name_or_table: str | EditableColormap, invert: bool | None = None
) -> None:
"""Set colormap
Args:
name_or_table: Colormap name or colormap
invert: True to invert colormap, False otherwise (Default value = None,
i.e. do not change the default behavior)
"""
if self.use_source_cmap:
if self.image is not None:
self.image.set_color_map(name_or_table)
self.image.set_color_map(name_or_table, invert)
else:
BaseImageItem.set_color_map(self, name_or_table)
BaseImageItem.set_color_map(self, name_or_table, invert)

def get_color_map(self) -> qwt.color_map.QwtLinearColorMap:
"""Get colormap"""
Expand Down
9 changes: 4 additions & 5 deletions plotpy/items/image/image_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from qtpy.QtGui import QColor, QPainter

from plotpy.interfaces import IItemType
from plotpy.widgets.colormap.widget import EditableColormap

try:
from plotpy._scaler import INTERP_NEAREST, _scale_rect, _scale_xy
Expand Down Expand Up @@ -806,16 +807,14 @@ def set_background_color(self, qcolor: QColor | str) -> None:
self.lut = None

def set_color_map(
self, name_or_table: str | qwt.color_map.QwtLinearColorMap
self, name_or_table: str | EditableColormap, invert: bool | None = None
) -> None:
"""Set colormap
Args:
name_or_table: Colormap name or colormap
.. warning::
This method is not implemented for this item type.
invert: True to invert colormap, False otherwise (Default value = None,
i.e. do not change the default behavior)
"""
self.lut = None

Expand Down
Loading

0 comments on commit 4ef2dae

Please sign in to comment.