add color plugins to tran color

This commit is contained in:
jiaming 2018-12-10 18:22:40 +08:00
parent 4162cc0475
commit 8d00b53c15
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,53 @@
const hexReg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
const rgbReg = /^(rgb|rgba|RGB|RGBA)/
/**
* @description hex color to rgb / rgba color
* @param {string} rgba opacity
* @return {string} rgb / rgba color
*/
export function hexToRgb (hex, opacity) {
if (!hex || !hexReg.test(hex)) return false
hex = hex.toLowerCase().replace('#', '')
// deal 3bit hex color to 6bit hex color
hex.length === 3 && (hex = Array.from(hex).map(hexNum => hexNum + hexNum).join())
let rgb = []
for (let i = 0; i < 6; i += 2) {
rgb.push(parseInt(`0x${hex.slice(i, i + 2)}`))
}
rgb = rgb.join(',')
if (opacity) {
return `rgba(${rgb}, ${opacity})`
} else {
return `rgb(${rgb})`
}
}
/**
* @description rgb / rgba color to hex color
* @return {string} hex color
*/
export function rgbToHex (rgb) {
if (!rgb || !rgbReg.test(rgb)) return false
rgb = rgb.toLowerCase().replace(/rgb\(|rgba\(|\)/g, '').split(',').slice(0, 3)
const hex = rgb.map((rgbNum) => Number(rgbNum).toString(16)).map((rgbNum) => rgbNum === '0' ? rgbNum + rgbNum : rgbNum).join('')
return `#${hex}`
}
const color = {
hexToRgb,
rgbToHex
}
export default function (Vue) {
Vue.prototype.color = color
}

View File

@ -2,10 +2,13 @@ import methodsExtend from './methodsExtend'
import canvasExtend from './canvasExtend'
import colorExtend from './colorExtend'
import axiosExtend from './axiosExtend'
export default function (Vue) {
methodsExtend(Vue)
canvasExtend(Vue)
colorExtend(Vue)
axiosExtend(Vue)
}