-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcolor.py
51 lines (42 loc) · 1.19 KB
/
color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import struct
from pytwig.src.lib import util
class Color():
"""Data storage object that contains parameters for Bitwig colors.
"""
def __init__(self, rd, gr, bl, al = 1.0):
"""Initialization for Color object
Reads color and alpha values and stores them in an array. If alpha value is 1.0, it is ignored and data array is length 3.
Args:
rd (float): Red
gr (float): Green
bl (float): Blue
al (float): Alpha
"""
self.type = 'color'
self.data = [rd, gr, bl, al]
if (al == 1.0):
self.data = self.data[:-1]
return
def __iter__(self):
yield 'type', self.type
yield 'data', self.data
def __repr__(self):
return self.__str__()
def __str__(self):
return "Color: " + str(self.data)
def show(self):
print(str(self.__dict__()).replace(', ', ',\n').replace('{', '{\n').replace('}', '\n}'))
def encode(self):
"""Encodes the color object into Bitwig bytecode.
Returns:
bytes: Bitwig bytecode representation of the Color object.
"""
output = b''
count = 0
for item in self.data:
flVal = struct.unpack('>I', struct.pack('>f', item))[0]
output += util.hex_pad(flVal,8)
count += 1
if count == 3:
output += struct.pack('>f', 1.0)
return output