From f9e915f6ebc4b0fa4807b89618fdd6cc39fa1aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20=C3=9Cmit=20=C3=96zden?= Date: Thu, 28 Jul 2022 21:47:23 +0300 Subject: [PATCH] Installation path updated & execution time improved & redundant functions removed --- Makefile | 18 +++++++++------ colpick | 62 ++++++++++++++++++--------------------------------- docs/index.md | 14 ++++++------ 3 files changed, 40 insertions(+), 54 deletions(-) mode change 100644 => 100755 colpick diff --git a/Makefile b/Makefile index 3dea265..b5c79db 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,18 @@ -DEST_DIR ?= /usr/bin +PREFIX ?= /usr/local/ +DEST_DIR ?= $(PREFIX)/bin +TARGET_FILE ?= colpick +SRC_FILE ?= colpick all: - @echo Run \'make install\' to install colpick + @echo Run \'make install\' to install $(SRC_FILE) + @echo Run \'make uninstall\' to uninstall $(SRC_FILE) install: - @cp colpick $(DEST_DIR)/colpick - @chmod 755 $(DEST_DIR)/colpick - @echo colpick has been installed + @cp $(SRC_FILE) $(DEST_DIR)/$(TARGET_FILE) + @chmod 755 $(DEST_DIR)/$(TARGET_FILE) + @echo $(SRC_FILE) has been installed uninstall: - @rm -rf $(DEST_DIR)/colpick - @echo colpick has been uninstalled + @rm -f $(DEST_DIR)/$(TARGET_FILE) + @echo $(SRC_FILE) has been uninstalled diff --git a/colpick b/colpick old mode 100644 new mode 100755 index 198cbd9..d4b5b44 --- a/colpick +++ b/colpick @@ -1,13 +1,13 @@ #!/bin/sh -# _ _ _ +# _ _ _ # ___ ___ | |_ __ (_) ___| | __ # / __/ _ \| | '_ \| |/ __| |/ / -#| (_| (_) | | |_) | | (__| < +#| (_| (_) | | |_) | | (__| < # \___\___/|_| .__/|_|\___|_|\_\ -# |_| -# +# |_| +# #author: Mehmet Ümit Özden -#dependencies: ImageMagick, scrot, xdotool, xclip(optional), notification daemon(optional) +#dependencies: ImageMagick, scrot, xdotool, xclip(optional), notification-daemon(optional) VERSION="1.0" TEMP_DIR="/tmp" @@ -42,59 +42,41 @@ options: EOF } check_dependencies() { - if [ $COPY_TO_CLIPBOARD == true ]; then - ! command -v xclip &>/dev/null && - echo "xclip need to be installed" && exit 1 + if [ $COPY_TO_CLIPBOARD = true ]; then + ! command -v xclip >/dev/null && echo "xclip need to be installed" && exit 1 else - ! command -v convert &>/dev/null && - echo "ImageMagick need to be installed" && exit 1 - ! command -v scrot &>/dev/null && - echo "scrot need to be installed" && exit 1 - ! command -v xdotool &>/dev/null && - echo "xdotool need to be installed" && exit 1 + ! command -v convert >/dev/null && echo "ImageMagick need to be installed" && exit 1 + ! command -v scrot >/dev/null && echo "scrot need to be installed" && exit 1 + ! command -v xdotool >/dev/null && echo "xdotool need to be installed" && exit 1 fi } -# Converts hex code to rgb which includes hashtag -hex_to_rgb() { - rgb=$(printf "(%d,%d,%d)" 0x"${1:1:2}" 0x"${1:3:2}" 0x"${1:5:2}") - echo $rgb -} -# Get color code depends on color type -get_color_code(){ - if [ $COLOR_TYPE == "rgb" ]; then - color_code="$(hex_to_rgb $1)" - else - color_code=$1 - fi - echo $color_code -} main() { # x,y mouse_pos=$(xdotool getmouselocation | awk -F"[:a-z=&\"]*" '{printf "%d,%d",$2,$3}') - # Bitmap for high accuarcy and speed - hex_color=$(scrot -o "$TEMP_DIR/colpick.bmp" && convert "$TEMP_DIR/colpick.bmp" -set colorspace RGB -format "#%[hex:p{$mouse_pos}]" info:-) - color_code="$(get_color_code $hex_color)" + # Bitmap for high accuracy and speed + # We can use only ImageMagick too but scrot is much faster + color_code=$(scrot -a $mouse_pos,1,1 -o "$TEMP_DIR/colpick.bmp" && convert "$TEMP_DIR/colpick.bmp" -set colorspace RGB txt:- | ([ $COLOR_TYPE = "rgb" ] && awk 'NR==2{ print $2 }' || awk 'NR==2{ print $3 }') ) - if [ $COPY_TO_CLIPBOARD == true ]; then - printf $color_code | xclip -selection clipboard + if [ $COPY_TO_CLIPBOARD = true ]; then + printf "$color_code" | xclip -selection clipboard fi - if [ $NOTIFY == true ]; then + if [ $NOTIFY = true ]; then + [ $COLOR_TYPE = "rgb" ] && xc="rgb$color_code" || xc="$color_code" # Solid color icon for notification - convert -bordercolor $BORDER_COLOR -border $BORDER_SIZE -size $ICON_SIZE xc:$hex_color $ICON_PATH + convert -bordercolor $BORDER_COLOR -border $BORDER_SIZE -size $ICON_SIZE xc:"$xc" $ICON_PATH # Notify the notification manager - notify-send -t $EXPIRE_TIME -i $ICON_PATH $color_code "${NOTIFICATION_MESSAGE}" - + notify-send -t $EXPIRE_TIME -i $ICON_PATH "$color_code" "$NOTIFICATION_MESSAGE" fi - if [ $STDOUT == true ]; then - echo $color_code + if [ $STDOUT = true ]; then + echo "$color_code" fi } check_dependencies -while [[ "$1" ]]; do +while [ "$1" ]; do case "$1" in -h|--help) help && exit ;; -v|--version) echo $VERSION && exit ;; diff --git a/docs/index.md b/docs/index.md index 81cc2ed..299526d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -9,8 +9,8 @@ A Linux tool that allows users to pick color on mouse position. * Integrated configuration option for allowing users to adapt their workflow. -* Bitmap screenshot and mouse position used to get pixel color codes. -* Notifier, hex to RGB conversion and clipboard features implemented. +* 1x1 Bitmap screenshot of mouse position used to get pixel color codes. +* Notifier, color codes(RGB and hex) and clipboard features implemented. ![demonstration](https://raw.githubusercontent.com/mehmetumit/colpick/main/demo/demo.gif) ## Dependencies @@ -18,15 +18,15 @@ A Linux tool that allows users to pick color on mouse position. * `scrot` * `xdotool` * `xclip` (optional) -* notification daemon (optional) +* notification-daemon (optional) ## Execution Time ``` $ time colpick -n -real 0m0.326s -user 0m0.253s -sys 0m0.135s +real 0m0.057s +user 0m0.037s +sys 0m0.028s ``` ## Installation @@ -36,7 +36,7 @@ $ git clone https://github.com/mehmetumit/colpick.git # Change directory $ cd colpick # Install with make -$ sudo make install +$ make install ``` ## Usage