Browse Source

Reorganise script as object oriented library

master
Burathar 5 years ago
parent
commit
93b7350b3d
  1. 48
      color.py
  2. 35
      keymap.py

48
color.py

@ -0,0 +1,48 @@
class Color:
def __init__(self, red, green, blue):
if not (Color.check_color_value(red) or Color.check_color_value(green) or Color.check_color_value(blue)):
print(f"Color is out of range")
return
self.red = red
self.green = green
self.blue = blue
@classmethod
def from_hex(cls, hexstring):
"Initialize Color from hexstring (e.g. #ffffff)"
if isinstance(hexstring, str):
if hexstring[0] == '#':
hexstring = hexstring[1:]
if len(hexstring) == 6:
color = tuple(int(color[i:i+2], 16) for i in (0, 2, 4))
return cls(color[0], color[1], color[2])
@classmethod
def from_name(cls, color_name):
"Initialize Color from name (e.g. 'white')"
color_name = str.lower(color_name)
if color_name == 'white':
return cls(255,255,255)
elif color_name == 'black':
return cls(0, 0, 0)
elif color_name == 'red':
return cls(255, 0, 0)
elif color_name == 'green':
return cls(0, 255, 0)
elif color_name == 'blue':
return cls(0, 0, 255)
else:
raise ValueError(f'Colorname {color_name} was not recognised')
@staticmethod
def check_color_value(value):
if not isinstance(value, int):
print(f"Colorvalue '{value}' is not a number")
return False
if value < 0 or value > 255:
print(f"Color value '{value}' is out of bounds (0-255)")
return False
return True

35
keymap.py

@ -0,0 +1,35 @@
from pathlib import Path
import json
import math
class Keymap:
def __init__(self):
self.keys = dict()
pass
def load_from_file(self, path = ''):
if path == '':
path = Path.home() / '.config/openrazer_scripter/keymap.json'
with open(path,'r') as keymap_file:
self.keys = json.load(keymap_file)
def set_keys(self, keys):
self.keys = keys
def get_row_column(self, index):
row = math.floor(index / 22)
column = index % 22
return (row, column)
def get_location(self, key):
if key in self.keys.keys():
value = self.keys[key]
return self.get_row_column(value)
def write_keymap_file(self):
keymap_json = json.dumps(self.keys)
path = Path.home() / '.config/openrazer_scripter/keymap.json'
Path(path.parent).mkdir(parents=True, exist_ok=True)
with open(path,'w') as keymap_file:
keymap_file.write(keymap_json)
Loading…
Cancel
Save