View unanswered posts | View active topics It is currently Fri Mar 29, 2024 6:47 am



Reply to topic  [ 8 posts ] 
How to fill a specific zone with a color ? 
Author Message
pChart user
pChart user

Joined: Thu Apr 14, 2011 2:50 pm
Posts: 6
Post How to fill a specific zone with a color ?
Hi everyone,

I am trying to draw a kind of mercury thermometer, very simply created with the mix of a rectangle and circle.

I actually have to problems that are almost the same :

- First, since my objects are layered, I cannot use transparency properly ;
- Second, I would like to fill the thermometer dynamically with two different colors, depending on some parameters (to enable it to play its thermometer's role ^^).

So my question is : is there a way to fill in a specific zone of a picture ?

Thanks for reading ! :)


Thu Apr 14, 2011 3:00 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Thu Dec 02, 2010 2:31 pm
Posts: 409
Location: France
Post Re: How to fill a specific zone with a color ?
Maybe the drawPolygon function could help you. Transparency will work as you'll only draw one object.

JD.


Fri Apr 15, 2011 12:19 pm
Profile WWW
pChart user
pChart user

Joined: Thu Apr 14, 2011 2:50 pm
Posts: 6
Post Re: How to fill a specific zone with a color ?
Hi,

Thanks for your answer.
Actually, I am not sure I am able to draw that kind of things with
only one polygon.

But I found something that might be interesting with the gd library,
the function imagecolorset.
But now the question is : is there a way to use these functions with your
objects ? Since your work is based on this library ?


Fri Apr 15, 2011 1:22 pm
Profile
pChart user
pChart user

Joined: Thu Apr 14, 2011 2:50 pm
Posts: 6
Post Re: How to fill a specific zone with a color ?
Well it seems I got a bit confused, but imagecolorset is used on an existing image, so I can use it as a kind of post-treatment.

I think it can be a possible solution.


Fri Apr 15, 2011 2:04 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Thu Dec 02, 2010 2:31 pm
Posts: 409
Location: France
Post Re: How to fill a specific zone with a color ?
If you need to access directly the GD image from your script you can use the $myPicture->picture handle :
Code:
$myPicture = new pImage(100,100);

$black = imagecolorallocate($myPicture->picture, 0, 0, 0);
imagefilledrectangle($myPicture->picture, 10, 10, 90, 90, $black);

$myPicture->picture;

$myPicture->autoOutput("picture.png");


Fri Apr 15, 2011 2:12 pm
Profile WWW
pChart user
pChart user

Joined: Thu Apr 14, 2011 2:50 pm
Posts: 6
Post Re: How to fill a specific zone with a color ?
Ok, i assume you're rather talking about $myPicture->Picture ?

I tried to do it that way ($myPicture being of course a pImage object) :

Code:
$color = imagecolorat($myPicture->Picture, 150, 300);
imagecolorset($myPicture->Picture, $color, 0, 255, 0);

$myPicture->Render("pictures/basic.png");


But the imagecolorset function returns false :? .

What do I get wrong ?


Fri Apr 15, 2011 2:39 pm
Profile
pChart user
pChart user

Joined: Thu Apr 14, 2011 2:50 pm
Posts: 6
Post Re: How to fill a specific zone with a color ?
I am thinking maybe this has to do with my function, so I give you the code in case it's usefull, as well as the picture generated in attachment :

Code:
function buildThermometer($width=300, $height=600, $ratio=0.5, $goal=0.8) {

  /* All the pChart classes needed */
  require_once("application/third_party/pChart2.1.1/class/pDraw.class.php");
  require_once("application/third_party/pChart2.1.1/class/pImage.class.php");
  require_once("application/third_party/pChart2.1.1/class/pSurface.class.php");


  /* the thermometer to be returned */
  $picture = new pImage($width, $height);
  /* margin if the picture */
  $margin = 10;
  /* transparency */
  $alpha = 10;

  /*
   * The thermometer is represented thanks to a circle (the basis)
   * and a rectangle (the pipe).
   */
 
  /* Circle's caracteristics */
  /* the base must be a small part of the thermometre (obviously) */
  $cRadius = ($height - 2*$margin ) / 8 ;
  $cAbs    =  $width / 2;
  $cOrd    =  $height - $margin - $cRadius ;
  $CircleSettings =
                array("R"=>209,"G"=>134,"B"=>27,"Alpha"=>$alpha ,"Surrounding"=>30);

  /* Adding circle to the picture */
  $picture->drawFilledCircle($cAbs,$cOrd,$cRadius, $CircleSettings);
   
 
  /* rectangle's caracteristics */
  $lAbs = ($width - $cRadius)/2 ;    /* left top angle abscissa     */
  $lOrd = $margin;                   /* left top angle ordinate     */
  $rAbs = $width  - $lAbs;           /* right bottom angle abscissa */
  $rOrd = $height - $margin*2;       /* right bottom angle ordinate */

  $RectangleSettings = array("R"=>209,"G"=>134,"B"=>27, "Alpha"=>$alpha);

  /* TODO : find how to optimise the radius value for rectangle's corner */
  /* Adding rectangle to the picture */
  $picture->drawRoundedFilledRectangle($lAbs,$lOrd,$rAbs,$rOrd, 34 ,$RectangleSettings);

    return $picture;
}


Attachments:
basic.png
basic.png [ 6.53 KiB | Viewed 10112 times ]
Fri Apr 15, 2011 2:49 pm
Profile
pChart user
pChart user

Joined: Thu Apr 14, 2011 2:50 pm
Posts: 6
Post Re: How to fill a specific zone with a color ?
Hello again,

I haven't found any solution to this issue, even if I believe the trick to be related with the
picture's resolution. But I found a way to bypass the problem, by using another function : bool imagefill ( resource $image , int $x , int $y , int $color ).

It basically acts like the paint's filling function, and allow me to do what I wanted to.

Hope it will help someone else ;) !


Tue Apr 19, 2011 8:24 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron