gpiozero icon indicating copy to clipboard operation
gpiozero copied to clipboard

Add gamma parameter to RGBLED

Open kevinastone opened this issue 5 years ago • 0 comments

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))

kevinastone avatar Dec 13 '19 22:12 kevinastone