Alex
Alex

Reputation: 67248

Blend two colors (GD)

For example:

$im = imagecreatefrompng("php.png");
$rgb = imagecolorat($im, 10, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

(from PHP doc)

Color I want to add:

$r2 = rand(0, 255);
$g2 = rand(0, 255);
$b2 = rand(0, 255);

$color = imagecolorallocatealpha($im, $r2, $g2, $b2, 0);        
imagesetpixel($im, 10, 15, $color); 

So the original color ($r, $g, $b) gets replaced with the new color ($r2, $g2, $b2).

But how can I add just a certain amount of $r2, $g2, $b2, not replace completely.

So if the original color is red, and the second random color I generate is green, I want to add only 10% or 15% of the second color.

Upvotes: 2

Views: 637

Answers (1)

Zar
Zar

Reputation: 6892

I haven't really done much image-processing, so this is just a long shot, but couldn't you do something like this:

function addPercentageToNumber($number, $minPercentage, $maxPercentage) {
   return $number + rand( ($number / 100) * $minPercentage, ($number / 100) * $maxPercentage  );
}

// Base color
$r2 = rand(0, 255);

// Add 10-20%
$r2 = addPercentageToNumber($r2, 10, 20);

You would also need to add some code to handle what happens when a result would be >255 and so on. Hope this helps you a little atleast. :-)

Upvotes: 1

Related Questions