gideondsouza has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to just draw a string over an existing image loaded from disk.

I'm using the GD module from cpan.

I managed to draw the string fine, but the string shows in this weird transparent like color instead of plain black. I see the POD for colorAllocate and colorExact but I really don't get what theyr'e supposed to do, and how I should draw a black coloured string?

This is my code:

$to_print = "some string"; my $im = newFromJpeg GD::Image("somefile.jpg"); my $black = $im->colorExact(0,0,0); #also tried $im->colorAllocate(0,0,0); $im->stringFT($black, '/Open_Sans/OpenSans-Bold.ttf' ,12, 0, 100, 50, +$to_print);

The color returned is also a -1, So what is the right way to set black as a color?

It just prints a wierd transparent color, that looks a lot like the background of the image. (The background is a texture). And changing the RGB values doesn't help in any way!

Any help appreciated! :)

Replies are listed 'Best First'.
Re: how can I set a real color while drawing text with GD
by Loops (Curate) on Aug 08, 2013 at 19:05 UTC
    Hi,

    So by default GD only supports 256 colors. The image you are using must not have any pixel set to (0,0,0) so there is no way to match it exactly or to allocate it for that matter. What you could do is:

    my $black = $im->colorClosest(0,0,0);

    Which will work and get you something very close to (0,0,0) and likely be indistinguishable visually. However if you set truecolor right after importing GD, you can avoid all these issues and your colorAllocate above will work. However i'd suggest using colorResolve instead, which will use an existing index or create a new one automatically if needed:

    use GD; GD::Image->trueColor(1); ... my $black = $im->colorResolve(0,0,0);
    HTH.

      Thanks very much! This helps.

      Where can I read more about all this color table stuff, the API seems to involve a lot of internal image stuff.