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)
This commit is contained in:
Laurens Valk
2020-08-12 12:18:24 +02:00
parent be8592f096
commit 1eb75e24e7
2 changed files with 39 additions and 23 deletions
+11 -11
View File
@@ -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:
+28 -12
View File
@@ -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):