UNPKG

weex-nuke

Version:

基于 Rax 、Weex 的高性能组件体系 ~~

62 lines (54 loc) 1.71 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.valueToObj = valueToObj; exports.hexToRgb = hexToRgb; exports.rgba = rgba; /** * @param {*} hex * @param {*} opacity */ function valueToObj(obj) { Object.keys(obj).forEach(function (name) { if (_typeof(obj[name]) === 'object') { obj[name] = { name: name, raw: obj[name] }; } }); return obj; } function hexToRgb(hex, opacity) { var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; hex = hex.replace(shorthandRegex, function (m, r, g, b) { return r + r + g + g + b + b; }); var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); if (!result) return null; return { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) }; } /** * @param {*} hex * @param {*} opacity */ function rgba(hex, opacity) { if ((typeof hex === 'undefined' ? 'undefined' : _typeof(hex)) === 'object' && 'raw' in hex) { hex = hex.raw; } if ((typeof opacity === 'undefined' ? 'undefined' : _typeof(opacity)) === 'object' && 'raw' in opacity) { opacity = opacity.raw; } var result = exports.hexToRgb(hex); if (!result) return null; // if ('raw' in hex) { // return { raw: `rgba(${result.r},${result.g},${result.b},${opacity})` }; // } return 'rgba(' + result.r + ',' + result.g + ',' + result.b + ',' + opacity + ')'; }