Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support brightness in RGBLED #92

Open
liminspace opened this issue Dec 21, 2022 · 0 comments
Open

Support brightness in RGBLED #92

liminspace opened this issue Dec 21, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@liminspace
Copy link

It would be nice to support brightness for RGBLED.
I implemented it as possible fourth item of a color value.

class RGBLED(OutputDevice, PinsMixin):
    def __init__(
        self, red=None, green=None, blue=None, active_high=True, initial_value=(0, 0, 0), pwm=True
    ):
        ...
        super().__init__(active_high, self._apply_brightness(initial_value))

    def _apply_brightness(self, value):
        if isinstance(value, tuple) and len(value) == 4:
            brightness = value[3]
            value = tuple(v * brightness for v in value[:3])
        return value

    @value.setter
    def value(self, value):
        self._stop_change()
        self._write(self._apply_brightness(value))

    @color.setter
    def color(self, value):
        if len(value) == 4:
            self.value = tuple(self._from_255(v) for v in value[:3]) + (value[3],)
        else:
            self.value = tuple(self._from_255(v) for v in value)

for example:

rgbled.color = (255, 200, 50, 0.5)

The range of brightness must be 0.0...1.0.
I use it in my project and I see it to be quite useful.
It can be implemented better, like using some logarithmic curve for percentage/brightness, but for most cases it's not so important.

@martinohanlon martinohanlon added the enhancement New feature or request label Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants