Skip to content

Commit

Permalink
first release
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpstpdwn committed May 30, 2024
1 parent 4c17f24 commit df6cc65
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 69 deletions.
19 changes: 0 additions & 19 deletions .config/shwal/script

This file was deleted.

25 changes: 9 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ Set wallpaper and generate colorscheme from an image.
Options:
-i <image> Set wallpaper and generate colorscheme from <image>.
-j <json> load colorscheme from <json>.
-s <script> Run an external script.
-r Restore wallpaper.

Examples:
shwal -i ~/Pictures/wallpaper.jpg
shwal -j ~/Files/gruvbox.json
shwal -s ~/.config/shwal/script
shwal -r

Note:
-i will set the wallpaper and also change the colorschome while.
-j will only make changes to the colorscheme.
```
- `-i <image>` : This option will set <image> as wallpaper, generate and apply colorcheme, process templates with the new colorcheme, run the `script` file.
- `j <json>` : This flag can be used to load a colorscheme from a json file.
- `-r` : This flag will reset the wallpaper to the last used one. this flag should be used to put wallpaper
on the screen after a reboot. put it in your startscript.

- `-i <image>` : This option will set <image> as wallpaper, generate and apply colorcheme, process templates with the new colorcheme, run the `script` file.
- `-j <json>` : This flag can be used to load a colorscheme from a json file.
- `-s <script>` : Execute an external script.
- `-r` : This flag will reset the wallpaper to the last used one. this flag should be used to put wallpaper
on the screen after a reboot. put it in your startscript.

## Installation

Expand All @@ -50,6 +52,7 @@ The `setup.sh` also installs some configs to your `$HOME/.config/shwal`.
- `ImageMagick` (magick) for color extraction.
- `feh` for setting wallpaper.
- `jq` for setting colorsheme from json files.
- `bc` for arithmetic, if you already dont have it.

## Configuration

Expand All @@ -76,17 +79,7 @@ The `setup.sh` also installs some configs to your `$HOME/.config/shwal`.
}
```

- ## `$HOME/.config/shwal/script`

A shell script that will automaticaly be run after colorscheme generation. This script can be used to
to execute anything you would like after the colorschemes are generated for example i use it to enforce
the theme generated to xmonad, mpv, dunst etc .. by using `sed` to change color variable values in their configs and
reload those software with new colorscheme.
This is a good workaround for those software for whom you dont know how color templates are to be made / imported to the
respective configs etc ...

- ## `$HOME/.config/shwal/colorschemes`

This folder have an extensive collection of dark and light themes saved as json files which can be used with shwal. This folder was taken from the `pywal` project.

- Customize the script to match your setup.
Expand All @@ -97,5 +90,5 @@ Contributions are welcome! Feel free to fork the repository and submit pull requ

## License

This project is licensed under the GPLv3 License.
This project is licensed under the GPLv3 [License](LICENSE).

100 changes: 66 additions & 34 deletions shwal/shwal
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@

OUTPUT_DIR="$HOME/.cache/shwal"
TEMPLATE_DIR="$HOME/.config/shwal/templates"
SCRIPT="$HOME/.config/shwal/script"
mkdir -p "$OUTPUT_DIR"

# Create colorscheme

# Function to calculate the luminance of a color
luminance() {
local hex=$1
local r=$(printf "%d" "0x${hex:1:2}")
local g=$(printf "%d" "0x${hex:3:2}")
local b=$(printf "%d" "0x${hex:5:2}")
echo "scale=4; 0.2126 * $r + 0.7152 * $g + 0.0722 * $b" | bc
}

# Sort colors based on luminance
sort_colors(){
declare -A luminance_values

for color in "${colors[@]}"; do
luminance_values[$color]=$(luminance "$color")
done

sorted_colors=($(for color in "${!luminance_values[@]}"; do
echo "$color ${luminance_values[$color]}"
done | sort -n -k 2 | awk '{print $1}'))
colors=("${sorted_colors[@]}")
}

# Get colors from an image
colors(){
max_attempts=10
attempt=1
Expand All @@ -23,16 +46,16 @@ colors(){

echo

# Ensure we have at least 16 colors

if [ "${#colors[@]}" -lt 16 ]; then
echo "[E] Not enough colors extracted"
exit 1
else
sort_colors
echo "[S] Colorsheme created and saved."
fi
}

# Get and set colorscheme from json file.
colors_from_json(){
if ! command -v jq &> /dev/null; then
echo "[E] 'jq' command not found. Please install 'jq' to use this feature."
Expand Down Expand Up @@ -140,19 +163,7 @@ templates(){
fi
}

# Exec a script after setting colorschemes and templates

scripts(){
if [ -e "$SCRIPT" ]; then
echo -ne "[ ] Running Script ..."
sh "$SCRIPT" > /dev/null 2>&1
echo -ne "\r[S] Running Script ..."
else
echo "[ ] No scripts found"
fi
echo
}

# Help string
help_text(){
echo
echo "Usage: shwal [OPTIONS] [FILE]"
Expand All @@ -161,11 +172,13 @@ help_text(){
echo "Options:"
echo " -i <image> Set wallpaper and generate colorscheme from <image>."
echo " -j <json> load colorscheme from <json>."
echo " -s <script> Run an external script."
echo " -r Restore wallpaper."
echo
echo "Examples:"
echo " shwal -i ~/Pictures/wallpaper.jpg"
echo " shwal -j ~/Files/gruvbox.json"
echo " shwal -s ~/.config/shwal/script"
echo " shwal -r"
echo
echo "Note:"
Expand All @@ -176,23 +189,42 @@ help_text(){

# MAIN

if [ "$1" = "-i" ] && file --mime-type "$2" | grep -q "image"; then
colors $2
set_wallpaper $2
templates
scripts
elif [ "$1" = "-j" ] && file --mime-type "$2" | grep -q "json"; then
colors_from_json $2
templates
scripts
elif [ "$1" = "-r" ]; then
wallpaper=$(head "$OUTPUT_DIR/wallpaper" -n 1 | sed 's/[[:space:]]S$//') > /dev/null 2>&1
if [ ${#wallpaper} -ne 0 ]; then
set_wallpaper $wallpaper
fi
else
help_text
fi
case $1 in
"-i")
if file --mime-type "$2" | grep -q "image"; then
colors $2
set_wallpaper $2
templates
else
echo "Try: shwal -h"
fi
;;
"-j")
if file --mime-type "$2" | grep -q "json"; then
colors_from_json $2
templates
else
echo "Try: shwal -h"
fi
;;
"-s")
if [ -n "$2" ] && [ -x "$2" ]; then
./"$2" > /dev/null 2>&1
else
echo "Try: shwal -h"
fi
;;
"-r")
wallpaper=$(head "$OUTPUT_DIR/wallpaper" -n 1 | sed 's/[[:space:]]S$//') > /dev/null 2>&1
if [ -e "$wallpaper" ]; then
set_wallpaper $wallpaper
else
echo "Try: shwal -h"
fi
;;
*)
help_text
;;
esac

# End

0 comments on commit df6cc65

Please sign in to comment.