peterhinch / micropython-nano-gui

A lightweight MicroPython GUI library for display drivers based on framebuf class
MIT License
511 stars 88 forks source link

Rainbow #14

Closed IhorNehrutsa closed 3 years ago

IhorNehrutsa commented 3 years ago

Idea from https://basecase.org/env/on-rainbows

Video https://drive.google.com/file/d/1Pa9en9BPZnuL-qrnCP3k3uhoWlH9tDZl/view?usp=sharing

peterhinch commented 3 years ago

Regarding any work on colors I suggest stepping back to consider overall purpose and applicability. The latter goes beyond this one driver to the whole of nano-gui, and extends to my touch GUIs. All use the same convention for defining colors, with an rgb(r, g, b) method taking args in range 0-255 and producing something meaningful to the GUI.

Perhaps color work should be an entirely new and separate project, with functions producing (r, g, b) tuples capable of being unpacked and presented to rgb() methods?

IhorNehrutsa commented 3 years ago

Perhaps color work should be an entirely new and separate project, with functions producing (r, g, b) tuples capable of being unpacked and presented to rgb() methods?

I think it is not need to make new separate project. We can upsize(grow) micropython-nano-gui capabilities.

I propose to add inverse functions for create_color(idx, r, g, b) in micropython-nano-gui\gui\core\colors.py

# unpack_color_to_rgb() is inverse function of create_color() 
# _r, _g, _b = unpack_color_to_rgb(create_color(idx, r, g, b))
# Note: _r, _g, _b may not equal to r, g, b if driver uses rgb565, rgb444 or other reduced color maps.
def unpack_color_to_rgb(color):
    # color is the driver-dependent instance.
    # color is an index in 'lut' if 'lut' is present in the display driver, or
    # color is rgb888, rgb565, rgb444, etc. according to the driver.
    return SSD.unpack_rgb(color)

and @staticmethod unpack_to_rgb() in each of SSD drivers

    # This is inverse functions for rgb(r, g, b)
    @staticmethod
    def unpack_to_rgb(color_index:ptr16):
        reinverted = self.lut[color_index] ^ 0xffff
        r = reinverted & 0xf8
        g = (reinverted  >> 11) & 0x1c | (inverted << 5) & 0xe0
        b = (reinverted >> 5) & 0xf8
        return r, g, b

This will allow to manipulate with factual (r, g, b) color tuple of the SSD driver and user applications. I added these functions in https://github.com/peterhinch/micropython-nano-gui/pull/15

IhorNehrutsa commented 3 years ago

About the rainbow.py. rainbow.py is not typical for the micropython-nano-gui, I can't imagine many applications like it, but it shows how to redefine colors and may be useful for users.

I think rainbow.py is any SSD color driver compatible.

IhorNehrutsa commented 3 years ago

More rainbows! https://habr.com/ru/company/timeweb/blog/554778/ ;-)

peterhinch commented 3 years ago

I have several problems with this.

  1. I can't see the application for a reverse RGB lookup.
  2. Until now nobody has requested this feature.
  3. A lookup only needs a dict whose key is the color value, values being (r, g, b) tuples.
  4. Implementing this would mean my modifying every single graphics driver I have written, assembling the hardware and re-testing. This really does not appeal to me.

Regarding the rainbow (sinebow) function I absolutely see its merit and application, but I don't believe its place is in this library as its potential usage is much more general.

IhorNehrutsa commented 3 years ago

Ok. Let's keep in mind.