Prossibly use Imager; it has methods like getcolorcount or getcolorusage where you could maybe look and see how used your watermark color is.

Edit: rough sample finding a single color. You'd need to walk the color histogram hash and maybe look and find how many pixels are using below whatever threshold of "too dark" pixel.

my $img = Imager->new->open( file => q{myfile.jpg} ); my $colors = $img->getcolorusagehash(); my $color_black = pack( q{CCC}, 0, 0, 0 ); say qq{True black used }, $colors->{$color_black}, qq{ times.}

Edit again: More thinking about it what I'd do (vague handwaving, but I offer a link!) is use the histogram hash and sort the keys (they're packed RGB values so when you sort them asciibetically it should do the right thing). You can compute a luminance value you consider "too dark" and then figure out what percentage of the image pixels are less than that value; if it's over whatever threshold, swap to your backup light color. Theoretically you could even copy out the subsection of your image where you're contemplating putting the watermark and just get the color usage for that subsection.

Another thought: Actually looking at your second sample image another option rather than changing the watermark color might be if it's too dark then just write it first 2 pixels larger in a lighter color and then write the dark version on top (so it'd be outlined).

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: Adding a watermark to an image with GD::Image by Fletch
in thread Adding a watermark to an image with GD::Image by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.