代码之家  ›  专栏  ›  技术社区  ›  onassar

是否可以检测SVG中的独特颜色?

  •  0
  • onassar  · 技术社区  · 6 年前

    我想访问SVG可能使用的所有颜色。我和你玩过 convert ,但希望了解一些关于如何找出SVG可能包含的独特颜色的指导。

    这是我写的PHP代码 ,以尝试确定位图是否包含颜色,但它非常有限:

    /**
     * containsColor
     * 
     * @see     https://www.imagemagick.org/discourse-server/viewtopic.php?t=19580&start=15
     * @see     https://superuser.com/questions/508472/how-to-recognize-black-and-white-images
     * @see     https://www.imagemagick.org/discourse-server/viewtopic.php?t=19580
     * @access  public
     * @static
     * @param   string $path
     * @return  bool
     */
    public static function containsColor(string $path): bool
    {
        $commands = array(
            'convert ' .
                ($path) .' ' .
                '-format "%[colorspace]" info:'
        );
        $command = implode(' && ', $commands);
        $response = exec($command);
        $color = strtolower($response) !== 'gray';
        return $color;
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   emcconville    6 年前

    如果我们回顾 Format and Print Image Properties 文档中,我们应该能够识别唯一的颜色计数 -format %k .

    public static function containsColor(string $path): int
    {
        $commands = array(
            'convert ' .
                ($path) .' ' .
                '-format "%k" info:'
        );
        $command = implode(' && ', $commands);
        $response = exec($command);
        return (int)$response;
    }
    

    如果要计算SVG后期渲染使用的所有颜色,可以使用 -unique-colors

    convert input.svg -depth 8 -unique-colors txt:-
    

    它将输出一些很容易被PHP解析的东西。

    # ImageMagick pixel enumeration: 403,1,65535,srgba
    0,0: (0,0,0,65535)  #000000FF  black
    1,0: (257,0,0,65535)  #010000FF  srgba(1,0,0,1)
    2,0: (257,257,257,65535)  #010101FF  srgba(1,1,1,1)
    3,0: (257,257,0,65535)  #010100FF  srgba(1,1,0,1)
    4,0: (514,257,0,65535)  #020100FF  srgba(2,1,0,1)
    5,0: (771,771,771,65535)  #030303FF  grey1
    ...
    

    请记住,SVG实际上只是XML,因此可以将其加载到 DOMDocument 类和使用 DOMXPath 提取颜色属性。但正如评论中正确提到的,您将无法识别CSS3过滤器或高级颜色渲染&混合。

    使用工作示例更新

    public static function containsColor(string $input) : bool
    {
        $pixelInfoList = [];
        exec(sprintf('convert "%s" -depth 8 -alpha Off --colors 255 -unique-colors txt:-', $input), $pixelInfoList);
        // ... Insert error handling here ...
        for($index = 1; $index < count($pixelInfoList); $index++) {
            preg_match('/\((\d+),(\d+),(\d+)\)/', $pixelInfoList[$index], $colorParts);
            if ($colorParts[1] == $colorParts[2] && $colorParts[2] == $colorParts[3]) {
                // Color is gray. Do nothing?
            } else {
                // Non-gray color. Stop search, and return.
                return true;
            }
        }
        return false;
    }
    

    这项工作是评估输出的颜色通道 txt:- . 如果红色、绿色和蓝色通道相同,则可以将其表示为灰色&继续下一行,否则我们可以声明存在非灰色&停止迭代。我也在利用图书馆 -alpha Off -colors 255 把额外的数据整理出来。

        2
  •  0
  •   Moini    6 年前

    这可能对您没有帮助,因为您似乎想将其用于服务器应用程序,但它可能会帮助其他人解决同样的问题。

    文件>保存副本:GIMP调色板(*.gpl)

    但是,这不会保存渐变中显示的所有颜色,只保存文件中实际使用的颜色。