gpiozero
gpiozero copied to clipboard
Add gamma parameter to RGBLED
The RGBLED
doesn't gamma correct it's output power. This creates color mixing issues.
Purpose of change: Adding a gamma parameter provides better perceptual color accuracy.
Please provide example usage of gpiozero with your suggested change:
Here's my sub-class of RGBLED that adds the gamma correction as a work-around.
import math
from functools import cached_property
from typing import Any, Tuple
from gpiozero import RGBLED as BaseRGBLED
class RGBLED(BaseRGBLED): # type: ignore
_gamma: float
def __init__(self, *args: Any, gamma: float = 2.3, **kwargs: Any) -> None:
self._gamma = gamma
super().__init__(*args, **kwargs)
@cached_property
def _inverse_gamma(self) -> float:
return 1.0 / self._gamma
@property
def value(self) -> Tuple[float, float, float]:
r, g, b = tuple(math.pow(val, self._inverse_gamma) for val in super().value)
return r, g, b
@value.setter
def value(self, value: Tuple[float, float, float]) -> None:
r, g, b = tuple(math.pow(val, self._gamma) for val in value)
super(RGBLED, self.__class__).value.fset(self, (r, g, b))