Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix morphing in 'blinkstick.py'. #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions blinkstick/blinkstick.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ def set_color(self, channel=0, index=0, red=0, green=0, blue=0, name=None, hex=N

red, green, blue = self._determine_rgb(red=red, green=green, blue=blue, name=name, hex=hex)

_set_rgb(self, channel=channel, index=index, red=red, green=green, blue=blue)

def _set_rgb(self, channel=0, index=0, red=0, green=0, blue=0):
r = int(round(red, 3))
g = int(round(green, 3))
b = int(round(blue, 3))

if self.inverse:
r, g, b = 255 - r, 255 - g, 255 - b

if index == 0 and channel == 0:
control_string = bytes(bytearray([0, r, g, b]))
report_id = 0x0001
Expand Down Expand Up @@ -435,6 +435,9 @@ def _determine_rgb(self, red=0, green=0, blue=0, name=None, hex=None):
except ValueError:
red = green = blue = 0

if self.inverse:
red, green, blue = 255 - red, 255 - green, 255 - blue

red, green, blue = _remap_rgb_value([red, green, blue], self.max_rgb_value)

# TODO - do smarts to determine input type from red var in case it is not int
Expand Down Expand Up @@ -780,15 +783,8 @@ def morph(self, channel=0, index=0, red=0, green=0, blue=0, name=None, hex=None,
"""

r_end, g_end, b_end = self._determine_rgb(red=red, green=green, blue=blue, name=name, hex=hex)
# descale the above values
r_end, g_end, b_end = _remap_rgb_value_reverse([r_end, g_end, b_end], self.max_rgb_value)

r_start, g_start, b_start = _remap_rgb_value_reverse(self._get_color_rgb(index), self.max_rgb_value)

if r_start > 255 or g_start > 255 or b_start > 255:
r_start = 0
g_start = 0
b_start = 0
r_start, g_start, b_start = self._get_color_rgb(index)

gradient = []

Expand All @@ -803,15 +799,12 @@ def morph(self, channel=0, index=0, red=0, green=0, blue=0, name=None, hex=None,

ms_delay = float(duration) / float(1000 * steps)

self.set_color(channel=channel, index=index, red=r_start, green=g_start, blue=b_start)

for grad in gradient:
grad_r, grad_g, grad_b = grad

self.set_color(channel=channel, index=index, red=grad_r, green=grad_g, blue=grad_b)
self._set_rgb(channel=channel, index=index, red=grad_r, green=grad_g, blue=grad_b)
time.sleep(ms_delay)

self.set_color(channel=channel, index=index, red=r_end, green=g_end, blue=b_end)
self._set_rgb(channel=channel, index=index, red=r_end, green=g_end, blue=b_end)

def open_device(self, d):
"""Open device.
Expand Down Expand Up @@ -1624,19 +1617,11 @@ def _remap(value, leftMin, leftMax, rightMin, rightMax):
def _remap_color(value, max_value):
return _remap(value, 0, 255, 0, max_value)

def _remap_color_reverse(value, max_value):
return _remap(value, 0, max_value, 0, 255)

def _remap_rgb_value(rgb_val, max_value):
return [_remap_color(rgb_val[0], max_value),
_remap_color(rgb_val[1], max_value),
_remap_color(rgb_val[2], max_value)]

def _remap_rgb_value_reverse(rgb_val, max_value):
return [_remap_color_reverse(rgb_val[0], max_value),
_remap_color_reverse(rgb_val[1], max_value),
_remap_color_reverse(rgb_val[2], max_value)]

def get_blinkstick_package_version():
return __version__