import DataUtils from "./DataUtils";

/**
 * 颜色工具类
 */
const ColorUtils = {
  /**
   * 16进制颜色转RGB
   * @param {String} hex -16进制颜色字符串
   */
  toRgb(hex) {
  },

  /**
   * RGB转16进制颜色
   * @param {String} RGB -RGB颜色字符串
   */
  toHex(rgb) {
  },

  /**
   * 16进制颜色转数组
   * @param {String} hex -16进制颜色字符串
   */
  hexToArray(hex) {
    let color = hex.replace(/\s/g, '');

    //#rrggbbaa
    let rrggbbaaMatcher = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i,

    matches = rrggbbaaMatcher.exec(color);
    if (matches !== null) {
      let red = parseInt(matches[1], 16);
      let green = parseInt(matches[2], 16);
      let blue = parseInt(matches[3], 16);
      // 透明度
      if (matches.length > 3) {
        let alpha = parseInt(DataUtils.default(matches[4], 'f'), 16) / 15.0;

        return [red, green, blue, alpha];
      }

      return [red, green, blue];
    }
  }
};

export default ColorUtils;
