#!/bin/bash
#mx-color-picker
#yad color picker with editable palettes by Melber
#License: GPL-3.0+
#Version 26.03.02

#export TEXTDOMAINDIR=/usr/share/locale
#export TEXTDOMAIN="xxx"
#source gettext.sh

#define paths
mxcp_path=$HOME/.config/MX-Linux/mx-color-picker
config_path=$HOME/.config/MX-Linux/mx-color-picker/mxcp.conf
colorlist_path=$HOME/.config/MX-Linux/mx-color-picker/palettes
colorlist_default=$HOME/.config/MX-Linux/mx-color-picker/palettes/default.txt

#check for wayland
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
#TRANSLATORS warning message that tool doesn't work with wayland
    wayland_text_1="$(gettext 'Sorry, this tool is not available when running a wayland session.')"

    yad --title="$title" --class="$class" --window-icon="$iconpath" \
    --borders=10 --center --width=300 --height=100 \
    --text="\n$wayland_text_1\n" --text-align=center \
    --button=yad-close \

    exit
fi

#first run setup

if [ ! -d "$colorlist_path" ]; then
    mkdir -p $colorlist_path
fi

if [ ! -f "$config_path" ]; then
    printf "init_color=#0f56d9
init_palette=default
output_mode=hex" > $config_path
fi

if [ ! -f "$colorlist_default" ]; then
    printf "15 86 217 mx-blue\t(15,86,217)\n" > $colorlist_default
fi

########
# palette manager

