如何调整此功能以允许抖动?

问题描述

| 我在下面编写了函数。它将一种颜色切换为另一种颜色。进入的图片是具有各种颜色和白色背景的css精灵。 因此,精灵1可能是蓝色,而精灵2可能是绿色。 该函数将运行两次以用所需的颜色替换蓝色+绿色。
/**
 * Changes the color of a graphic.
 * $settings = array (
 *  \'icon\'
 *  \'new_icon\'
 *  \'old_color\' = array
 *  \'new_color\' = array
 * );
*/
function updateIconColor($settings=array()) {
    // Create Image
    $image = imagecreatefrompng($settings[\'icon\']);

    // Convert True color image to a palatte
    imagetruecolortopalette($image,false,255);

    // Restore Alpha
    $white = imagecolorclosest($image,255,255);
    imagecolortransparent($image,$white);

    // Find + Set color
    $index = imagecolorclosest($image,$settings[\'old_color\'][0],$settings[\'old_color\'][1],$settings[\'old_color\'][2]);
    imagecolorset($image,$index,$settings[\'new_color\'][0],$settings[\'new_color\'][1],$settings[\'new_color\'][2]);

    // Restore Alpha
    imageAlphaBlending($image,true);
    imageSaveAlpha($image,true);

    // Save
    imagepng($image,$settings[\'new_icon\']); // save image as gif
    imagedestroy($image);
}
我需要允许对这些图像进行抖动处理。有没有一种方法可以修改功能以应对抖动图像或添加抖动本身?     

解决方法

要在调色板转换中添加抖动,请更改:
imagetruecolortopalette($image,false,255);
至:
imagetruecolortopalette($image,true,255);