-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadjustcolor
More file actions
executable file
·135 lines (110 loc) · 3.46 KB
/
Copy pathadjustcolor
File metadata and controls
executable file
·135 lines (110 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/sh
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2021 Jeremy Brubaker <јЬruЬаkе@оrіоnаrtѕ.іо>
#
# abstract: Darken or lighten a color by a percentage
#
# Defaults {{{1
#
VERSION='1.0'
# Documentation {{{1
#
print_help() (
[ -n "$1" ] && printf "%s\n" "$1"
cat <<EOF
Usage: $0 <darken | lighten> COLOR PERCENTAGE
Darken or lighten a color by a percentage
COLOR is a six-digit hexadecimal string, optionally preceded by a '#'.
PERCENTAGE is a value from 0-100 with an optional trailing '%'.
-v display version info and exit
-h display this help and exit
subcommands:
{darken,lighten}
EOF
) >&2
print_version() (
cat <<EOF
$0 $VERSION
Copyright (C) 2021 Orion Arts
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jeremy Brubaker.
EOF
) >&2
# Functions {{{1
#
lighten_color() {
# Lighten a hexadecimal color by a percentage
# of 0 (do nothing) to 100 (make it white)
r=$(printf "%.f" "$(echo "(255-$1) * $amt + $1" | bc)")
b=$(printf "%.f" "$(echo "(255-$2) * $amt + $2" | bc)")
g=$(printf "%.f" "$(echo "(255-$3) * $amt + $3" | bc)")
color=$((g | (b << 8) | (r <<16)))
printf "(%s,%s,%s) #%x\n" "$r" "$b" "$g" "$color"
}
darken_color() {
# Darken a hexadecimal color by a percentage
# of 0 (do nothing) to 100 (make it black)
r=$(printf "%.f" "$(echo "$amt * $1" | bc)")
b=$(printf "%.f" "$(echo "$amt * $2" | bc)")
g=$(printf "%.f" "$(echo "$amt * $3" | bc)")
color=$(((r<<16) + (b<<8) + g))
printf "#%x (%s,%s,%s)\n" "$color" "$r" "$b" "$g"
}
# Process command line {{{1
#
# Options {{{2
while getopts 'vh' opt; do
case $opt in
v) print_version; exit ;;
h|?) print_help; exit ;;
esac
done
# Subcommand {{{2
case "$1" in
d|da|dar|dark|darke|darken) cmd=darken ;;
l|li|lig|ligh|light|lighte|lighten) cmd=lighten ;;
*) print_help "$0: invalid command: $1"; exit 1 ;;
esac
# Pop the subcommand
shift
# Check for parameters {{{2
if [ "$#" -ne 2 ]; then
print_help "$0: invalid arguments"
exit 1
fi
# Run subcommand {{{1
#
# Convert hex string to decimal
color=$(echo "$1" | sed -e 's/^#//' | tr 'a-f' 'A-F')
if ! echo "$color" | grep -Evq "[^0-9A-F]"; then
printf "%s not hexadecimal\n" "$color" >&2
elif [ "${#color}" -ne 6 ]; then
printf "#%s is not six digits\n" "$color" >&2
fi
color=$(echo "ibase=16; $color" | bc)
# Convert percentage to decimal
amt=$(echo "$2" | tr -d "%")
amt=$(echo "scale=2; 1-$amt/100" | bc)
# Extract RBG in decimal
r=$(( color >> 16 ))
b=$(((color >> 8) & 0x00ff))
g=$(( color & 0x00ff))
case $cmd in
darken) darken_color $r $b $g ;;
lighten) lighten_color $r $b $g ;;
*) printf "How the heck did we get here?\n" >&2 ;;
esac