blob: 0e3796d4419c4fc5a6674fc92b4f830d0f945dc6 (
plain) (
blame)
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
|
#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
PALETTE_FILE=${DIR}/paletteColors
FILE_NAME=$1
function hexToRgb {
color=$1
if [[ $(echo $color | wc -c) = 5 ]]
then
color="#${color:1:1}${color:1:1}${color:2:1}${color:2:1}${color:3:1}${color:3:1}"
fi
printf "rgb(%s%%,%s%%,%s%%)" \
$(echo "scale=3;$((0x${color:1:2}))*100/255" | bc) \
$(echo "scale=3;$((0x${color:3:2}))*100/255" | bc) \
$(echo "scale=3;$((0x${color:5:2}))*100/255" | bc)
}
function findNearestColor {
paletteColors=$(for color in $(cat "$PALETTE_FILE")
do
echo $(hexToRgb $color)
done)
rgb1=($(grep -o -P '(\d+(.\d+)?)(?=%)' <<< $1))
r1=${rgb1[0]}
g1=${rgb1[1]}
b1=${rgb1[2]}
smallestDistance=173
bestColor=''
while read -r paletteColor
do
rgb2=($(grep -o -P '(\d+(.\d+)?)(?=%)' <<< $paletteColor))
r2=${rgb2[0]}
g2=${rgb2[1]}
b2=${rgb2[2]}
distance=$(echo "scale=3;sqrt(($r2-$r1)^2 + ($g2-$g1)^2 + ($b2-$b1)^2)" | bc)
if (( $(echo $distance'<'$smallestDistance | bc) ))
then
smallestDistance=$distance
bestColor=$paletteColor
fi
done <<< $paletteColors
echo $bestColor
}
for file in $(find -iname "$FILE_NAME" -maxdepth 1 -type f -printf "%f\n")
do
echo ===================================================
echo $file
svg=$(cat $file)
file_colors=$(grep -o -P -i 'rgb\((\d+(.\d+)?%,?){3}\)|#((\d|[abcdef]){3}){1,2}' $file | sort -u)
for file_color in $file_colors
do
if [[ ${file_color:0:1} = '#' ]]
then
file_color_rgb=$(hexToRgb $file_color)
new_color=$(findNearestColor $file_color_rgb)
else
new_color=$(findNearestColor $file_color)
fi
echo $file_color '->' $new_color
svg=$(sed "s/$file_color/$new_color/g" <<< $svg)
done
echo $svg > $file
done
|