You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.3 KiB
81 lines
2.3 KiB
5 years ago
|
import openrazer.client
|
||
|
import json
|
||
|
from pathlib import Path
|
||
|
import math
|
||
|
|
||
|
def get_keyboard():
|
||
|
devman = openrazer.client.DeviceManager()
|
||
|
for device in devman.devices:
|
||
|
if (device.name == 'Razer BlackWidow Chroma V2'):
|
||
|
return device
|
||
|
return None
|
||
|
|
||
|
def read_keymap_file(path = ''):
|
||
|
if path == '':
|
||
|
path = Path.home() / '.config/openrazer_scripter/keymap.json'
|
||
|
keymap = None
|
||
|
with open(path,'r') as keymap_file:
|
||
|
keymap = json.load(keymap_file)
|
||
|
return keymap
|
||
|
|
||
|
kbd = get_keyboard()
|
||
|
if (kbd == None):
|
||
|
print("No Razer device with devicename 'Razer Blackwidow Chroma V2' found.")
|
||
|
exit()
|
||
|
keymap = read_keymap_file()
|
||
|
|
||
|
def set_static(kbd, color):
|
||
|
color = get_rgb(color)
|
||
|
if color is None:
|
||
|
print(f"Color set_static() was incorrect, skipping.")
|
||
|
return
|
||
|
for row in range(0,6):
|
||
|
for column in range(0, 22):
|
||
|
kbd.fx.advanced.matrix[row,column] = [color[0], color[1], color[2]]
|
||
|
|
||
|
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
|
||
|
|
||
|
def get_rgb(color):
|
||
|
r, g, b = None, None, None
|
||
|
if isinstance(color, str):
|
||
|
if len(color) == 6:
|
||
|
color = tuple(int(color[i:i+2], 16) for i in (0, 2, 4))
|
||
|
if len(color) == 3:
|
||
|
r = color[0]
|
||
|
g = color[1]
|
||
|
b = color[2]
|
||
|
else:
|
||
|
print(f"Could not interpert color '{color}'")
|
||
|
return None
|
||
|
if check_color_value(r) and check_color_value(g) and check_color_value(b):
|
||
|
return r, g, b
|
||
|
else:
|
||
|
print(f"Color '{color}' is out of range")
|
||
|
return None
|
||
|
|
||
|
def get_row_column(index):
|
||
|
row = math.floor(index / 22)
|
||
|
column = index % 22
|
||
|
return row, column
|
||
|
|
||
|
def set_keys(kbd, color, *keys):
|
||
|
color = get_rgb(color)
|
||
|
if color is None:
|
||
|
print(f"Color for key(s) '{keys}' was incorrect, skipping.")
|
||
|
return
|
||
|
for key in keys:
|
||
|
if key in keymap:
|
||
|
position = get_row_column(keymap[key])
|
||
|
kbd.fx.advanced.matrix[position[0], position[1]] = [color[0], color[1], color[2]]
|
||
|
|
||
|
set_static(kbd, (255,255,255))
|
||
|
set_keys(kbd, 'ff0000', 'enter')
|
||
|
kbd.fx.advanced.draw()
|