混合颜色GD-PHP [英] Blending colors GD - PHP

查看:80
本文介绍了混合颜色GD-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我有一个64x64像素的图像,有些像素是白色,有些是灰色,有些更暗,所以我还有另一张64x64像素的图像,其中有些黄色的像素将确定必须更改第一幅图像的哪些像素. 到目前为止,我可以使用以下代码更改第一张图像上的颜色,但是问题是我不知道如何将给定颜色与第一张图像上已经存在的颜色融合".

Ok, I have a 64x64 pixels image, some pixels are white, some are grey, and some darker, so I have another 64x64 pixel image with some yellow pixels which will determine which pixels of the first image must be changed. So far I could change the colors on the first image with the following code, but the thing is I have no idea how to "blend" the given color with the colors that were already on the first image.

例如,如果像素为白色(255,255,255)而新颜色为红色(255,0,0),则结果为(255,0,0),但如果像素稍暗,则新的红色也应该更暗.有什么想法吗?

For example, if a pixel is white (255,255,255) and the new color is red (255,0,0) the result will be (255,0,0) but if the pixel is a bit darker, the new red should also be darker. Any ideas?

$image = 'o1.png';
$overlay = 'o2.png';

$background = imagecreatefrompng($image);

imagealphablending($background, true);

// Create overlay image
$overlay = imagecreatefrompng($overlay);

// get size
$size = getimagesize("o2.png");
$L=$size[0];
$H=$size[1];

for($j=0;$j<$H;$j++){
    for($i=0;$i<$L;$i++){

        $rgb = imagecolorat($overlay, $i, $j);

        $red = (isset($_GET['r']) ? $_GET['r'] : 0);
        $green = (isset($_GET['g']) ? $_GET['g'] : 0);
        $blue = (isset($_GET['b']) ? $_GET['b'] : 0);

        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;

        if(($r==255)&&($g==255)&&($b==0)) {

            $color = imagecolorallocate($background, $red, $green, $blue);
            imagesetpixel($background, $i, $j, $color);

        }

    }
}


header("Content-type: image/png");
header("Content-Disposition: filename=" . $image);

imagepng($background);

// Destroy the images
imagedestroy($background);
imagedestroy($overlay);

推荐答案

我认为您正在谈论的是乘法混合模式.根据维基百科的公式是:

I think you are talking about multiply blend mode. The formula for this according to Wikipedia is:

结果颜色=(顶部颜色)*(底部颜色)/255

Result Color = (Top Color) * (Bottom Color) /255

使用此公式,背景色较暗的结果图像将更暗.

Using this formula the resulting image will be darker where the background color is darker.

这篇关于混合颜色GD-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