File indexing completed on 2025-01-12 03:40:55

0001 #!/bin/bash
0002 
0003 PARSED_OPTIONS=$(getopt -n "$0"  -o hf: --long "file:,TextFrom:,TextTo:,BackgroundFrom:,BackgroundTo:,HighlightFrom:,HighlightTo:,ViewTextFrom:,ViewTextTo:,ViewBackgroundFrom:,ViewBackgroundTo:,ViewHoverFrom:,ViewHoverTo:,ViewFocusFrom:,ViewFocusTo:,ButtonTextFrom:,ButtonTextTo:,ButtonBackgroundFrom:,ButtonBackgroundTo:,ButtonHoverFrom:,ButtonHoverTo:,ButtonFocusFrom:,ButtonFocusTo:"  -- "$@")
0004 
0005 if [ $? -ne 0 ];
0006 then
0007   exit 1
0008 fi
0009 
0010 eval set -- "$PARSED_OPTIONS"
0011 
0012 textFrom=\#31363b
0013 backgroundFrom=\#eff0f1
0014 highlightFrom=\#3daee9
0015 viewTextFrom=\#31363b
0016 viewBackgroundFrom=\#fcfcfc
0017 viewHoverFrom=\#93cee9
0018 viewFocusFrom=\#3daee9
0019 buttonTextFrom=\#31363b
0020 buttonBackgroundFrom=\#eff0f1
0021 buttonHoverFrom=\#93cee9
0022 buttonFocusFrom=\#3daee9
0023 
0024 textTo=\#31363b
0025 backgroundTo=\#eff0f1
0026 highlightTo=\#3daee9
0027 viewTextTo=\#31363b
0028 viewBackgroundTo=\#fcfcfc
0029 viewHoverTo=\#93cee9
0030 viewFocusTo=\#3daee9
0031 buttonTextTo=\#31363b
0032 buttonBackgroundTo=\#eff0f1
0033 buttonHoverTo=\#93cee9
0034 buttonFocusTo=\#3daee9
0035 
0036 file=''
0037 
0038 while true;
0039 do
0040   case "$1" in
0041  
0042     -h|--help)
0043       echo "usage $0 [-h|options] -f file.svgz"
0044       echo "Where options can be:"
0045       echo " --TextFrom=color html encoded color to replace with the ColorScheme-Text from the stylesheet"
0046       echo " --TextTo=color html encoded that the ColorScheme-Text class will have"
0047       echo
0048       echo " --BackgroundFrom=color html encoded color to replace with the ColorScheme-Background from the stylesheet"
0049       echo " --BackgroundTo=color html encoded that the ColorScheme-Background class will have"
0050       echo
0051       echo " --HighlightFrom=color html encoded color to replace with the ColorScheme-Highlight from the stylesheet"
0052       echo " --HighlightTo=color html encoded that the ColorScheme-Highlight class will have"
0053       echo
0054       echo " --ViewTextFrom=color html encoded color to replace with the ColorScheme-ViewText from the stylesheet"
0055       echo " --ViewTextTo=color html encoded that the ColorScheme-ViewText class will have"
0056       echo
0057       echo " --ViewBackgroundFrom=color html encoded color to replace with the ColorScheme-ViewBackground from the stylesheet"
0058       echo " --ViewBackgroundTo=color html encoded that the ColorScheme-ViewBackground class will have"
0059       echo
0060       echo " --ViewHoverFrom=color html encoded color to replace with the ColorScheme-ViewHover from the stylesheet"
0061       echo " --ViewHoverTo=color html encoded that the ColorScheme-ViewHover class will have"
0062       echo
0063       echo " --ViewFocusFrom=color html encoded color to replace with the ColorScheme-ViewFocus from the stylesheet"
0064       echo " --ViewFocusTo=color html encoded that the ColorScheme-ViewFocus class will have"
0065       echo
0066       echo " --ButtonTextFrom=color html encoded color to replace with the ColorScheme-ButtonText from the stylesheet"
0067       echo " --ButtonTextTo=color html encoded that the ColorScheme-ButtonText class will have"
0068       echo
0069       echo " --ButtonBackgroundFrom=color html encoded color to replace with the ColorScheme-ButtonBackground from the stylesheet"
0070       echo " --ButtonBackgroundTo=color html encoded that the ColorScheme-ButtonBackground class will have"
0071       echo
0072       echo " --ButtonHoverFrom=color html encoded color to replace with the ColorScheme-ButtonHover from the stylesheet"
0073       echo " --ButtonHoverTo=color html encoded that the ColorScheme-ButtonHover class will have"
0074       echo
0075       echo " --ButtonFocusFrom=color html encoded color to replace with the ColorScheme-ButtonFocus from the stylesheet"
0076       echo " --ButtonFocusTo=color html encoded that the ColorScheme-ButtonFocus class will have"
0077       echo
0078       echo "All the colors have default values conformant to the Breeze color palette"
0079       echo
0080       exit
0081      shift;;
0082  
0083     --TextFrom)
0084       textFrom=$2
0085       shift 2;;
0086     --TextTo)
0087       textTo=$2
0088       shift 2;;
0089  
0090     --BackgroundFrom)
0091       backgroundFrom=$2
0092       shift 2;;
0093     --BackgroundTo)
0094       backgroundTo=$2
0095       shift 2;;
0096 
0097     --HighlightFrom)
0098       highlightFrom=$2
0099       shift 2;;
0100     --HighlightTo)
0101       highlightTo=$2
0102       shift 2;;
0103 
0104     --ViewTextFrom)
0105       viewTextFrom=$2
0106       shift 2;;
0107     --ViewTextTo)
0108       viewTextTo=$2
0109       shift 2;;
0110 
0111     --ViewBackgroundFrom)
0112       viewBackgroundFrom=$2
0113       shift 2;;
0114     --ViewBackgroundTo)
0115       viewBackgroundTo=$2
0116       shift 2;;
0117 
0118     --ViewHoverFrom)
0119       viewHoverFrom=$2
0120       shift 2;;
0121     --ViewHoverTo)
0122       viewHoverTo=$2
0123       shift 2;;
0124 
0125     --ViewFocusFrom)
0126       viewFocusFrom=$2
0127       shift 2;;
0128     --ViewFocusTo)
0129       viewFocusTo=$2
0130       shift 2;;
0131 
0132     --ButtonTextFrom)
0133       buttonTextFrom=$2
0134       shift 2;;
0135     --ButtonTextTo)
0136       buttonTextTo=$2
0137       shift 2;;
0138 
0139     --ButtonBackgroundFrom)
0140       buttonBackgroundFrom=$2
0141       shift 2;;
0142     --ButtonBackgroundTo)
0143       buttonBackgroundTo=$2
0144       shift 2;;
0145 
0146     --ButtonHoverFrom)
0147       buttonHoverFrom=$2
0148       shift 2;;
0149     --ButtonHoverTo)
0150       buttonHoverTo=$2
0151       shift 2;;
0152 
0153     --ButtonFocusFrom)
0154       buttonFocusFrom=$2
0155       shift 2;;
0156     --ButtonFocusTo)
0157       buttonFocusTo=$2
0158       shift 2;;
0159 
0160     -f|--file)
0161       file=`echo $2 | cut -d'.' --complement -f2-`
0162       shift 2;;
0163 
0164     --)
0165       shift
0166       break;;
0167   esac
0168 done
0169 
0170 
0171 if [ -z "$file" ]; 
0172     then echo missing svg file
0173     exit 1
0174 fi
0175 
0176 isSvgz=0
0177 
0178 if [ ! -f $file.svgz ] && [ ! -f $file.svg ]; then
0179     echo "you must specify a valid svg"
0180     exit 1
0181 fi
0182 
0183 if [ -f $file.svgz ]; then
0184     isSvgz=1
0185 fi
0186 
0187 
0188 if [ $isSvgz = 1 ]; then
0189     mv $file.svgz $file.svg.gz
0190     gunzip $file.svg.gz
0191 fi
0192 
0193 echo Processing $file
0194 
0195 stylesheet="
0196       .ColorScheme-Text {
0197         color:$textTo;
0198       }
0199       .ColorScheme-Background {
0200         color:$backgroundTo;
0201       }
0202       .ColorScheme-Highlight {
0203         color:$highlightTo;
0204       }
0205       .ColorScheme-ViewText {
0206         color:$viewTextTo;
0207       }
0208       .ColorScheme-ViewBackground {
0209         color:$viewBackgroundTo;
0210       }
0211       .ColorScheme-ViewHover {
0212         color:$viewHoverTo;
0213       }
0214       .ColorScheme-ViewFocus{
0215         color:$viewFocusTo;
0216       }
0217       .ColorScheme-ButtonText {
0218         color:$buttonTextTo;
0219       }
0220       .ColorScheme-ButtonBackground {
0221         color:$buttonBackgroundTo;
0222       }
0223       .ColorScheme-ButtonHover {
0224         color:$buttonHoverTo;
0225       }
0226       .ColorScheme-ButtonFocus{
0227         color:$buttonFocusTo;
0228       }
0229       "
0230 colors=($textFrom $backgroundFrom $highlightFrom $viewTextFrom $viewBackgroundFrom $viewHoverFrom $viewFocusFrom $buttonTextFrom $buttonBackgroundFrom $buttonHoverFrom $buttonFocusFrom)
0231 colorNames=(ColorScheme-Text ColorScheme-Background ColorScheme-Highlight ColorScheme-ViewText ColorScheme-ViewBackground ColorScheme-ViewHover ColorScheme-ViewFocus ColorScheme-ButtonText ColorScheme-ButtonBackground ColorScheme-ButtonHover ColorScheme-ButtonFocus)
0232 
0233 reorderXslt='
0234 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
0235   xmlns:svg="http://www.w3.org/2000/svg">
0236  <xsl:output omit-xml-declaration="yes" indent="yes"/>
0237  <xsl:strip-space elements="*"/>
0238 
0239   <xsl:template match="@* | node()">
0240     <xsl:copy>
0241       <xsl:apply-templates select="@* | node()"/>
0242     </xsl:copy>
0243   </xsl:template>
0244 
0245   <xsl:template match="svg:defs">
0246     <xsl:copy>
0247       <xsl:apply-templates select="@*" />
0248       <xsl:apply-templates select="*">
0249         <xsl:sort select="name()" data-type="text" order="descending"/>
0250       </xsl:apply-templates>
0251     </xsl:copy>
0252   </xsl:template>
0253 
0254 </xsl:stylesheet>
0255 '
0256 echo $reorderXslt > transform.xsl
0257 
0258 if grep -q '"current-color-scheme"' $file.svg; then
0259     echo replacing the stylesheet
0260     xmlstarlet ed --update "/svg:svg/svg:defs/_:style" -v "$stylesheet" $file.svg > temp.svg
0261 else
0262     echo adding the stylesheet
0263 xmlstarlet ed --subnode "/svg:svg/svg:defs" -t elem -n "style" -v "$stylesheet"\
0264        --subnode "/svg:svg/svg:defs/style" -t attr -n "type" -v "text/css"\
0265        --subnode "/svg:svg/svg:defs/style" -t attr -n "id" -v "current-color-scheme" $file.svg > temp.svg
0266 fi
0267 
0268 xmlstarlet tr transform.xsl temp.svg > temp2.svg
0269 mv temp2.svg temp.svg
0270 
0271 for i in ${!colors[@]}
0272 do
0273   xmlstarlet ed --subnode "//*/*[contains(@style, '${colors[i]}') and not (@class)]" -t attr -n "class" -v "${colorNames[i]}" temp.svg > temp2.svg
0274 
0275   mv temp2.svg temp.svg
0276 
0277   sed -i 's/\(style=".*\)fill:'${colors[i]}'/\1fill:currentColor/g' temp.svg
0278   sed -i 's/\(style=".*\)stop-color:'${colors[i]}'/\1stop-color:currentColor/g' temp.svg
0279 done
0280 
0281 rm transform.xsl
0282 
0283 mv temp.svg $file.svg
0284 if [ $isSvgz = 1 ]; then
0285     gzip -n $file.svg
0286     mv $file.svg.gz $file.svgz
0287 fi