palette_manager() {

# make palette list
manage_palettes() {

colorlist_all=($(ls $colorlist_path/*.txt | sed -e "s|$colorlist_path/||g" | sed -e "s/.txt//g" | sed -e "s/ //g"))

source $config_path

if [ -z "$init_palette" ] || [ ! -f $colorlist_path/$init_palette.txt ]; then
    init_palette="default"
fi

if [ "$init_palette" = "default" ]; then
    colorlist_yad='true default'
else
    colorlist_yad='false default'
fi

for item in ${colorlist_all[@]}; do
    if [ "$item" != "default" ];then
        if [ "$init_palette" = "$item" ]; then
            colorlist_yad+=" true $item"
        else
            colorlist_yad+=" false $item"
        fi
    fi
done


#yad setup
\
#TRANSLATORS Window Title
title_palette="$(gettext 'Palette Management')"
class_palette="mx-color-picker-palettes"
iconpath="/usr/share/icons/hicolor/scalable/apps/mx-color-picker.svg"

#TRANSLATORS Window text
text_palettes_main1="$(gettext 'Select a color palette and manage existing palettes')"
#TRANSLATORS Tooltip for plus button
hint_palettes1="$(gettext 'Create new palette')"
#TRANSLATORS Tooltip for minus button
hint_palettes2="$(gettext 'Delete selected palette')"
#TRANSLATORS Tooltip for edit button
hint_palettes3="$(gettext 'Rename selected palette')"
#TRANSLATORS Button text
BTN_APPLY="$(gettext 'Apply')" ; BTN_APPLY+='!gtk-ok'

# manager yad
palette_selection=$(yad --title="$title_palette" --class="$class_palette" --window-icon="$iconpath" \
--geometry=150x300+10+10 --borders=10 --separator="" \
--text="<b>$text_palettes_main1\n</b>" --text-align=center \
--list --radiolist --no-headers --column="radios" --column="palettes" --print-column=2 \
--buttons-layout=spread \
--button="!edit-rename!$hint_palettes3":28 \
--button="!list-remove!$hint_palettes2":26 \
--button="!list-add!$hint_palettes1":24 \
--button="$BTN_APPLY":22 \
${colorlist_yad[@]} )

case $? in
    252 )
        palman_done="jaja"
    ;;
    
    22 )
        old_palette=$(grep 'init_palette' $config_path | sed -e 's/init_palette=//' )
        sed -i "s/$old_palette/$palette_selection/" $config_path
        
        palman_done="jaja"
    ;;
    
    24 )
        #TRANSLATORS Window Title
        title_add="$(gettext 'Create new palette')"
        #TRANSLATORS Window text
        text_add="$(gettext 'New palette name')"
        
        addpalette_name=$(yad --title="$title_add" --class="$class_palette" --window-icon="$iconpath" \
        --geometry=300x100+10+10 --buttons-layout=spread --form --separator="" \
        --field="$text_add" "" \
        --button="yad-ok" )

        if [ ! -f $colorlist_path/$addpalette_name.txt ]; then
            touch $colorlist_path/$addpalette_name.txt
        fi
    ;;    
    
    26 )

        
        if [ $palette_selection = "default" ]; then
           #TRANSLATORS Window Title
           title_warn_del="$(gettext 'Warning')"
           #TRANSLATORS Window text
           text_warn_del="$(gettext 'Default palette cannot be deleted!')"
            yad --title="$title_warn_del" --class="$class_palette" --window-icon="$iconpath" --separator="" \
            --geometry=300x100+10+10 --buttons-layout=spread \
            --text="$text_warn_del" "" --button="yad-ok"
        else
            rm $colorlist_path/$palette_selection.txt
        fi
    ;;     

    28 )
           #TRANSLATORS Window Title
           title_rename="$(gettext 'Rename palette ')"
           #TRANSLATORS Window text
           text_rename="$(gettext 'New palette name')"
                
        palette_newname=$(yad --title="$title_rename" --class="$class_palette" --window-icon="$iconpath" \
        --geometry=300x100+10+10 --buttons-layout=spread --separator="" --form \
        --field="$text_rename" "" \
        --button="yad-ok" )

        if [ -f $colorlist_path/$palette_newname.txt ]; then
           #TRANSLATORS Window Title
           title_warn_rename="$(gettext 'Warning')"
           #TRANSLATORS Window text
           text_warn_rename="$(gettext 'Renaming failed. A palette with this name already exists!')"
            yad --title="$title_warn_rename" --class="$class_palette" --window-icon="$iconpath" --separator="" \
            --geometry=300x100+10+10 --buttons-layout=spread \
            --text="$text_warn_rename" "" --button="yad-ok"
        else
            mv $colorlist_path/$palette_selection.txt $colorlist_path/$palette_newname.txt
        fi
    ;;   
    
esac
}

export -f manage_palettes

while [ "$palman_done" != "jaja" ]; do
    manage_palettes
done

unset palman_done

}

export -f palette_manager

########

#rgb to hex function
rgb_to_hex() {
    printf "#%02X%02X%02X" ${*//','/' '}
}

########

#main function

color_picker_plus() {

# set up yad
source $config_path

if [ -z "$init_color" ]; then
    init_color='#0f56d9'
fi

colorlist_file=$colorlist_path/$init_palette.txt

if [ $output_mode == hex ]; then
    text_output='#RRGGBB'
elif [ $output_mode == rgb ]; then
    text_output='R,G,B'
fi

#TRANSLATORS Window Title
title="$(gettext 'MX Color Picker')"
class="mx-color-picker"
iconpath="/usr/share/icons/hicolor/scalable/apps/mx-color-picker.svg"

#TRANSLATORS Tooltip for plus button
hint_add="$(gettext 'Add color to palette')"
#TRANSLATORS Tooltip for minus button
hint_remove="$(gettext 'Remove color from palette')"
#TRANSLATORS Tooltip for hamburger button
hint_manage="$(gettext 'Manage palettes')"
#TRANSLATORS Tooltip for copy button
hint_copy="$(gettext 'Copy color to clipboard')"
#TRANSLATORS Tooltip for </> button
hint_hexrgb="$(gettext 'Toggle output to clipboard between #RRGGBB and R,G,B formats')"

BTN_ADDLIST="!list-add!$hint_add"
BTN_REMOVELIST="!list-remove!$hint_remove"
BTN_MANAGELIST="!format-justify-fill!$hint_manage"
BTN_COPY="!edit-copy!$hint_copy"
BTN_HEXRGB="!dialog-xml-editor!$hint_hexrgb"

#TRANSLATORS Window text 1
text_main1="$(gettext 'Color palette:')"
#TRANSLATORS Window text 1
text_main2="$(gettext 'Output format:')"

# main yad

color_selection=$(yad --title="$title" --class="$class" --window-icon="$iconpath" \
--geometry=300x680+10+10 --separator="" --buttons-layout=spread \
--text="<b>$text_main1</b> $init_palette\t\t<b>$text_main2</b> $text_output" --text-align=center \
--color --picker --init-color=$init_color \
--palette=$colorlist_file --expand-palette --mode=rgb \
--button="${BTN_MANAGELIST}":10 \
--button="${BTN_REMOVELIST}":8 \
--button="${BTN_ADDLIST}":6 \
--button="${BTN_HEXRGB}":4 \
--button="${BTN_COPY}":2 \
)

getout=$?

color_selection=$(echo $color_selection | sed -e 's/rgb(//' | sed -e 's/)//' )

case $getout in
# exit
    252 )
        alldone="alldone"
    ;;
# copy to clipboard    
    2 )    
        color_selection_hex="$(rgb_to_hex $color_selection)"

        if [ $output_mode == hex ]; then
            echo $color_selection_hex | xclip -selection clipboard
        else 
            echo $color_selection | xclip -selection clipboard
        fi         
               
        sed -i "s/init_color=$init_color/init_color=$color_selection_hex/" $config_path

        alldone="nope"    
    ;;
# output mode   
    4 )    
        if [ $output_mode == hex ]; then
            sed -i 's/output_mode=hex/output_mode=rgb/' $config_path
        elif [ $output_mode == rgb ]; then
            sed -i 's/output_mode=rgb/output_mode=hex/' $config_path
        fi
        
        alldone="nope"    
    ;;
