summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hogenson <rayhogenson@openmailbox.org>2016-09-27 17:28:45 -0400
committerRaymond Hogenson <rayhogenson@openmailbox.org>2016-09-27 17:28:45 -0400
commit0bf53f9ddd3aab57a82999d93955c05eedf620ce (patch)
tree657c413568bda5928ec5a0954107f1e27614ccaa
parent2266f5d72c634c66cc0bfbfa1e636f2556ab2ddb (diff)
downloadbridge-0bf53f9ddd3aab57a82999d93955c05eedf620ce.tar.zst
Move color vectors into separate file
RGB vector has additionally been added, and color mixing happens in RGB.
-rw-r--r--bridge.py68
-rw-r--r--color.py84
2 files changed, 91 insertions, 61 deletions
diff --git a/bridge.py b/bridge.py
index 876e84d..22d8da2 100644
--- a/bridge.py
+++ b/bridge.py
@@ -1,6 +1,7 @@
#!/usr/bin/python
"""Simple demo of oscillating colors."""
from __future__ import division
+import color
import colorsys
import lumiversepython
import math
@@ -15,55 +16,6 @@ MIN_TIME = 0.3
MAX_HUE = 169 / 360
MIN_HUE = 250 / 360
-class Vector(object):
- """N-dimensional vector class."""
- def __init__(self, *components):
- """Create a vector from its components."""
- self.components = components
-
- def __add__(self, other):
- """Add two vectors."""
- return Vector(
- *map(lambda x, y: x + y, zip(self.components, other.components))
- )
-
- def __sub__(self, other):
- """Subtract two vectors."""
- return Vector(
- *map(lambda x, y: x - y, zip(self.components, other.components))
- )
-
- def __mul__(self, other):
- """Multiply a vector by a constant."""
- return Vector(*map(lambda x: x * other, self.components))
-
- def __rmul__(self, other):
- """Multiply a constant by a vector."""
- return self.__mul__(other)
-
- def __abs__(self):
- """Return the 2-norm of the vector."""
- return math.sqrt(sum(map(lambda x: x * x, self.components)))
-
-
-def HSV(Vector):
- """HSV 3-vector."""
- def __init__(self, hue, saturation, value)
- super().__init__(hue, saturation, value)
-
- def h(self):
- """Return hue."""
- return self.components[0]
-
- def s(self):
- """Return saturation."""
- return self.components[1]
-
- def v(self):
- """Return value."""
- return self.components[2]
-
-
def get_all_lights(rig):
"""Return a list of all panels."""
return [
@@ -77,12 +29,12 @@ def pick_HSV():
hue = random.random() * (MAX_HUE - MIN_HUE) + MIN_HUE
saturation = random.random()
value = random.random()
- return HSV(hue, saturation, value)
+ return color.HSV(hue, saturation, value)
def transition(start, end, step):
"""Return a vector equal to (end - start) * step + start."""
- return (end - start) * step + start
+ return (end - start) / abs(end - start) * step + start
def random_change():
@@ -92,19 +44,14 @@ def random_change():
return 1 / random.randint(min_time, max_time)
-def HSV_to_RGB(h):
- """Convert HSV to a 3-tuple of RGB."""
- return colorsys.hsv_to_rgb(h.h(), h.s(), h.v())
-
-
def demo(lights, rig):
"""Do some light things."""
light_status = [
{
'change': 0,
'light': light,
- 'current value': Vector(0, 0, 0),
- 'transitioning to': Vector(0, 0, 0),
+ 'current value': color.RGB(0, 0, 0),
+ 'transitioning to': color.RGB(0, 0, 0),
}
for light in lights
]
@@ -117,11 +64,10 @@ def demo(lights, rig):
if abs(transitioning_to - current_value) < change:
change = random_change()
light_s['change'] = change
- transitioning_to = pick_HSV()
+ transitioning_to = pick_HSV().to_RGB()
light_s['transitioning to'] = transitioning_to
new_color = transition(current_value, transitioning_to, change)
- rgb = HSV_to_RGB(new_color)
- light.setRGBRaw(*rgb)
+ light.setRGBRaw(*new_color.components)
light_s['current value'] = new_color
rig.updateOnce()
time.sleep(1 / FRAME_RATE)
diff --git a/color.py b/color.py
new file mode 100644
index 0000000..c8cb3cb
--- /dev/null
+++ b/color.py
@@ -0,0 +1,84 @@
+"""Color vector library containing HSV and RGB."""
+from __future__ import division
+import colorsys
+import math
+
+class Vector(object):
+ """N-dimensional vector class."""
+ def __init__(self, *components):
+ """Create a vector from its components."""
+ self.components = components
+
+ def __add__(self, other):
+ """Add two vectors."""
+ return Vector(
+ *map(lambda x, y: x + y, zip(self.components, other.components))
+ )
+
+ def __sub__(self, other):
+ """Subtract two vectors."""
+ return Vector(
+ *map(lambda x, y: x - y, zip(self.components, other.components))
+ )
+
+ def __mul__(self, other):
+ """Multiply a vector by a constant."""
+ return Vector(*map(lambda x: x * other, self.components))
+
+ def __rmul__(self, other):
+ """Multiply a constant by a vector."""
+ return self * other
+
+ def __abs__(self):
+ """Return the 2-norm of the vector."""
+ return math.sqrt(sum(map(lambda x: x * x, self.components)))
+
+ def __div__(self, scalar):
+ """Return self * 1 / scalar."""
+ return self * (1 / scalar)
+
+
+def HSV(Vector):
+ """HSV 3-vector."""
+ def __init__(self, hue, saturation, value):
+ """Initialize with hue, saturation, and value."""
+ super().__init__(hue, saturation, value)
+
+ def to_RGB(self):
+ """Convert to an RGB vector."""
+ return RGB(colorsys.hsv_to_rgb(*self.components))
+
+ def h(self):
+ """Return hue."""
+ return self.components[0]
+
+ def s(self):
+ """Return saturation."""
+ return self.components[1]
+
+ def v(self):
+ """Return value."""
+ return self.components[2]
+
+
+def RGB(Vector):
+ """RGB 3-vector."""
+ def __init__(self, r, g, b):
+ """Initialize with red green and blue."""
+ super().__init__(r, g, b)
+
+ def to_HSV(self):
+ """Convert to HSV vector."""
+ return HSV(colorsys.rgb_to_hsv(*self.components))
+
+ def r(self):
+ """Return red value."""
+ return self.components[0]
+
+ def g(self):
+ """Return green value."""
+ return self.components[1]
+
+ def b(self):
+ """Return blue value."""
+ return self.components[2]