weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
50 lines (44 loc) • 1.15 kB
JavaScript
/**
* @param {*} hex
* @param {*} opacity
*/
export function valueToObj(obj) {
Object.keys(obj).forEach((name) => {
if (typeof obj[name] === 'object') {
obj[name] = {
name,
raw: obj[name],
};
}
});
return obj;
}
export function hexToRgb(hex, opacity) {
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, (m, r, g, b) => r + r + g + g + b + b);
const 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
*/
export function rgba(hex, opacity) {
if (typeof hex === 'object' && 'raw' in hex) {
hex = hex.raw;
}
if (typeof opacity === 'object' && 'raw' in opacity) {
opacity = opacity.raw;
}
const 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})`;
}