65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
# LEDD Project
|
|
# Copyright (C) 2015 LEDD Team
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from colour import Color
|
|
|
|
|
|
class Stripe:
|
|
"""
|
|
A stripe is the smallest controllable unit.
|
|
"""
|
|
|
|
def __init__(self, controller, name, rgb, channels, sid=-1, from_db=False):
|
|
self.controller = controller
|
|
self.name = name
|
|
self.rgb = bool(rgb)
|
|
self.channels = channels
|
|
self.id = sid
|
|
self._color = Color()
|
|
self.gamma_correct = (2.8, 2.8, 2.8) # TODO: add to DB
|
|
self.read_color()
|
|
if not from_db:
|
|
self.save_to_db()
|
|
|
|
def save_to_db(self):
|
|
cur = self.controller.db.cursor()
|
|
if self.id == -1:
|
|
cur.execute("INSERT INTO stripes DEFAULT VALUES")
|
|
self.id = cur.lastrowid
|
|
cur.execute(
|
|
"UPDATE stripes SET channel_r = ?, channel_g = ?, channel_b = ?,controller_id = ?, name = ? WHERE id = ?",
|
|
self.channels + [self.controller.id, self.name, self.id])
|
|
cur.close()
|
|
self.controller.db.commit()
|
|
|
|
def read_color(self):
|
|
self._color.rgb = [self.controller.get_channel(channel) ** (1 / 2.8) for channel in self.channels]
|
|
|
|
@classmethod
|
|
def from_db(cls, controller, row):
|
|
return cls(controller, name=row["name"], rgb=row["rgb"],
|
|
channels=(row["channel_r"], row["channel_g"], row["channel_b"]), sid=row["id"], from_db=True)
|
|
|
|
def set_color(self, c):
|
|
self._color = c
|
|
for channel, gamma_correct, value in zip(self.channels, self.gamma_correct, c.rgb):
|
|
self.controller.set_channel(channel, value ** gamma_correct)
|
|
|
|
def get_color(self):
|
|
return self._color
|
|
|
|
color = property(get_color, set_color)
|