Skip to content

Commit

Permalink
Added background color to the markers
Browse files Browse the repository at this point in the history
- Drop the auto background
  • Loading branch information
vallsv committed Dec 18, 2023
1 parent 0433c6e commit 8fb1dd1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/silx/gui/plot/items/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
45 changes: 38 additions & 7 deletions src/silx/gui/plot/items/marker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


import logging
import numpy

from ....utils.proxy import docstring
from .core import (
Expand All @@ -44,6 +45,8 @@
)
from silx import config
from silx.gui import qt
from silx.gui import colors


_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -75,28 +78,24 @@ def __init__(self):

self._x = None
self._y = None
self._bgColor: colors.RGBAColorType | numpy.ndarray | None = None
self._constraint = self._defaultConstraint
self.__isBeingDragged = False

def _addRendererCall(self, backend, symbol=None, linestyle="-", linewidth=1):
"""Perform the update of the backend renderer"""
color = self.getColor()
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)
)
return backend.addMarker(
x=self.getXPosition(),
y=self.getYPosition(),
text=self.getText(),
color=color,
color=self.getColor(),
symbol=symbol,
linestyle=linestyle,
linewidth=linewidth,
constraint=self.getConstraint(),
yaxis=self.getYAxis(),
font=self._font, # Do not use getFont to spare creating a new QFont
bgcolor=bgColor,
bgcolor=self.getBackgroundColor(),
)

def _addBackendRenderer(self, backend):
Expand Down Expand Up @@ -147,6 +146,38 @@ 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 | numpy.ndarray | None:
"""Returns the RGBA background color of the item
:rtype: 4-tuple of float in [0, 1] or array of colors
"""
return self._bgColor

def setBackgroundColor(self, color, copy=True):
"""Set item background color
:param color: color(s) to be used
:type color: str ("#RRGGBB") or (npoints, 4) unsigned byte array or
one of the predefined color names defined in colors.py
:param bool copy: True (Default) to get a copy,
False to use internal representation (do not modify!)
"""
if color is None:
pass
elif isinstance(color, (str, qt.QColor)):
color = colors.rgba(color)
else:
color = numpy.array(color, copy=copy)
# TODO more checks + improve color array support
if color.ndim == 1: # Single RGBA color
color = colors.rgba(color)
else: # Array of colors
assert color.ndim == 2

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
Expand Down

0 comments on commit 8fb1dd1

Please sign in to comment.