# add to palette        
    6 )
        colorcheck=$(grep "$color_selection" $colorlist_file)
        
        if [ -z "$colorcheck" ]; then

            rgb_numbers=$(echo $color_selection | sed -e 's/rgb=(//' | sed -e 's/)//' | sed -e 's/,/ /g' )

            #TRANSLATORS Window Title
            title_addcolor="$(gettext 'Add color to palette')"
            #TRANSLATORS Window text
            addcolor_text="$(gettext 'Color Name:')"
            
            addcolor_name=$(yad --title="$title_addcolor" --class="$class" --window-icon="$iconpath" \
            --geometry=300x100+10+10 --buttons-layout=spread --separator="" \
            --form \
            --field="$addcolor_text" "" \
            --button="yad-ok"
            )

            addcolor_string="$rgb_numbers $addcolor_name\t($color_selection)"
        
            printf "$addcolor_string\n" >> $colorlist_file
 
        else

            #TRANSLATORS Window Title
            title_addcolor_warn="$(gettext 'Warning')"
            #TRANSLATORS Window text
            text_addcolor_warn1="$(gettext 'Palette already contains this color!')"
            #TRANSLATORS Window text
            text_addcolor_warn2="$(gettext 'Overwrite the existing color?')"  
             
            yad --title="$title_addcolor_warn" --class="$class" --window-icon="$iconpath" \
            --geometry=300x100+10+10 --buttons-layout=spread --separator="" \
            --text="\n$text_addcolor_warn1\n$text_addcolor_warn2" --text-align=center \
            --button="yad-no:30" --button="yad-yes:32"

            case $? in
            30 )
                overwrite_color=nej
            ;;
            
            32 ) 
                rgb_numbers=$(echo $color_selection | sed -e 's/rgb=(//' | sed -e 's/)//' | sed -e 's/,/ /g' )

                #TRANSLATORS Window Title
                title_addcolor="$(gettext 'Add color to palette')"
                #TRANSLATORS Window text
                addcolor_text="$(gettext 'Color Name:')"                
                
                addcolor_name=$(yad --title="$title_addcolor" --class="$class" --window-icon="$iconpath" \
                --geometry=300x100+10+10 --buttons-layout=spread --separator="" \
                --form \
                --field="$addcolor_text" "" \
                --button="yad-ok"
                )

                addcolor_string="$rgb_numbers $addcolor_name\t($color_selection)"
        
                sed -i "s/$colorcheck/$addcolor_string/" $colorlist_file
                sed -i '/^$/d' $colorlist_file
            ;;
            
            esac
        
        fi     
                
        color_selection_hex="$(rgb_to_hex $color_selection)"
        sed -i "s/init_color=$init_color/init_color=$color_selection_hex/" $config_path

        alldone="nee"
    ;;
# remove from palette
    8 )
        findline=$(grep "$color_selection" $colorlist_file)
        sed -i "s/$findline//" $colorlist_file
        sed -i '/^$/d' $colorlist_file
        
        color_selection_hex="$(rgb_to_hex $color_selection)"
        sed -i "s/init_color=$init_color/init_color=$color_selection_hex/" $config_path
        
        alldone="nah"
    ;;
# palette manager
    10 )
        palette_manager
        alldone="nein"    
    ;;
    
esac

}

export -f rgb_to_hex color_picker_plus


#loop main function

while [ "$alldone" != "alldone" ]; do

    color_picker_plus

done

exit 0
