SVG feComponentTransfer为图像着色

问题描述 投票:1回答:2

我正在尝试使用滤镜对白色图像进行着色,但是我得到了意想不到的结果

SVG示例:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="373" height="400"
     viewBox="0 0 373 400">

    <defs>
        <filter id="colorize">
            <feComponentTransfer>
                <feFuncR type="linear" slope="0.3333333"></feFuncR>
                <feFuncG type="linear" slope="0"></feFuncG>
                <feFuncB type="linear" slope="0.3333333"></feFuncB>
            </feComponentTransfer>
        </filter>
    </defs>

    <g filter="url(#colorize)">
        <image x="0" y="0" xlink:href="https://i.imgur.com/Df8zD8O.png" width="373" height="400"></image>
    </g>
    <text font-weight="bold" fill="rgb(85,0,85)" x="0" y="100" font-size="100">Surfer</text>

</svg>

预期的结果是图像变为与文本相同的颜色 在这种情况下#550055rgb(85,0,85)

我根据0.3333333的结果将过滤器上的斜率设置为RB85 / 255但是你可以看到结果不正确

也许我正在使用错误的计算方法来获得所需的颜色

需要注意的一点是,具有接近/等于255的组件的颜色会产生更好的结果

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="373" height="400"
     viewBox="0 0 373 400">

    <defs>
        <filter id="colorize">
            <feComponentTransfer>
                <feFuncR type="linear" slope="255"></feFuncR>
                <feFuncG type="linear" slope="0"></feFuncG>
                <feFuncB type="linear" slope="255"></feFuncB>
            </feComponentTransfer>
        </filter>
    </defs>

    <g filter="url(#colorize)">
        <image x="0" y="0" xlink:href="https://i.imgur.com/Df8zD8O.png" width="373" height="400"></image>
    </g>
    <text font-weight="bold" fill="rgb(255,0,255)" x="0" y="100" font-size="100">Surfer</text>

</svg>

我的计算基于这个公式

C' = slope * C + intercept

我究竟做错了什么?

svg colors rgb svg-filters
2个回答
2
投票

most SVG filters is linearRGB的默认颜色空间。你好像假设它是sRGB。

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="373" height="400"
     viewBox="0 0 373 400">

    <defs>
        <filter id="colorize" color-interpolation-filters="sRGB">
            <feComponentTransfer>
                <feFuncR type="linear" slope="0.3333333"></feFuncR>
                <feFuncG type="linear" slope="0"></feFuncG>
                <feFuncB type="linear" slope="0.3333333"></feFuncB>
            </feComponentTransfer>
        </filter>
    </defs>

    <g filter="url(#colorize)">
        <image x="0" y="0" xlink:href="https://i.imgur.com/Df8zD8O.png" width="373" height="400"></image>
    </g>
    <text font-weight="bold" fill="rgb(85,0,85)" x="0" y="100" font-size="100">Surfer</text>

</svg>

2
投票

您必须将颜色空间设置为sRGB - 因为这是CSS rgb()颜色属性使用的颜色空间。 (SVG过滤器的默认颜色空间是linearRGB)。

color-interpolation-filters="sRGB"添加到您的过滤器元素,您将获得您期望的结果。

© www.soinside.com 2019 - 2024. All rights reserved.