comparison keyboard_colors/color_profile.py @ 3:eb2aa09653bd default tip

beginnings of CPU colors, daemon
author Brad Greco <brad@bgreco.net>
date Mon, 29 Mar 2021 20:27:09 -0400
parents 091a1f59a79c
children
comparison
equal deleted inserted replaced
2:091a1f59a79c 3:eb2aa09653bd
1 import colorsys
1 import gi 2 import gi
2 import importlib 3 import importlib
3 import json 4 import json
4 import os 5 import os
5 import pathlib 6 import pathlib
7 import re
6 import sys 8 import sys
7 import types 9 import types
8 import uuid 10 import uuid
9 from types import SimpleNamespace 11 from types import SimpleNamespace
10 gi.require_version("Gtk", "3.0") 12 gi.require_version("Gtk", "3.0")
17 type_name = '(no name)' 19 type_name = '(no name)'
18 20
19 def __init__(self): 21 def __init__(self):
20 self.name = '' 22 self.name = ''
21 self.id = str(uuid.uuid4()) 23 self.id = str(uuid.uuid4())
24 self.transition_time = 1000
25 self.frequency_time = 1000
26
27 def color_scale_value(self, value):
28 if value in self.color_scale:
29 return self.color_scale[value]
30
31 thresholds = self.color_scale.keys()
32 thresholds_below = (i for i in thresholds if i < value)
33 thresholds_above = (i for i in thresholds if i > value)
34 threshold_below = max(thresholds_below, default=min(thresholds))
35 threshold_above = min(thresholds_above, default=max(thresholds))
36
37 percent = value / (threshold_above - threshold_below)
38 return Color.get_step_color(
39 self.color_scale[threshold_below],
40 self.color_scale[threshold_above],
41 percent
42 )
22 43
23 def build_settings_ui(self): 44 def build_settings_ui(self):
24 glade_file = sys.modules[self.__module__].__file__.replace('.py', '.glade') 45 glade_file = sys.modules[self.__module__].__file__.replace('.py', '.glade')
25 builder = Gtk.Builder() 46 builder = Gtk.Builder()
26 builder.add_from_file(glade_file) 47 builder.add_from_file(glade_file)
82 def get_profiles(self): 103 def get_profiles(self):
83 return self.profiles 104 return self.profiles
84 105
85 def get_profile(self, profile_id): 106 def get_profile(self, profile_id):
86 return self.get_profiles()[profile_id] 107 return self.get_profiles()[profile_id]
108
109
110 class Color:
111
112 def __init__(self, r, g, b):
113 self.r = r
114 self.g = g
115 self.b = b
116
117 @staticmethod
118 def from_hsv(h, s, v):
119 return Color(*colorsys.hsv_to_rgb(h, s, v))
120
121 def to_hsv(self):
122 return colorsys.rgb_to_hsv(self.r, self.g, self.b)
123
124 @staticmethod
125 def from_hex(hex):
126 hex = hex.strip().strip('#').lower()
127 if not re.match('[0-9a-f]{6}', hex):
128 raise ValueError('Invalid hex color string: ' + hex)
129
130 r = int(hex[0:2], 16) / 255
131 g = int(hex[2:4], 16) / 255
132 b = int(hex[4:6], 16) / 255
133
134 return Color(r, g, b)
135
136 def to_hex(self):
137 return format(int(self.r * 255), '02x') \
138 + format(int(self.g * 255), '02x') \
139 + format(int(self.b * 255), '02x')
140
141 @staticmethod
142 def get_steps(start_color, end_color, step_count):
143 # (start_h, start_s, start_v) = start_color.to_hsv()
144 # (end_h, end_s, end_v) = end_color.to_hsv()
145
146 # # Find the shortest distance between the two hues.
147 # if abs(start_h - end_h) < 0.5:
148 # h_step = (end_h - start_h) / step_count
149 # else:
150 # h_step = (1 - abs(end_h - start_h)) / step_count
151 # if (start_h < end_h):
152 # h_step = h_step * -1
153
154 # steps = []
155 # for i in range(0, step_count):
156 # h = (start_h + h_step * i) % 1
157 # s = start_s + (end_s - start_s) / step_count * i
158 # v = start_v + (end_v - start_v) / step_count * i
159 # steps.append(Color.from_hsv(h, s, v))
160
161 steps = []
162 for i in range(0, step_count):
163 percent = i / step_count
164 steps.append(Color.get_step_color(start_color, end_color, percent))
165
166 return steps
167
168 @staticmethod
169 def get_step_color(start_color, end_color, percent):
170 (start_h, start_s, start_v) = start_color.to_hsv()
171 (end_h, end_s, end_v) = end_color.to_hsv()
172
173 # Find the shortest distance between the two hues.
174 if abs(start_h - end_h) < 0.5:
175 h_diff = end_h - start_h
176 else:
177 h_diff = 1 - abs(end_h - start_h)
178 if (start_h < end_h):
179 h_diff = h_diff * -1
180
181 h = (start_h + h_diff * percent) % 1
182 s = start_s + (end_s - start_s) * percent
183 v = start_v + (end_v - start_v) * percent
184
185 return Color.from_hsv(h, s, v)
186
187 def __repr__(self):
188 return 'Color #' + self.to_hex()