-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_colors.sh
executable file
·202 lines (191 loc) · 7.57 KB
/
utils_colors.sh
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
# ##############################################################################
# Script Name : utils_colors.sh
# Description : Common function for shell projects working with colors
# Author : Kevin GRILLET
# GitHub : https://github.com/kevingrillet/ShellUtils
# License : GNU GPL-3
# Require : utils_maths.sh
# ##############################################################################
[ -n "${UTILS_COLORS}" ] && return; UTILS_COLORS=0; #pragma once
# ##############################################################################
# Function Name : sRGBColorDelta
# Args : <COLOR1> <COLOR2>
# Output : stdout [0 means same colors, 100 means opposite colors]
# Source : https://stackoverflow.com/a/22692625/7295428
# ##############################################################################
sRGBColorDelta() {
if [ "$#" -ne 2 ] ; then
echo "Usage: sRGBColorDelta <COLOR1> <COLOR2>" >&2
echo " 0 means same colors, 100 means opposite colors" >&2
return 1
fi
r=$((0x${1:0:2} - 0x${2:0:2}))
g=$((0x${1:2:2} - 0x${2:2:2}))
b=$((0x${1:4:2} - 0x${2:4:2}))
d=$((((765 - (${r#-} + ${g#-} + ${b#-})) * 100) / 765)) # 765 = 3 * 255
d=$((100-d))
echo $d >&1
}
# ##############################################################################
# Function Name : sRGBColorDistance
# Args : <COLOR1> <COLOR2>
# Output : stdout (6468 means opposit colors, 0 means same colors)
# Source : https://stackoverflow.com/a/9085524/7295428
# ##############################################################################
sRGBColorDistance() {
if [ "$#" -ne 2 ] ; then
echo "Usage: sRGBColorDistance <COLOR1> <COLOR2>" >&2
echo " 6468 means opposit colors, 0 means same colors" >&2
return 1
fi
rmean=$(((0x${1:0:2} + 0x${2:0:2}) / 2))
r=$((0x${1:0:2} - 0x${2:0:2}))
g=$((0x${1:2:2} - 0x${2:2:2}))
b=$((0x${1:4:2} - 0x${2:4:2}))
d=$(((((512 + rmean) * r * r) / 2) + 4 * g * g + (((767 - rmean) * b * b) / 2)))
sqrt $d
}
# ##############################################################################
# Section : sRGB Euclidean Distance
# Source : https://www.compuphase.com/cmetric.htm
# ##############################################################################
# ##############################################################################
# Function Name : sRGBEuclideanDistance
# Args : <COLOR1> <COLOR2>
# Output : stdout (441 means opposit colors, 0 means same colors)
# Source : https://www.compuphase.com/cmetric.htm
# ##############################################################################
sRGBEuclideanDistance() {
if [ "$#" -ne 2 ] ; then
echo "Usage: sRGBEuclideanDistance <COLOR1> <COLOR2>" >&2
echo " 441 means opposit colors, 0 means same colors" >&2
return 1
fi
r=$((0x${1:0:2} - 0x${2:0:2}))
g=$((0x${1:2:2} - 0x${2:2:2}))
b=$((0x${1:4:2} - 0x${2:4:2}))
d=$((r * r + g * g + b * b))
sqrt $d
}
# ##############################################################################
# Function Name : sRGBEuclideanDistanceWeighted
# Args : <COLOR1> <COLOR2>
# Output : stdout (765 means opposit colors, 0 means same colors)
# Source : https://www.compuphase.com/cmetric.htm
# ##############################################################################
sRGBEuclideanDistanceWeighted() {
if [ "$#" -ne 2 ] ; then
echo "Usage: sRGBEuclideanDistanceWeighted <COLOR1> <COLOR2>" >&2
echo " 765 means opposit colors, 0 means same colors" >&2
return 1
fi
rmean=$(((0x${1:0:2} + 0x${2:0:2}) / 2))
r=$((0x${1:0:2} - 0x${2:0:2}))
g=$((0x${1:2:2} - 0x${2:2:2}))
b=$((0x${1:4:2} - 0x${2:4:2}))
if [[ $rmean -lt 128 ]]; then
d=$((2 * r * r + 4 * g * g + 3 * b * b))
else
d=$((3 * r * r + 4 * g * g + 2 * b * b))
fi
sqrt $d
}
# ##############################################################################
# Function Name : sRGBEuclideanDistanceRedmean
# Description : A low-cost approximation
# Args : <COLOR1> <COLOR2>
# Output : stdout (721 means opposit colors, 0 means same colors)
# Source : https://www.compuphase.com/cmetric.htm
# ##############################################################################
sRGBEuclideanDistanceRedmean() {
if [ "$#" -ne 2 ] ; then
echo "Usage: sRGBEuclideanDistanceRedmean <COLOR1> <COLOR2>" >&2
echo " 721 means opposit colors, 0 means same colors" >&2
return 1
fi
rmean=$(((0x${1:0:2} + 0x${2:0:2}) / 2))
r=$((0x${1:0:2} - 0x${2:0:2}))
g=$((0x${1:2:2} - 0x${2:2:2}))
b=$((0x${1:4:2} - 0x${2:4:2}))
d=$(((2 + rmean / 256) * r * r + 4 * g * g + (2 + (255 - rmean) / 256) * b * b))
sqrt $d
}
# ##############################################################################
# Function Name : sRGBExtractColor
# Description : Extract color from RGB
# Args : <COLOR> <R/G/B>
# Output : stdout
# ##############################################################################
sRGBExtractColor() {
if [ "$#" -ne 2 ]; then echo "Usage: sRGBExtractColor <COLOR> <R/G/B>" >&2; return 1; fi
case $2 in
r|R)
echo "${1:0:2}" >&1
;;
g|G)
echo "${1:2:2}" >&1
;;
b|B)
echo "${1:4:2}" >&1
;;
esac
}
# ##############################################################################
# Section : sRGB Luminance
# Source : https://stackoverflow.com/a/596243/7295428
# ##############################################################################
# ##############################################################################
# Function Name : sRGBLuminance
# Args : <COLOR>
# Output : stdout (255 means light, 0 means dark)
# Source : https://en.wikipedia.org/wiki/Relative_luminance
# ##############################################################################
sRGBLuminance() {
if [ "$#" -ne 1 ] ; then
echo "Usage: sRGBLuminance <COLOR>" >&2
echo " 255 means light, 0 means dark" >&2
return 1
fi
r=$((0x${1:0:2}))
g=$((0x${1:2:2}))
b=$((0x${1:4:2}))
l=$(((2126 * r + 7152 * g + 722 * b) / 10000))
echo $l >&1
}
# ##############################################################################
# Function Name : sRGBLuminanceW3
# Args : <COLOR>
# Output : stdout (255 means light, 0 means dark)
# Source : https://www.w3.org/TR/AERT/#color-contrast
# ##############################################################################
sRGBLuminanceW3() {
if [ "$#" -ne 1 ] ; then
echo "Usage: sRGBLuminanceW3 <COLOR>" >&2
echo " 255 means light, 0 means dark" >&2
return 1
fi
r=$((0x${1:0:2}))
g=$((0x${1:2:2}))
b=$((0x${1:4:2}))
l=$(((299 * r + 587 * g + 114 * b) / 1000))
echo $l >&1
}
# ##############################################################################
# Function Name : sRGBLuminanceHSP
# Args : <COLOR>
# Output : stdout (255 means light, 0 means dark)
# Source : https://alienryderflex.com/hsp.html
# ##############################################################################
sRGBLuminanceHSP() {
if [ "$#" -ne 1 ] ; then
echo "Usage: sRGBLuminanceHSP <COLOR>" >&2
echo " 255 means light, 0 means dark" >&2
return 1
fi
r=$((0x${1:0:2}))
g=$((0x${1:2:2}))
b=$((0x${1:4:2}))
l=$(((299 * r * r + 587 * g * g + 114 * b * b) / 1000))
sqrt $l
}