混合颜色 GD - PHP

发布于 2024-12-14 12:05:20 字数 1315 浏览 0 评论 0原文

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

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

$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);

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.

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2024-12-21 12:05:20

我认为你正在谈论多重混合模式。根据维基百科,其公式为:

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

使用此公式,背景颜色较暗的情况下生成的图像会较暗。

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文