【问题标题】:How to interpolate hue with svg gradient?如何用 svg 渐变插入色调?
【发布时间】:2016-03-18 10:45:14
【问题描述】:

我想要一个 svg 形状,其渐变从 hsl(0, 100%, 50%)hsl(360, 100%, 50%),色调从 0 -> 360 平稳运行,以创建类似:

当我用这些停止颜色制作渐变时:

<linearGradient id="Gradient1">
  <stop offset="0%" stop-color="hsl(0, 100%, 50%)"/>
  <stop offset="100%" stop-color="hsl(360, 100%, 50%)"/>
</linearGradient>

…它会产生一个完全红色的渐变

我已经设法通过添加更多站点来稍微hack around它:

<linearGradient id="Gradient2">
  <stop offset="0%" stop-color="hsl(0, 100%, 50%)"/>
  <stop offset="1%" stop-color="hsl(3, 100%, 50%)"/>
  <stop offset="2%" stop-color="hsl(7, 100%, 50%)"/>
  <!-- Lots more -->
  <stop offset="98%" stop-color="hsl(352, 100%, 50%)"/>
  <stop offset="99%" stop-color="hsl(356, 100%, 50%)"/>
</linearGradient>

虽然看起来很丑。

有更好的方法吗?

【问题讨论】:

    标签: svg colors linear-gradients


    【解决方案1】:

    全 hsl 渐变 0 - 360

    通过将停止颜色设置为 10%,我得到一个接近图像的形状:

    <svg height="100%" viewBox="0 0 100 20">
      <defs>
        <linearGradient id="Gradient2">
          <stop offset="0%" stop-color="hsl(0, 100%, 50%)" />
          <stop offset="10%" stop-color="hsl(36, 100%, 50%)" />
          <stop offset="20%" stop-color="hsl(72, 100%, 50%)" />
          <stop offset="30%" stop-color="hsl(108, 100%, 50%)" />
          <stop offset="40%" stop-color="hsl(144, 100%, 50%)" />
          <stop offset="50%" stop-color="hsl(180, 100%, 50%)" />
          <stop offset="60%" stop-color="hsl(252, 100%, 50%)" />
          <stop offset="70%" stop-color="hsl(236, 100%, 50%)" />
          <stop offset="80%" stop-color="hsl(288, 100%, 50%)" />
          <stop offset="90%" stop-color="hsl(324, 100%, 50%)" />
          <stop offset="100%" stop-color="hsl(360, 100%, 50%)" />
        </linearGradient>
      </defs>
      <line stroke-width="16" stroke-linecap="round" stroke="url(#Gradient2)" x1="10" y1="10" y2="10.1" x2="90" />
    </svg>

    如果你想看看如何在 1% 处停止颜色,这里是由Harry 创建的小提琴:
    Fiddle

    与 10% 的停靠点相比包含在内。

    如果您认为有很多停止颜色,那么您可以使用 javascript 添加每个停止元素。但我认为通常手动添加它们是一种更好的方法。

    【讨论】:

    • 是的,它非常接近,有 10 个停靠点(这里是 2、10 和 100 的比较 - jsfiddle.net/8q0jnu54/2 - 你失去了一点黄色的亮度,它似乎略微移动)。我想知道是否有另一种方法可以在本地进行这种插值,而不是依赖于在其他地方生成停靠点?
    【解决方案2】:

    SVG 中的颜色插值是使用 sRGB 颜色空间完成的(虽然您应该能够指定 linearRGB,但我认为它的支持并不好),所以您不能做您想做的事情 - 那些 HSL 颜色被转换为插值之前的 sRGB。

    (从技术上讲,SVG 1.1 不支持 HSL 颜色 - 所以虽然这在 Chrome 中有效,但如果它不能在任何地方都有效,请不要感到惊讶)

    【讨论】:

      【解决方案3】:

      梯度每 60 度停止一次!

      由于hsl and rgb interact 的方式,您将通过在色调上每隔 60 度设置渐变停止来获得最佳效果。添加更多站点或 其他地方,会使结果更糟。

      使用 hex、html 颜色名称、rgb 或 hsl 来定义渐变应该都在 Web 浏览器中创建相同的结果,因为它们最终都会转换为 rgb。我不确定网络浏览器之外的 hsl svg 兼容性,为了安全起见,在大多数情况下我会坚持使用 rgb 或 hex。

      <svg width=100%>
        <rect width=100%           height="1em" fill="url(#hslGradient)"/>
        <rect width=100% y="1.5em" height="1em" fill="url(#nameGradient)"/>
        <rect width=100% y="3em"   height="1em" fill="url(#rgbGradient)"/>
        <rect width=100% y="4.5em" height="1em" fill="url(#hexGradient)"/>
        <defs>
          <linearGradient id="hslGradient">
            <stop offset=0%   stop-color="hsl(  0, 100%, 50%)" />
            <stop offset=16%  stop-color="hsl( 60, 100%, 50%)" />
            <stop offset=33%  stop-color="hsl(120, 100%, 50%)" />
            <stop offset=50%  stop-color="hsl(180, 100%, 50%)" />
            <stop offset=66%  stop-color="hsl(240, 100%, 50%)" />
            <stop offset=83%  stop-color="hsl(300, 100%, 50%)" />
            <stop offset=100% stop-color="hsl(360, 100%, 50%)" />
          </linearGradient>
          <linearGradient id="nameGradient">
            <stop offset=0%   stop-color="red"    />
            <stop offset=16%  stop-color="yellow" />
            <stop offset=33%  stop-color="lime"   />
            <stop offset=50%  stop-color="cyan"   />
            <stop offset=66%  stop-color="blue"   />
            <stop offset=83%  stop-color="magenta"/>
            <stop offset=100% stop-color="red"    />
          </linearGradient>
          <linearGradient id=rgbGradient>
            <stop offset=0%   stop-color="rgb(255,  0,  0)"/>
            <stop offset=16%  stop-color="rgb(255,255,  0)"/>
            <stop offset=33%  stop-color="rgb(  0,255,  0)"/>
            <stop offset=50%  stop-color="rgb(  0,255,255)"/>
            <stop offset=66%  stop-color="rgb(  0,  0,255)"/>
            <stop offset=83%  stop-color="rgb(255,  0,255)"/>
            <stop offset=100% stop-color="rgb(255,  0,  0)"/>
          </linearGradient>
        <linearGradient id=hexGradient>
            <stop offset=0%   stop-color="#f00"/>
            <stop offset=16%  stop-color="#ff0"/>
            <stop offset=33%  stop-color="#0f0"/>
            <stop offset=50%  stop-color="#0ff"/>
            <stop offset=66%  stop-color="#00f"/>
            <stop offset=83%  stop-color="#f0f"/>
            <stop offset=100% stop-color="#f00"/>
          </linearGradient>
        </defs>
      </svg>

      如果我们谈论的是网络,我个人只会使用 css 渐变。

      html {
        background: linear-gradient(to right,
          #f00,
          #ff0,
          #0f0,
          #0ff,
          #00f,
          #f0f,
          #f00)
      }

      【讨论】:

        猜你喜欢
        • 2020-04-27
        • 1970-01-01
        • 2021-12-30
        • 2021-09-19
        • 2019-02-15
        • 1970-01-01
        • 2019-12-26
        • 2015-12-21
        • 1970-01-01
        相关资源
        最近更新 更多