Skip to content

Instantly share code, notes, and snippets.

@mknw
Last active January 6, 2021 12:07
Show Gist options
  • Save mknw/6a45d901ea17cf962c11dea4d00100b3 to your computer and use it in GitHub Desktop.
Save mknw/6a45d901ea17cf962c11dea4d00100b3 to your computer and use it in GitHub Desktop.
Set colorscheme for razer blackwidow 2019 keyboard.
#!/usr/bin/zsh
if [ $1 = "-h" ]; then
echo -e "Usage:\n"
echo -e "\tclazer [mode=mode] [selector=hexadecimal_color]\n"
echo -e "Options:"
echo -e "\tmode:\t\tdefault | game"
echo -e "\tselector:\tfKeys | letters | modK | posK | numK | other\n"
echo -e "Example:"
echo -e "\tclazer mode=default letters=FFC0CB numK=00ff00\n"
echo -e "\tThis makes letters pink and numbers green."
echo -e "\tThe other keys will keep the predefined colors for the selected mode.\n"
echo -e "\t(Or, for a sweet, winter colorscheme,"
echo -e "\ttry: \tclazer modK=ffffff letters=10E0C0 fKeys=00ff80 other=FF0052 posK=ed00ff)"
exit
fi
# default colors
function defaultColors {
if [ $1 = "default" ]; then
fKeys="\xff\xa2\x00"
letters="\x00\xff\x80"
modK="\xff\x22\x00"
posK="\xff\xa2\x00"
numK="\x00\x80\xff"
none="\x0\x0\x0"
other="\xff\xa2\x00"
raw_fKeys="ffa200" # - warm yello - Meta, ESC, Fn keys
raw_letters="00ff80" # - seafoam green
raw_modK="ff2200" # - orangish red - Mod keys such as ctrl, used for hjkl in my case
raw_posK="ffa200" # - same as fKeys - Position keys - home, pos1, page up / down etc.
raw_numK="0080ff" # - sky blue
raw_none="000" # empty, absent on razer blackwidow 2019
raw_other="ffa200" # any other keys - currently not used on my keyboard
elif [ $1 = "game" ]; then
fKeys="\xff\xa2\x00"
letters="\x00\xff\x80"
modK="\xff\x22\x00"
posK="\xff\xa2\x00"
numK="\x00\x80\xff"
none="\x0\x0\x0"
other="\xff\xa2\x00"
raw_fKeys="ffa200" # - warm yello - ESC, Fn keys, WASD
raw_letters="00ff80" # - seafoam green
raw_modK="ff2200" # - orangish red - Mod keys
raw_posK="ffa200" # - same as fKeys - Position keys
raw_numK="0080ff" # - sky blue
raw_none="000" # Meta
raw_other="ffa200" # any other keys - currently not used on my keyboard
else
echo -e "\nUnknown mode.\n"
echo -e "\tPossible options: 'game', 'default' (optional).\n"
echo -e "Example:"
echo -e "\tmode=game\n"
exit
fi
}
function assignColor {
selector=$1
color=$2
raw_color=$3
case $selector in
fKeys)
fKeys="$color"
raw_fKeys="$raw_color"
;;
letters)
letters="$color"
raw_letters="$raw_color"
;;
modK)
modK="$color"
raw_modK="$raw_color"
;;
posK)
posK="$color"
raw_posK="$raw_color"
;;
numK)
numK="$color"
raw_numK="$raw_color"
;;
other)
other="$color"
raw_other="$raw_color"
;;
*)
echo -e "\nUnknown selector.\n"
echo -e "\tselector=color in hexadecimal format for RGB values. \n"
echo -e "\tFor example: fKeys=FF0000"
echo -e "\tThis sets the F1 - F12 keys to red color. \n"
echo -e "\tAvailable selectors: "
echo -e "\tfKeys | letters | modK | posK | numK | other"
exit
;;
esac
}
function parseColor {
# get the color in hex code in format 000000 - for black, FFFFFF - for white
selector=$1
color=$2
# extract red, green and blue hex values
red="${color:0:2}"
green="${color:2:2}"
blue="${color:4:2}"
# write to the color variable in format \x00\x00\x00 - for black
color="\\x$red\\x$green\\x$blue"
rawcolor="$red$green$blue"
# assign color to key
assignColor $selector $color $rawcolor
}
for matrix_custom_frame in /sys/bus/hid/drivers/razerkbd/*:*:*.*/matrix_custom_frame
do
# echo $(dirname "$matrix_custom_frame")
# echo "$matrix_custom_frame"
if [[ $1 == mode* ]]; then
mode=$(echo "$1" | grep -oP "(?<==).*")
shift
else
mode="default"
fi
# set default colors for mode:
defaultColors $mode
while [ "$1" != "" ]; do
# get the selector part of the parameter, for example fKeys
selector=$(echo "$1" | grep -oP "(?=.+=)\w*")
# get the color in hex code in format 000000 - for black, FFFFFF - for white
color=$(echo "$1" | grep -oP "(?<==).*")
parseColor $selector $color
shift
done
# output color values just for info
echo ""
echo "Setting values:"
echo "\tfKeys: $raw_fKeys"
echo "\tletters: $raw_letters"
echo "\tmodK: $raw_modK"
echo "\tposK: $raw_posK"
echo "\tnumK: $raw_numK"
echo "\tother: $raw_other"
# push the color values into keyboard memory
if [ "$mode" = "default" ]; then
echo -n -e "\x0\x0\x15$none$fKeys$none$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$other$other$other$none$none$none$none" >| $matrix_custom_frame
echo -n -e "\x1\x0\x15$none$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$posK$posK$posK$numK$numK$numK$numK" >| $matrix_custom_frame
echo -n -e "\x2\x0\x15$none$modK$letters$letters$letters$letters$letters$letters$letters$letters$letters$letters$letters$letters$modK$posK$posK$posK$numK$numK$numK$numK" >| $matrix_custom_frame
echo -n -e "\x3\x0\x15$none$modK$letters$letters$letters$letters$letters$modK$modK$modK$modK$letters$letters$letters$modK$none$none$none$numK$numK$numK$none" >| $matrix_custom_frame
echo -n -e "\x4\x0\x15$none$modK$letters$letters$letters$letters$letters$letters$letters$letters$letters$letters$letters$none$modK$none$modK$none$numK$numK$numK$numK" >| $matrix_custom_frame
echo -n -e "\x5\x0\x15$none$modK$fKeys$modK$letters$letters$letters$letters$none$none$modK$letters$fKeys$fKeys$modK$modK$modK$modK$none$numK$numK$none" >| $matrix_custom_frame
elif [ "$mode" = "game" ]; then
# Esc + Fn keys
echo -n -e "\x0\x0\x15$none$fKeys$none$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$fKeys$other$other$other$none$none$none$none" >| $matrix_custom_frame
# numbers + positional keys
echo -n -e "\x1\x0\x15$none$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$numK$posK$posK$posK$numK$numK$numK$numK" >| $matrix_custom_frame
# modK + 'W' + letters
echo -n -e "\x2\x0\x15$none$modK$letters$fKeys$letters$letters$letters$letters$letters$letters$letters$letters$letters$letters$modK$posK$posK$posK$numK$numK$numK$numK" >| $matrix_custom_frame
# modK + 'ASD' + letters
echo -n -e "\x3\x0\x15$none$modK$fKeys$fKeys$fKeys$letters$letters$letters$letters$letters$letters$letters$letters$letters$modK$none$none$none$numK$numK$numK$none" >| $matrix_custom_frame
# modK + letters
echo -n -e "\x4\x0\x15$none$modK$letters$letters$letters$letters$letters$letters$letters$letters$letters$letters$letters$none$modK$none$modK$none$numK$numK$numK$numK" >| $matrix_custom_frame
# last row
echo -n -e "\x5\x0\x15$none$modK$none$modK$letters$letters$letters$letters$none$none$modK$letters$fKeys$fKeys$modK$modK$modK$modK$none$numK$numK$none" >| $matrix_custom_frame
else
echo "mode $mode not recognized"
fi
# apply the custom effect, that is custom colors
echo "Writing to file: \n\t$(dirname "$matrix_custom_frame")/matrix_effect_custom"
echo -n "1" > $(dirname "$matrix_custom_frame")/matrix_effect_custom
# # Uncomment all below to enable Fn keys functionality.
# # kill openrazer-daemon as it prevents usage of fn keys
# echo -n "Verifying daemon shutdown... "
# for i in {1..5}
# do
# if [ "$(pidof openrazer-daemon)" != "" ]
# then
# openrazer-daemon -s
# sleep 2
# else
# break
# fi
# done
# echo "done."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment