Re: Fetch and store color value in a variable

Anthony Thyssen <[email protected]>
Newsgroups gmane.comp.video.image-magick.user
Message-ID <20091112091600.40ba884b@wraith>
On Wed, 11 Nov 2009 02:32:37 -0800 (PST)
mini prashanth <[email protected]> wrote:

| Hi,
| Iam basically trying to fetch the color value of the pixel at position 0,0 and store it in the hex format "#cccccc" in a variable . 
| 
| FOR /F %%i IN ('identify -format "%%[fx:p{0,0}]" "img.jpg"') DO SET x=%%i (here iam getting some decimal value like x=0.298110)
| 
| I tried to change it into Hex value by using this..
| 
| For /F %%s IN ('EVAL "rgb(%x%)"') Do SET hexColor=%%s
| 
| This is not working ..Please Help
| 
fx only works with single values. That number if just the red color value
as a normalized 0 to 1 value.

color  will return a color value which IM can use. Often it will be
a rgb(...) color, but sometimes it will return the corresponding color
name  'black' 'white' 'red' puce' etc.  It is not in hexadecimal format.

   convert rose: -format "%[pixel:u]" info:-
   rgb(48,47,45)


As Fred pointed out, you can extract the value from a TXT: output

  convert rose:[1x1+0+0] txt:
  # ImageMagick pixel enumeration: 1,1,255,rgb
  0,0: ( 48, 47, 45)  #302F2D  rgb(48,47,45)

But as you are windows  'sed' is not really available, and windows
command line string handling abilities is pretty non-existant.

You can use -fx to extract each of the three color values (red, green,
blue), but you will get them as a floating point numbers, or intergers.

  convert rose: -crop 1x1+0+0 \
          -format "%[fx:int(255*r)],%[fx:int(255*g)],%[fx:int(255*b)]" \
          info:-
  48,47,45

If %[fx:..] had a awy to format its output we could ask it to output
the values as 2 digit hexadecimal values ( printf format "%02X" )
but that feature is not available at this time, and you again do not
have access to the UNIX 'printf' command to convert them.


I am sorry their is no simple way to extract the color, as a hexadecimal
directly from the command line ImageMagick.  The values are there just
not in the right format.


  Anthony Thyssen ( System Programmer )    <[email protected]>
 --------------------------------------------------------------------------
    In some parts of the city curiosity didn't just kill the cat,
    It threw it in the river with lead weights tied to its feet.
                                            -- Terry Pratchett, "Sourcery"
 --------------------------------------------------------------------------
     Anthony's Home is his Castle     http://www.cit.gu.edu.au/~anthony/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.