用法:
<template>
<view>
<uv-pick-color ref="pickerColor" :color="color" @confirm="confirm"></uv-pick-color>
<button @click="openColor">打开</button>
</view>
</template>
<script>
export default {
data() {
return {
color: { r: 60, g: 156, b: 255, a: 1 }
}
},
methods: {
openColor() {
this.$refs.pickerColor.open();
},
confirm(e) {
// 返回的e= {rgba: {r: 140,g: 24,b: 24,a: 0.6},hex: "#8c1818"}
// 返回的hex值没有透明度
console.log('confirm', e)
}
}
}
</script>
解决:
修改uv-picker-color的源码,源码中只提供了rgb 转 二进制 hex,没有rgba转hex的,所以我们增加rgba 转 二进制 hex函数
修改文件位置:uv-pick-color/components/uv-pick-color/color.js
/**
* rgba 转 二进制 hex
* @param {Object} rgba
*/
export function rgbaToHex(rgba) {
let hex = [rgba.r.toString(16), rgba.g.toString(16), rgba.b.toString(16)];
hex.map(function(str, i) {
if (str.length == 1) {
hex[i] = '0' + str;
}
});
let a = rgba.a === 0 ? '0'+rgba.a : rgba.a.toString(16).substring(2,4)
if(a.length <=1){
a = a + '0'
}
if(rgba.a !==1 ){
hex.push(a)
}
return hex.join('');
}
并在uv-pick-color/components/uv-pick-color/uv-pick-color.vue中使用rgbaToHex函数
import { rgbToHsb, hsbToRgb, rgbToHex, colorList, rgbaToHex } from './colors.js'
并修改
/**
* 设置二进制颜色
* @param {Object} rgb
*/
setValue(rgb) {
// this.hex = `#${(rgbToHex(rgb))}`;
//用rgbaToHex替换调原来的rgbToHex
//这样confirm时返回的e.hex就是8位数的带透明度的值啦
this.hex = `#${(rgbaToHex(rgb))}`;
},