Page 1 of 1

identify -list Color (as 6 hex digits)

Posted: 2012-03-12T14:04:56-07:00
by ajg
Hey all,

How do I format identify -list Color to show the real names and hex values in two columns? Is there a way to convert from rgb() using some IM internal?

Currently as a default I get:

Code: Select all

yellow4               rgb(139,139,0)                                X11
Thanks,

Adam

EDIT: I should mentioned I want to use this output for use in a Python script, I need to generate a tuple with real names and hex

Re: identify -list Color

Posted: 2012-03-12T16:14:29-07:00
by fmw42
In unix, do


colorlist=`convert -list color | tail -n +6 | cut -d\ -f1 | tr "\012" " "`
for color in $colorlist; do
hexcolor=`convert xc:"$color" txt: | tail -n 1 | sed -n 's/^.*\([\#][0-9,A-F,a-f]*\) *.*$/\1/p'`
echo "$color $hexcolor"
done

Re: identify -list Color

Posted: 2012-03-12T17:39:54-07:00
by ajg
Thanks Fred! Ingenious UNIX script :) On Mac OS Lion term the first line needed ammending, but the following was an equivalent:

Code: Select all

for color in `/opt/bin/convert -list color | awk '{print $2}'`; do
    hexcolor=`/opt/bin/convert xc:"$color" txt: | tail -n 1 | sed -n 's/^.*\([\#][0-9,A-F,a-f]*\) *.*$/\1/p'`;
    echo $hexcolor;
done
I'm unsure of how hex colors work (will go look it up), how would I convert to 6-character hex i.e. for web?

Cheers

Edit: If you could explain how your sed statement works, that'd be awesome. I'm more used to grep & awk :?

Re: identify -list Color

Posted: 2012-03-12T18:05:35-07:00
by fmw42
The IM hex values are 16 bit values. To change them to 8 bit values add -depth 8 to your command.

colorlist=`convert -list color | tail -n +6 | cut -d\ -f1 | tr "\012" " "`
for color in $colorlist; do
hexcolor=`convert xc:"$color" -depth 8 txt: | tail -n 1 | sed -n 's/^.*\([\#][0-9,A-F,a-f]*\) *.*$/\1/p'`
echo "$color $hexcolor"
done




hexcolor=`convert xc:"$color" txt: | tail -n 1 | sed -n 's/^.*\([\#][0-9,A-F,a-f]*\) *.*$/\1/p'`

the txt: outputs two lines, the second one is the color info, so tail -n 1 just grabs the last (second line).

the sed skips all characters until it finds the stuff inside \(...\) --- that is the ^.* does the skip
it then looks for # with characters 0-9 or A-F or a-f of any length and saves that --- that is \([\#][0-9,A-F,a-f]*\)
it finds and skips the next space and everything else --- that is *.*$
it returns the saved info --- that is \1
the -n says no return until told to print, which is the p at the end.

Lots of sed information by searching google for bash sed.

I am not as familiar with awk, which would be faster, but I have used it at times where speed was of the essence.


My unix may not be the most efficient, but it got the job done on my Mac OSX Snow Leopard.

Re: identify -list Color

Posted: 2012-03-12T23:35:56-07:00
by anthony
For another script that processes the color list see "hsl_named_colors"
http://www.imagemagick.org/Usage/script ... med_colors

Image

See Colors by Name
http://www.imagemagick.org/Usage/color_basics/#colors