[SOLVED] Getting complementary color from a given color

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
cubancigar11
Posts: 3
Joined: 2014-06-07T03:31:09-07:00
Authentication code: 6789

[SOLVED] Getting complementary color from a given color

Post by cubancigar11 »

Hi!

I am trying to find the two colors from an image: the dominant one, and its complementary. From viewtopic.php?f=1&t=12878, I am able to get the dominant color using following command:

Code: Select all

convert img.png -colors 16 -depth 8 -format "%c" histogram:info:|sort -rn|head -n 1|grep -oe '#[^\s]*'
But I don't want to process the same image again to get the complementary. Is it possible?

PS: I am not looking for exact values for colors.
Last edited by cubancigar11 on 2014-06-07T09:10:20-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting complementary color from a given color

Post by snibgo »

The complement of a colour is (usually) the same lightness and saturation, with the hue rotated by 180 degrees, or 50%. We can do this with "-modulate":

Code: Select all

convert xc:red -modulate 100,100,0 txt:
For "red", use any color you like, eg

Code: Select all

convert xc:srgb(10%,20%,30%) -modulate 100,100,0 txt:
snibgo's IM pages: im.snibgo.com
cubancigar11
Posts: 3
Joined: 2014-06-07T03:31:09-07:00
Authentication code: 6789

Re: Getting complementary color from a given color

Post by cubancigar11 »

Thanks. I am assuming there is no way to print it in #rrggbb format instead of #rrrrggggbbbb. But that is not a problem. Thanks again.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [SOLVED] Getting complementary color from a given color

Post by fmw42 »

add -depth 8 to snibgo's solution.

convert xc:"srgb(10%,20%,30%)" -modulate 100,100,0 -depth 8 txt:
cubancigar11
Posts: 3
Joined: 2014-06-07T03:31:09-07:00
Authentication code: 6789

Re: [SOLVED] Getting complementary color from a given color

Post by cubancigar11 »

Thanks! That does it!

Code: Select all

% convert xc:'#004d09' -modulate 100,100,0 -depth 8 txt:
# ImageMagick pixel enumeration: 1,1,255,srgb
0,0: ( 77,  0, 68)  #4D0044  srgb(77,0,68)
Post Reply