identify -list Color (as 6 hex digits)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
User avatar
ajg
Posts: 4
Joined: 2012-03-04T07:13:35-07:00
Authentication code: 8675308
Location: London, England

identify -list Color (as 6 hex digits)

Post 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
Last edited by ajg on 2012-03-13T06:01:59-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: identify -list Color

Post 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
User avatar
ajg
Posts: 4
Joined: 2012-03-04T07:13:35-07:00
Authentication code: 8675308
Location: London, England

Re: identify -list Color

Post 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 :?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: identify -list Color

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: identify -list Color

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply