From d407658a6140ccce54f420b6ee7e9da4a7a20166 Mon Sep 17 00:00:00 2001 From: Rahix <rahix@rahix.de> Date: Fri, 5 Jul 2019 21:30:29 +0200 Subject: [PATCH] feat(py): Add a color module Signed-off-by: Rahix <rahix@rahix.de> --- pycardium/modules/py/color.py | 44 ++++++++++++++++++++++++++++++++ pycardium/modules/py/foo.py | 8 ------ pycardium/modules/py/meson.build | 2 +- 3 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 pycardium/modules/py/color.py delete mode 100644 pycardium/modules/py/foo.py diff --git a/pycardium/modules/py/color.py b/pycardium/modules/py/color.py new file mode 100644 index 000000000..b072ef97f --- /dev/null +++ b/pycardium/modules/py/color.py @@ -0,0 +1,44 @@ +import ucollections + +_ColorTuple = ucollections.namedtuple("Color", ["red", "green", "blue"]) + + +class Color(_ColorTuple): + @classmethod + def from_hex(cls, color): + red = (color & 0xff0000) >> 16 + green = (color & 0x00ff00) >> 8 + blue = (color & 0x0000ff) + return cls(red, green, blue) + + def __str__(self): + # Return the color in hex + return "#{:02x}{:02x}{:02x}".format( + self.red, self.green, self.blue + ) + + +Color.BLACK = Color.from_hex(0x000000) +Color.WHITE = Color.from_hex(0xffffff) +Color.RED = Color.from_hex(0xff0000) +Color.GREEN = Color.from_hex(0x00ff00) +Color.YELLOW = Color.from_hex(0xffff00) +Color.BLUE = Color.from_hex(0x0000ff) +Color.MAGENTA = Color.from_hex(0xff00ff) +Color.CYAN = Color.from_hex(0x00ffff) + + +# Add the colors and constructors to the module for convenience +# This allows you to do the following: +# +# import colors +# +# colors.BLACK +# colors.from_hex(0xc6c6c6) +globals().update( + { + n: getattr(Color, n) + for n in dir(Color) + if n.startswith("from") or n.isupper() + } +) diff --git a/pycardium/modules/py/foo.py b/pycardium/modules/py/foo.py deleted file mode 100644 index e95c53d13..000000000 --- a/pycardium/modules/py/foo.py +++ /dev/null @@ -1,8 +0,0 @@ -# Foo Module - - -def bar(): - print("Hello from foo!") - - -X = 3.14 diff --git a/pycardium/modules/py/meson.build b/pycardium/modules/py/meson.build index 29795f41a..27d98ec7f 100644 --- a/pycardium/modules/py/meson.build +++ b/pycardium/modules/py/meson.build @@ -1,5 +1,5 @@ python_modules = files( - 'foo.py', + 'color.py', ) frozen_modules = mpy_cross.process(python_modules) -- GitLab