Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added css3 webcolor support for steady colors & random color option #34

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions msi_perkeyrgb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import sys
import argparse
import webcolors
import colorsys
import random
from .protocol_data.msi_keymaps import AVAILABLE_MSI_KEYMAPS
from .config import load_config, load_steady, ConfigError
from .parsing import parse_model, parse_usb_id, parse_preset, UnknownModelError, UnknownIdError, UnknownPresetError
Expand All @@ -24,7 +27,8 @@ def main():
parser.add_argument('-p', '--preset', action='store', help='Use vendor preset (see --list-presets).')
parser.add_argument('-m', '--model', action='store', help='Set laptop model (see --list-models). If not specified, will use %s as default.' % DEFAULT_MODEL)
parser.add_argument('--list-models', action='store_true', help='List available laptop models.')
parser.add_argument('-s', '--steady', action='store', metavar='HEXCOLOR', help='Set all of the keyboard to a steady html color. ex. 00ff00 for green')
parser.add_argument('-s', '--steady', action='store', metavar='COLOR', help='Set all of keyboard to a steady hex or css3 color e.g. 00ff00 or green.')
parser.add_argument('-r', '--random', action='store_true', help='Set all of keyboard to a random hex color.')

args = parser.parse_args()

Expand Down Expand Up @@ -126,7 +130,36 @@ def main():
# If user has requested to display a steady color
elif args.steady:
try:
bwu62 marked this conversation as resolved.
Show resolved Hide resolved
colors_map, warnings = load_steady(args.steady, msi_keymap)
colors_map, warnings = load_steady(webcolors.name_to_hex(args.steady)[1:], msi_keymap)
except ValueError as e:
try:
colors_map, warnings = load_steady(args.steady, msi_keymap)
except ConfigError as e:
print("Error preparing steady color : %s" % str(e))
sys.exit(1)
kb.set_colors(colors_map)
kb.refresh()

# If user has requested to display a random color
elif args.random:
try:
# Generate random color with 25% <= luminance < 75% and 50% <= saturation < 100%
# these bounds were chosen arbitrarily by inspection so that
# the resulting random color won't be too light or dark or desaturated.
#
# HLS was chosen instead of HVL due to being more human-friendly
# see https://en.wikipedia.org/wiki/HSL_and_HSV#Motivation
col=webcolors.PercentRGB(
*colorsys.hls_to_rgb(
random.random(), # hue
random.random()*.5+.25, # lightness
random.random()*.5+.5 # saturation
)
)
colors_map, warnings = load_steady(
webcolors.rgb_percent_to_hex(["{:.2%}".format(val) for val in col])[1:],
msi_keymap
)
except ConfigError as e:
print("Error preparing steady color : %s" % str(e))
sys.exit(1)
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
],
install_requires=[
'webcolors',
],
)