#! /bin/bash
# Last edited on 2025-07-21 20:18:57 by stolfi

# USAGE: show_imagemagick_colors.sh > sample.png

tmp="/tmp/$$"

convert -list color \
  | gawk '/[ \011]srgb/{print}' \
  | sed -e 's:[ \011][ \011]*srgb.*$::g' \
  | sed -e 's:[ \011][ \011]*::g' \
  | tr 'A-Z' 'a-z' \
  | sort | uniq \
  > ${tmp}.colors

echo "=== colors ===" 1>&2
cat ${tmp}.colors 1>&2

ts=12 # Text pointsize
dy=$(( ( ${ts} * 9 ) / 5 )) # Vertical spacing

nc=`cat ${tmp}.colors | wc -l`
echo "${nc} colors found" 1>&2

nx=400
ny=$(( ${dy} * ( ${nc} + 2 ) ))
echo "will create a ${nx} by ${ny} image" 1>&2

rm -f ${tmp}.cmds
printf "  -textcolor black \n" >> ${tmp}.cmds
y=${dy}
for c in `cat ${tmp}.colors` ; do
  xd="${dy}"; yd="${y}"
  xp=$(( ${xd} + 2 )); yp="${y}"
  printf "  -color %s -LD %5d %5d  %5d %5d o \n" "${c}" "${xp}" "${yp}" "${xd}" "${yd}" >> ${tmp}.cmds
  y=$(( y + ${dy} ))
done

printf "  -textcolor black \n" >> ${tmp}.cmds
y=${dy}
for c in `cat ${tmp}.colors` ; do 
  xt=$(( 3 * ${dy} )); yt="${y}";
  printf "  -T %5d %5d 0.0 0.5 %s \n" "${xt}" "${yt}" "${c}" >> ${tmp}.cmds
  y=$(( y + ${dy} ))
done

echo "=== commands ===" 1>&2
cat ${tmp}.cmds 1>&2
 
echo "creating background image ..." 1>&2
convert -size "${nx}x${ny}" xc:gray50 ${tmp}-bg.png
identify ${tmp}-bg.png 1>&2

echo "annotating image ..." 1>&2
annotate-pic.sh \
  ${tmp}-bg.png \
  ${ts} Black Yellow \
  `cat ${tmp}.cmds`

echo "cleanup ..." 1>&2
rm -f ${tmp}.colors ${tmp}.cmds ${tmp}-bg.png 

echo "done." 1>&2

