Category Archives: Nedoloceno

Randomize xterm colors

A bash script to randomize the xterm colors defined in the .Xresources file.

Requirements: An original .Xresources file. It must, among other definitions, specify all the neccesary colors in the following form (order is irrellevant):

xterm*color0    :  #151503
xterm*color1    :  #843706
xterm*color2    :  #69750e
...
xterm*color15    :  #f4dfd8
xterm*background    :  #211d22

Usage:
Navigate to the directory where your system’s .Xresources file is located (most likely your home directory). Make sure it complies with the listed requirements. If so, copy it into a new file to which we from now on refer as the original file. Else edit it so it does and then create the copy:

 cp .Xresources .XresourcesOriginal 

Now we are ready to run the script. Verify that it is located in current directory and classified executable. Then run:

 ./randXcolors .XresourcesOriginal .Xresources 10 

Fell free to replace the number 10 with any integer in range from 0 to 255. That is the limit on the randomization range. As it increases the output will in average differ more and more from the original.
After the new .Xresources is generated, you can load it:

 xrdb .Xresources 

Restart your xterm to see the change take place. Repeating this process a couple of times will likely provide you with a truly amazing xterm color scheme.

Code:
Paste this into the randXcolor file.

#!/bin/bash 

# randXcolors - a bash script to randomize xterm colors

# Operating on the standard Xresources file it changes the xterm*color* and
# xterm*background variables in order to produce a copy with colors randomized.#
# The new color definitions are placed at the end of the new file.

# args: 1. filename - Original Xresources file
#       2. filename - Output Xresurces file
#       3. integer - The radius of randomization with regard to the 255 color
#           range

usage(){
    echo -e "Usage: $0 originalFilename destinationFilename randomRadius\n"
    exit 1
}

function rgb2hex()
#args: RRRGGGBBB color, -001<RRR<256 etc.
#returns #HEXHEX color
{
    local hex1=$(printf '%02x' $((10#${1:0:3}))) #10# transf 2 dec, $1 is arg
    local hex2=$(printf '%02x' $((10#${1:3:3})))
    local hex3=$(printf '%02x' $((10#${1:6:3})))
    echo "#$hex1$hex2$hex3"
}

function hex2rgb()
#args: #HEXHEX color
#returns RRRGGGBBB color
{
    local rgb1=$(printf '%03d' 0x${1:1:2})
    local rgb2=$(printf '%03d' 0x${1:3:2})
    local rgb3=$(printf '%03d' 0x${1:5:2})
    echo "$rgb1$rgb2$rgb3"
}

function randomizeHex()
#args #HEXHEX color, randomization radius
#returns #HEXHEX randomized color
{
    local rgb=$(hex2rgb $1)
    declare -a local rgbArr=(${rgb:0:3} ${rgb:3:3} ${rgb:6:3})
    local rgbRand=""
    for i in "${rgbArr[@]}"
    do
        local substr=$(echo $(($(shuf -i 0-$(($2 + $2)) -n 1) - $2)))
        local sum=$((10#$i + $substr))
        if [ $sum -gt "255" ]; then
            sum="255"
        elif [ $sum -lt "0" ]; then
            sum="0"
        fi
        rgbRand="$rgbRand$(printf '%03d' $sum)"
    done
    echo $(rgb2hex $rgbRand)
}

#input check
[[ $# -ne 3 ]] && echo -e "\nERROR\nWrong number of arguments.\n" && usage
[[ $1 == $2 ]] && echo -e "\nERROR\nDo not use the same file as input and"\
    "output.\nExiting...\n"\ && exit 1
[[ $3 -gt 255 ]] || [[ $3 -lt 0 ]] && echo -e "\nERROR.\nThe randomization"\
    "radius too large or too small to make any sense.\nUse values between 0"\
    "and 255, with less then 100 recommended.\nExiting...\n" && exit 1

#"main" function
cp "$PWD/$1" "$PWD/$2"
sed -i '/[Xx][Tt]erm\*color*/d' "$PWD/$2"
sed -i '/[Xx][Tt]erm\*background*/d' "$PWD/$2"
count=0
while read l; do
    if [ ${l:0:1} != "!" ]; then #dont touch comments
        if [[ $l == *[Xx][Tt]erm\*color* ]]; then
            echo "xterm*color"$count"    :  "$(randomizeHex "#${l#*#}" $3) \
                >> "$PWD/$2"
            count=$(($count + 1))
        elif [[ $l == *[Xx][Tt]erm\*background* ]]; then
            echo "xterm*background    :  "$(randomizeHex "#${l#*#}" $3) \
                >> "$PWD/$2"
        fi
    fi
done < "$PWD/$1"