Skip to content
Snippets Groups Projects

color.py - add some more color methods

Merged wink requested to merge wink/firmware:color-2 into master

Adds a few methods to the color module

  • to_hsv() -> [h, s, v]
  • to_hsl() -> [h, s, l]
  • get_complementary() -> Color
  • change_hue()
  • change_saturation_hsv()
  • change_saturation_hsl()
  • change_value()
  • change_lightness()
  • darken() (uses change_sat_hsl)
  • brighten() (uses change_sat_hsl)
  • __mul__() -> Color
  • __add__() -> Color
  • __sub__() -> Color
Edited by wink

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • wink changed the description

    changed the description

  • wink mentioned in issue #30 (closed)

    mentioned in issue #30 (closed)

  • Just for reference:

    Flash Usage

                   text	   data	    bss	    dec	    hex	filename
    Master:      159756	    500	    588	 160844	  2744c	build/pycardium/pycardium.elf
    Your Branch: 161428	    500	    588	 162516	  27ad4	build/pycardium/pycardium.elf
                  +1672	     +0	     +0   +1672

    RAM Usage

    Master:        1712
    Your Branch:   2048
                   +336
  • Author Guest

    Thanks. I guess we can leave this open for a while and then someone needs to decide which of those make sense to include or not. I was going off the description int #30 (closed) - so I hope I didn't go too far off.

  • I like the functions you implemented! If it is ok, I'd like to request 3 more:

    # Multiply with a scalar to dim the color.  Clamp at 0 and 255
    # to ensure the color stays within bounds.
    color2 = color * 0.5
    
    # Adding two colors.  Clamped like above
    color3 = color1 + color2
    
    # Similarly, subtracting (this one is not too important)
    color3 = color2 - color1

    You can implement this behavior using some special methods.

  • Author Guest

    Sure, will see what I can do

  • Thanks a lot!

  • wink added 6 commits

    added 6 commits

    Compare with previous version

  • wink changed the description

    changed the description

  • Ooo, this is very cool!

    MicroPython v1.11-37-g62f004ba4 on 2019-07-17; card10 with max32665
    Type "help()" for more information.
    >>> import color
    >>> color.RED + color.GREEN
    Color(red=255, green=255, blue=0)
    >>> color.RED + color.GREEN == color.YELLOW
    True

    Sadly multiplication isn't quite working:

    >>> color.RED * 0.5
    Traceback (most recent call last):
      File "<stdin>", in <module>
    TypeError: unsupported types for : 'Color', 'float'
    >>> 0.5 * color.RED
    Traceback (most recent call last):
      File "<stdin>", in <module>
    TypeError: unsupported types for : 'float', 'Color'

    It's working on CPython though so this might be a MicroPython specific problem. I'll open a bug-issue, this shouldn't block your MR. Awesome work, thank you very much!

  • merged

  • rahix mentioned in commit ddcd810e

    mentioned in commit ddcd810e

  • Author Guest
    $ micropython
    MicroPython v1.11 on 2019-07-18; linux version
    Use Ctrl-D to exit, Ctrl-E for paste mode
    >>> import color
    >>> color.RED * 0.5
    Color(red=128, green=0, blue=0)
    >>> color.RED * 2
    Color(red=255, green=0, blue=0)

    Ah I see, it's my micropython that just works ;)

  • Yes, the Unix port probably has MICROPY_PY_ALL_SPECIAL_METHODS enabled while it was turned off for ours. I fixed it in ddcd810e. :)

  • Hey, you can now interpolate between two colors:

    import leds, htmlcolor, utime
    
    def lerp(c1, c2, t):
        return c2 * t + c1 * (1 - t)
    
    for i in range(0, 20):
        t = i / 20
        leds.set(
            leds.TOP_LEFT,
            lerp(htmlcolor.DEEPPINK, htmlcolor.INDIGO, t),
        )
        utime.sleep_ms(50)
Please register or sign in to reply
Loading