diff --git a/src/silx/gui/plot/backends/BackendBase.py b/src/silx/gui/plot/backends/BackendBase.py index 51f8a1085c..ded09a0455 100755 --- a/src/silx/gui/plot/backends/BackendBase.py +++ b/src/silx/gui/plot/backends/BackendBase.py @@ -37,6 +37,7 @@ from collections.abc import Callable import weakref +from silx.gui.colors import RGBAColorType from ... import qt @@ -218,6 +219,7 @@ def addMarker( constraint: Callable[[float, float], tuple[float, float]] | None, yaxis: str, font: qt.QFont, + bgcolor: RGBAColorType | None, ) -> object: """Add a point, vertical line or horizontal line marker to the plot. @@ -227,6 +229,7 @@ def addMarker( If None, the marker is a vertical line. :param text: Text associated to the marker (or None for no text) :param color: Color to be used for instance 'blue', 'b', '#FF0000' + :param bgcolor: Text background color to be used for instance 'blue', 'b', '#FF0000' :param symbol: Symbol representing the marker. Only relevant for point markers where X and Y are not None. Value in: diff --git a/src/silx/gui/plot/backends/BackendMatplotlib.py b/src/silx/gui/plot/backends/BackendMatplotlib.py index 0fc7f71f0d..e911510065 100755 --- a/src/silx/gui/plot/backends/BackendMatplotlib.py +++ b/src/silx/gui/plot/backends/BackendMatplotlib.py @@ -23,6 +23,8 @@ # ###########################################################################*/ """Matplotlib Plot backend.""" +from __future__ import annotations + __authors__ = ["V.A. Sole", "T. Vincent, H. Payno"] __license__ = "MIT" __date__ = "21/12/2018" @@ -67,6 +69,7 @@ ) from ...qt import inspect as qt_inspect from .... import config +from silx.gui.colors import RGBAColorType _PATCH_LINESTYLE = { "-": "solid", @@ -941,7 +944,18 @@ def addShape( return item def addMarker( - self, x, y, text, color, symbol, linestyle, linewidth, constraint, yaxis, font + self, + x, + y, + text, + color, + symbol, + linestyle, + linewidth, + constraint, + yaxis, + font, + bgcolor: RGBAColorType | None, ): textArtist = None fontProperties = None if font is None else qFontToFontProperties(font) @@ -968,6 +982,7 @@ def addMarker( y, text, color=color, + backgroundcolor=bgcolor, horizontalalignment="left", fontproperties=fontProperties, ) @@ -982,6 +997,7 @@ def addMarker( 1.0, text, color=color, + backgroundcolor=bgcolor, horizontalalignment="left", verticalalignment="top", fontproperties=fontProperties, @@ -997,6 +1013,7 @@ def addMarker( y, text, color=color, + backgroundcolor=bgcolor, horizontalalignment="right", verticalalignment="top", fontproperties=fontProperties, diff --git a/src/silx/gui/plot/backends/BackendOpenGL.py b/src/silx/gui/plot/backends/BackendOpenGL.py index 019a38b84c..035cc767fd 100755 --- a/src/silx/gui/plot/backends/BackendOpenGL.py +++ b/src/silx/gui/plot/backends/BackendOpenGL.py @@ -23,6 +23,8 @@ # ############################################################################*/ """OpenGL Plot backend.""" +from __future__ import annotations + __authors__ = ["T. Vincent"] __license__ = "MIT" __date__ = "21/12/2018" @@ -42,6 +44,7 @@ from ... import _glutils as glu from . import glutils from .glutils.PlotImageFile import saveImageToFile +from silx.gui.colors import RGBAColorType _logger = logging.getLogger(__name__) @@ -90,7 +93,18 @@ def __init__( class _MarkerItem(dict): def __init__( - self, x, y, text, color, symbol, linestyle, linewidth, constraint, yaxis, font + self, + x, + y, + text, + color, + symbol, + linestyle, + linewidth, + constraint, + yaxis, + font, + bgcolor, ): super(_MarkerItem, self).__init__() @@ -114,6 +128,7 @@ def __init__( "linewidth": linewidth, "yaxis": yaxis, "font": font, + "bgcolor": bgcolor, } ) @@ -590,10 +605,7 @@ def _renderItems(self, overlay=False): continue color = item["color"] - intensity = color[0] * 0.299 + color[1] * 0.587 + color[2] * 0.114 - bgColor = ( - (1.0, 1.0, 1.0, 0.75) if intensity <= 0.5 else (0.0, 0.0, 0.0, 0.75) - ) + bgColor = item["bgcolor"] if xCoord is None or yCoord is None: if xCoord is None: # Horizontal line in data space pixelPos = self._plotFrame.dataToPixel( @@ -612,7 +624,7 @@ def _renderItems(self, overlay=False): item["font"], x, y, - color=item["color"], + color=color, bgColor=bgColor, align=glutils.RIGHT, valign=glutils.BOTTOM, @@ -625,7 +637,7 @@ def _renderItems(self, overlay=False): (0, width), (pixelPos[1], pixelPos[1]), style=item["linestyle"], - color=item["color"], + color=color, width=item["linewidth"], ) context.matrix = self.matScreenProj @@ -645,7 +657,7 @@ def _renderItems(self, overlay=False): item["font"], x, y, - color=item["color"], + color=color, bgColor=bgColor, align=glutils.LEFT, valign=glutils.TOP, @@ -658,7 +670,7 @@ def _renderItems(self, overlay=False): (pixelPos[0], pixelPos[0]), (0, height), style=item["linestyle"], - color=item["color"], + color=color, width=item["linewidth"], ) context.matrix = self.matScreenProj @@ -687,7 +699,7 @@ def _renderItems(self, overlay=False): item["font"], x, y, - color=item["color"], + color=color, bgColor=bgColor, align=glutils.LEFT, valign=valign, @@ -701,7 +713,7 @@ def _renderItems(self, overlay=False): (pixelPos[0],), (pixelPos[1],), marker=item["symbol"], - color=item["color"], + color=color, size=11, ) context.matrix = self.matScreenProj @@ -1084,11 +1096,32 @@ def addShape( ) def addMarker( - self, x, y, text, color, symbol, linestyle, linewidth, constraint, yaxis, font + self, + x, + y, + text, + color, + symbol, + linestyle, + linewidth, + constraint, + yaxis, + font, + bgcolor: RGBAColorType | None, ): font = qt.QApplication.instance().font() if font is None else font return _MarkerItem( - x, y, text, color, symbol, linestyle, linewidth, constraint, yaxis, font + x, + y, + text, + color, + symbol, + linestyle, + linewidth, + constraint, + yaxis, + font, + bgcolor, ) # Remove methods diff --git a/src/silx/gui/plot/backends/glutils/GLText.py b/src/silx/gui/plot/backends/glutils/GLText.py index 15c001e0de..43501015f0 100644 --- a/src/silx/gui/plot/backends/glutils/GLText.py +++ b/src/silx/gui/plot/backends/glutils/GLText.py @@ -41,6 +41,7 @@ from .... import qt from ...._glutils import font, gl, Context, Program, Texture from .GLSupport import mat4Translate +from silx.gui.colors import RGBAColorType class _Cache: @@ -140,7 +141,7 @@ def __init__( x: float = 0.0, y: float = 0.0, color: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 1.0), - bgColor: tuple[float, float, float, float] | None = None, + bgColor: RGBAColorType | None = None, align: str = LEFT, valign: str = BASELINE, rotate: float = 0.0, diff --git a/src/silx/gui/plot/items/core.py b/src/silx/gui/plot/items/core.py index 2453b3afee..50386f4c19 100644 --- a/src/silx/gui/plot/items/core.py +++ b/src/silx/gui/plot/items/core.py @@ -154,6 +154,9 @@ class ItemChangedType(enum.Enum): FONT = "fontChanged" """Item's text font changed flag.""" + BACKGROUND_COLOR = "backgroundColorChanged" + """Item's text background color changed flag.""" + class Item(qt.QObject): """Description of an item of the plot""" diff --git a/src/silx/gui/plot/items/marker.py b/src/silx/gui/plot/items/marker.py index da5bd75f43..b3da451f5a 100755 --- a/src/silx/gui/plot/items/marker.py +++ b/src/silx/gui/plot/items/marker.py @@ -31,6 +31,7 @@ import logging +import numpy from ....utils.proxy import docstring from .core import ( @@ -44,6 +45,8 @@ ) from silx import config from silx.gui import qt +from silx.gui import colors + _logger = logging.getLogger(__name__) @@ -75,6 +78,7 @@ def __init__(self): self._x = None self._y = None + self._bgColor: colors.RGBAColorType | None = None self._constraint = self._defaultConstraint self.__isBeingDragged = False @@ -91,6 +95,7 @@ def _addRendererCall(self, backend, symbol=None, linestyle="-", linewidth=1): constraint=self.getConstraint(), yaxis=self.getYAxis(), font=self._font, # Do not use getFont to spare creating a new QFont + bgcolor=self.getBackgroundColor(), ) def _addBackendRenderer(self, backend): @@ -141,6 +146,23 @@ def setFont(self, font: qt.QFont | None): self._font = None if font is None else qt.QFont(font) self._updated(ItemChangedType.FONT) + def getBackgroundColor(self) -> colors.RGBAColorType | None: + """Returns the RGBA background color of the item""" + return self._bgColor + + def setBackgroundColor(self, color): + """Set item text background color + + :param color: color(s) to be used as a str ("#RRGGBB") or (npoints, 4) + unsigned byte array or one of the predefined color names + defined in colors.py + """ + if color is not None: + color = colors.rgba(color) + if self._bgColor != color: + self._bgColor = color + self._updated(ItemChangedType.BACKGROUND_COLOR) + def getXPosition(self): """Returns the X position of the marker line in data coordinates