Skip to content

Instantly share code, notes, and snippets.

@CoolGoose
Created May 3, 2023 19:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save CoolGoose/a0a868738b01f1d564c900c81326698a to your computer and use it in GitHub Desktop.
Simple chroma based css preferred color scheme generator
#!/usr/bin/env bash
set -euo pipefail
# Default values
light_theme=""
dark_theme=""
destination_path="assets/css/extended/syntax.css"
# Help message
usage() {
cat <<EOF
Usage: $0 [options] [--] [light-theme] [dark-theme]
Options:
-h, --help Show this help message and exit
--light-theme=LIGHT_THEME Set the light theme
--dark-theme=DARK_THEME Set the dark theme
--destination-path=PATH Set the destination path for the output file
Positional arguments:
light-theme Light theme (alternative to --light-theme)
dark-theme Dark theme (alternative to --dark-theme)
EOF
}
positional_arg_counter=0
# Parse options and arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
exit 0
;;
--light-theme=*)
light_theme="\${1#*=}"
positional_arg_counter=$((positional_arg_counter + 1))
shift
;;
--dark-theme=*)
dark_theme="\${1#*=}"
positional_arg_counter=$((positional_arg_counter + 1))
shift
;;
--destination-path=*)
destination_path="\${1#*=}"
shift
;;
*)
if [[ $positional_arg_counter -eq 0 ]]; then
light_theme="$1"
elif [[ $positional_arg_counter -eq 1 ]]; then
dark_theme="$1"
else
echo "Error: Too many arguments" >&2
usage
exit 1
fi
positional_arg_counter=$((positional_arg_counter + 1))
shift
;;
esac
done
if [[ -z "$light_theme" ]] || [[ -z "$dark_theme" ]]; then
echo "Error: Missing arguments" >&2
usage
exit 1
fi
hugo gen chromastyles --style="$light_theme" > "$destination_path"
{
echo "@media (prefers-color-scheme: dark) {"
hugo gen chromastyles --style="$dark_theme" | sed -E 's/(^\/\*[^*]*\*\/)?(.+)/\1 .dark\2/'
echo "}"
} >> "$destination_path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment