From 1eb75e24e7dfe1105a4f96fb520406febe6cfaa7 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Wed, 12 Aug 2020 12:18:24 +0200 Subject: [PATCH] api/parameters/Color: generalize color type First step towards making a color type with HSV representation. Colors such as Color.RED or Color.BLUE continue to exist, but now you can also define your own colors: DARK_RED = Color(h=0, s=100, v=50) --- doc/api/parameters.rst | 22 +++++++++++----------- pybricks/parameters.py | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/doc/api/parameters.rst b/doc/api/parameters.rst index b75ab15..e8bf3b2 100644 --- a/doc/api/parameters.rst +++ b/doc/api/parameters.rst @@ -89,20 +89,20 @@ .. rubric:: Saturated colors - .. data:: RED - .. data:: ORANGE - .. data:: YELLOW - .. data:: GREEN - .. data:: CYAN - .. data:: BLUE - .. data:: VIOLET - .. data:: MAGENTA + .. autoattribute:: RED + .. autoattribute:: ORANGE + .. autoattribute:: YELLOW + .. autoattribute:: GREEN + .. autoattribute:: CYAN + .. autoattribute:: BLUE + .. autoattribute:: VIOLET + .. autoattribute:: MAGENTA .. rubric:: Unsaturated colors - .. data:: BLACK - .. data:: GRAY - .. data:: WHITE + .. autoattribute:: BLACK + .. autoattribute:: GRAY + .. autoattribute:: WHITE .. autoclass:: pybricks.parameters.Button :no-members: diff --git a/pybricks/parameters.py b/pybricks/parameters.py index 7c3b2ca..cfc1579 100644 --- a/pybricks/parameters.py +++ b/pybricks/parameters.py @@ -27,20 +27,36 @@ class _PybricksEnum(_Enum, metaclass=_PybricksEnumMeta): return str(self) -class Color(_PybricksEnum): +class Color: """Light or surface color.""" - BLACK = 1 - BLUE = 2 - GREEN = 3 - YELLOW = 4 - RED = 5 - WHITE = 6 - ORANGE = 8 - VIOLET = 9 - MAGENTA = 10 - CYAN = 11 - GRAY = 12 + def __init__(self, h, s=100, v=100, name='custom'): + self.h = h % 360 + self.s = max(0, min(s, 100)) + self.v = max(0, min(v, 100)) + self.name = name + + def __repr__(self): + return "Color({}, {}, {}, '{}')".format( + self.h, self.s, self.v, self.name + ) + + def __eq__(self, other): + return (isinstance(other, Color) and + self.h == other.h and self.s == other.s and self.v == other.v) + + +Color.BLACK = Color(0, 0, 0, 'BLACK') +Color.GRAY = Color(0, 0, 50, 'GRAY') +Color.WHITE = Color(0, 0, 100, 'WHITE') +Color.RED = Color(0, 100, 100, 'RED') +Color.ORANGE = Color(30, 100, 100, 'ORANGE') +Color.YELLOW = Color(60, 100, 100, 'YELLOW') +Color.GREEN = Color(120, 100, 100, 'GREEN') +Color.CYAN = Color(180, 100, 100, 'CYAN') +Color.BLUE = Color(240, 100, 100, 'BLUE') +Color.VIOLET = Color(270, 100, 100, 'VIOLET') +Color.MAGENTA = Color(300, 100, 100, 'MAGENTA') class Port(_PybricksEnum):