css:echarts渐变色转换为css渐变色

通过一个下拉框来选择渐变类型,为了简化,我设置了三种:水平方向的渐变、垂直方向的渐变和径向渐变用,表格来配置echarts渐变色的百分比位置和颜色。

 config是表格里的数据格式如下:

offset是百分比位置,color是对应位置的颜色,new graphic前面四个参数表示渐变的方向,我这里默认是从上到下,从左到右,想要反过来的话,直接把颜色反过来就行了。

let config = ref([
  { offset: 0, color: '#e9a90a' },
  { offset: 0.5, color: '#0bcfcf' },
  { offset: 1, color: '#cc0ce6' }
])
if (tempGradualType.value === 'linearY') {
      // 垂直方向的线性渐变
      temp = new graphic.LinearGradient(
        0, 0, 0, 1, JSON.parse(JSON.stringify(config.value))
      )
    }
    else if (tempGradualType.value === 'linearX') {
      // 水平方向的线性渐变
      temp = new graphic.LinearGradient(
        0, 0, 1, 0, JSON.parse(JSON.stringify(config.value))
      )
    } else {
      // 径向渐变
      temp = new graphic.RadialGradient(
        radial.value.x, radial.value.y, radial.value.r, JSON.parse(JSON.stringify(config.value))
      )
    }

最后生成的echarts颜色是这样的,type表示线性渐变还是径向渐变,colorStops就是刚才在表格里设置的数组。

现在我们还要把echarts的渐变色格式换成css的格式,因为要做下图这样的颜色指示块。

这种颜色指示块,其实就是给一个div设置渐变色背景。 

 <div v-else style="width: 100%;height: 100%" @click="showGradualDialog(item, index)"
          :style="{ backgroundImage: getBackgroundImage(item) }">

 css里线性渐变从上到下,是to bottom,从左到右,是to right,把颜色数组像['#000','#111','#222']放进去即可,不需要设置百分比位置。径向渐变需要设置起点坐标,从echarts颜色的xy取值即可。

const getBackgroundImage = (item) => {
  const data = JSON.parse(JSON.stringify(item))
  let bgc = ''
  const colors = data.colorStops.map((config) => {
    return config.color
  }).join(',')
  if (data.type === 'linear') {
    if (data.x2 === 0 && item.y2 === 1) {
      // 从上到下:0,0,0,1
      bgc = 'linear-gradient(to bottom,' + colors + ')'
    } else if (data.x2 === 1 && data.y2 === 0) {
      // 从左到右:0,0,1,0
      bgc = 'linear-gradient(to right,' + colors + ')'
    }
  } else {
    // 径向渐变
    bgc = 'radial-gradient(circle at ' + data.x + ' ' + data.y + ',' + colors + ')'
  }
  return bgc
}

最终效果:

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用 Python 编写一个函数来将 CSS 渐变色转换成 QSS 渐变色。下面是一个 Python 函数的示例代码: ```python def css_gradient_to_qss(css_gradient): """ Convert CSS gradient to QSS gradient """ qss_gradient = "" gradient_type = css_gradient.split("(")[0].strip() # get gradient type if gradient_type == "linear-gradient": qss_gradient = "qlineargradient(" gradient_direction = css_gradient.split("(")[1].split(")")[0].strip() # get gradient direction if gradient_direction == "to top": qss_gradient += "x1: 0, y1: 1, x2: 0, y2: 0" elif gradient_direction == "to right": qss_gradient += "x1: 0, y1: 0, x2: 1, y2: 0" elif gradient_direction == "to bottom": qss_gradient += "x1: 0, y1: 0, x2: 0, y2: 1" elif gradient_direction == "to left": qss_gradient += "x1: 1, y1: 0, x2: 0, y2: 0" else: # unsupported direction return "" gradient_colors = css_gradient.split("(")[1].split(")")[1].split(",") # get gradient colors for i, color in enumerate(gradient_colors): color = color.strip() if color.startswith("#"): # hex color qss_gradient += ", stop: %s %s" % (i / (len(gradient_colors) - 1), color) else: # named color qss_gradient += ", stop: %s %s" % (i / (len(gradient_colors) - 1), colorname_to_hex(color)) elif gradient_type == "radial-gradient": qss_gradient = "qradialgradient(" gradient_shape = css_gradient.split("(")[1].split(")")[0].split()[0].strip() # get gradient shape if gradient_shape == "circle": qss_gradient += "cx: 0.5, cy: 0.5, radius: 0.5, fx: 0.5, fy: 0.5" elif gradient_shape == "ellipse": qss_gradient += "cx: 0.5, cy: 0.5, radius: 0.5, fx: 0.75, fy: 0.25" else: # unsupported shape return "" gradient_colors = css_gradient.split("(")[1].split(")")[1].split(",") # get gradient colors for i, color in enumerate(gradient_colors): color = color.strip() if color.startswith("#"): # hex color qss_gradient += ", stop: %s %s" % (i / (len(gradient_colors) - 1), color) else: # named color qss_gradient += ", stop: %s %s" % (i / (len(gradient_colors) - 1), colorname_to_hex(color)) else: # unsupported gradient type return "" qss_gradient += ")" return qss_gradient ``` 这个函数可以将 CSS 渐变色转换成 QSS 渐变色,支持线性渐变和径向渐变,支持颜色名称和十六进制颜色值。需要注意的是,该函数中的 `colorname_to_hex(color)` 函数需要自行编写,用于将颜色名称转换成十六进制颜色值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值