RGB to HTML color

Colors

red=51
green=255
blue=51
alpha=128

#%02X: two hexadecimal digits with a leading zero, -v puts the text into a
#variable
printf -v htmlColor '#%02X%02X%02X' "$red" "$green" "$blue"
#htmlColor is "#33FF33"
echo "style=\"color: $htmlColor\""

#with transparency
printf -v htmlColor '#%02X%02X%02X%02X' "$red" "$green" "$blue" "$alpha"
#htmlColor is "#33FF3380"

#%02x gives small letters
printf -v lowerColor '#%02x%02x%02x' "$red" "$green" "$blue"
#lowerColor is "#33ff33"

#trap: a part over 255 gives three digits, and the color breaks
printf -v wrongColor '#%02X%02X%02X' 256 0 0
#wrongColor is "#1000000"

echo "htmlColor is $htmlColor"
echo "lowerColor is $lowerColor"
echo "wrongColor is $wrongColor"