aboutsummaryrefslogtreecommitdiff
path: root/tools/colorFixer.sh
blob: 96d15233a6f22c376f88b2a27b8fc919ad7ab778 (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
#!/bin/bash

paletteColors=(
    'rgb(0%,0%,0%)'
    'rgb(100%,100%,100%)'
	'rgb(97.647059%,87.058824%,41.176471%)'
	'rgb(100%,78.431373%,19.215686%)'
	'rgb(100%,78.431373%,19.215686%)'
	'rgb(5.882353%,63.137255%,37.254902%)'
	'rgb(10.588235%,63.921569%,53.72549%)'
	'rgb(90.196078%,90.196078%,90.196078%)'
	'rgb(27.058824%,27.058824%,27.058824%)'
	'rgb(32.156863%,70.196078%,85.098039%)'
	'rgb(21.176471%,48.235294%,94.117647%)'
	'rgb(72.941176%,9.019608%,30.588235%)'
)

function findNearestColor {
    rgb1=($(grep -o -P '(\d+(.\d+)?)(?=%)' <<< $1))
    r1=${rgb1[0]}
    g1=${rgb1[1]}
    b1=${rgb1[2]}
    
    smallerDistance=173
    bestColor=''
    
    for paletteColor in ${paletteColors[@]}
    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'<'$smallerDistance | bc) ))
        then
            smallerDistance=$distance
            bestColor=$paletteColor
        fi
    done
    
    echo $bestColor
}


for file in $(find -maxdepth 1 -type f -printf "%f\n")
do
    echo ===================================================
	svg=$(cat $file)
	file_colors=$(grep -o -P 'rgb\((\d+(.\d+)?%,?){3}\)' $file)
	for file_color in $file_colors
	do
	    new_color=$(findNearestColor $file_color)
	    echo $file_color '->' $new_color
	    	    
		svg=$(sed "s/$file_color/$new_color/g" <<< $svg)
	done
	echo $svg > $file
done