UNPKG

amos-tool

Version:

amos ui tool

255 lines (244 loc) 6.07 kB
"use strict"; var utils = require("./../utils"), colorKeyWordsHexStr = require("./_keyColor"), strRGBA = function(r) { return r.a || 0 === r.a ? "rgba(" + r.r + "," + r.g + "," + r.b + "," + r.a + ")" : "rgb(" + r.r + "," + r.g + "," + r.b + ")"; }, colorRegex = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/, rgbaRegex = /(?:\(|\)|rgba?|RGBA?)*/g, toStringHexColor = function(r) { if (/^(rgba?|RGBA?)/.test(r)) { for (var e = r.replace(rgbaRegex, "").split(","), t = "#", o = 0; o < e.length; o++) if (3 === o) { var n = Number(e[o]); n < 1 && (t += n = n.toString(16).substring(2, 4)); } else { var s = Number(e[o]).toString(16); 1 === s.length && (s = "0".concat(s)), t += s; } return 7 !== t.length && 9 !== t.length && (t = r), t; } if (!colorRegex.test(r)) return r; var l = r.replace(/#/, "").split(""); if (6 === l.length) return r; if (3 === l.length) { for (var a = "#", i = 0; i < l.length; i += 1) a += l[i] + l[i]; return a; } }, toHexColor = function(r) { if (utils.isNil(r)) return ""; if (isHexColor(r)) return r; if (isKeyColor(r)) return colorKeyWordsHexStr[r]; if (!utils.isString(r)) return ""; var e = r; r.indexOf("rgb") > -1 && (e = e.replace(rgbaRegex, "")); for (var t = e.split(","), o = "#", n = 0; n < t.length; n++) if (n < 3) { var s = Number(t[n]).toString(16); 1 === s.length && (s = "0".concat(s)), o += s; } return o.length > 7 && (o = r), o; }, toRGBcolor = function(r) { var e = r.toLowerCase(); if (e && colorRegex.test(e)) { if (4 === e.length) { for (var t = "#", o = 1; o < 4; o += 1) t += e.slice(o, o + 1).concat(e.slice(o, o + 1)); e = t; } for (var n = [], s = 1; s < 7; s += 2) n.push(parseInt("0x" + e.slice(s, s + 2))); return "rgb(" + n.join(",") + ")"; } return e; }, transformColor = function(r) { var e = /#([a-zA-Z0-9]{6})/.exec(r), t = "rgb("; if (e && 2 == e.length) { var o = e[1]; t += parseInt("0x" + o.substr(0, 2)) + ",", t += parseInt("0x" + o.substr(2, 2)) + ",", t += parseInt("0x" + o.substr(4, 2)), t += ")"; } else t = "rgb(147, 147, 147)"; return t; }; function isRgbColor(r) { var e = r.replace(rgbaRegex, "").split(","); return !/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/.test(r) && e.length >= 3; } function isHexColor(r) { return /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/.test(r); } function isKeyColor(r) { return !!colorKeyWordsHexStr[r]; } function keyColorToHex(r) { return colorKeyWordsHexStr[r] || r; } function rgb2object(r) { if (isRgbColor(r)) { var e = r.replace(rgbaRegex, "").split(","); if (3 == e.length) return { r: e[0], g: e[1], b: e[2], a: "1" }; if (e.length > 3) return { r: e[0], g: e[1], b: e[2], a: e[3] || "1" }; } return { r: "0", g: "0", b: "0", a: "1" }; } function objRGB2str(r) { return utils.isObject(r) && utils.isKeyInObject([ "r", "g", "b" ], r) ? strRGBA(r) : strRGBA({ r: 0, g: 0, b: 0 }); } function gradientColor(r, e, t) { var o = function(r) { var e = /rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/.exec(r), t = /rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*/.exec(r), o = /#([a-zA-Z0-9]{6})/.exec(r), n = []; if (e && 4 == e.length || t && 4 == t.length) n = e && 4 == e.length ? e : t; else { if (!o || 2 != o.length) return null; var s = o[1]; n.push(""), n.push(parseInt("0x" + s.substr(0, 2))), n.push(parseInt("0x" + s.substr(2, 2))), n.push(parseInt("0x" + s.substr(4, 2))); } return { r: Number(n[1]), g: Number(n[2]), b: Number(n[3]) }; }, n = o(r.from), s = o(r.to), l = function(r, e, t) { return Math.round(r + (e - r) * t / 100); }, a = function(r, e) { return null == e || e < 0 || "number" != typeof e ? r : (e = Math.round(e), Math.round(r * Math.pow(10, e)) / Math.pow(10, e)); }; return function(r) { var o = t == e ? 0 : (r.value - e) / (t - e), i = l(n.r, s.r, 100 * a(o, 2)), u = l(n.g, s.g, 100 * a(o, 2)), g = l(n.b, s.b, 100 * a(o, 2)); return strRGBA({ r: i, g: u, b: g }); }; } var alphaHexMap = { "1.00": "FF", .99: "FC", .98: "FA", .97: "F7", .96: "F5", .95: "F2", .94: "F0", .93: "ED", .92: "EB", .91: "E8", "0.90": "E6", .89: "E3", .88: "E0", .87: "DE", .86: "DB", .85: "D9", .84: "D6", .83: "D4", .82: "D1", .81: "CF", "0.80": "CC", .79: "C9", .78: "C7", .77: "C4", .76: "C2", .75: "BF", .74: "BD", .73: "BA", .72: "B8", .71: "B5", "0.70": "B3", .69: "B0", .68: "AD", .67: "AB", .66: "A8", .65: "A6", .64: "A3", .63: "A1", .62: "9E", .61: "9C", "0.60": "99", .59: "96", .58: "94", .57: "91", .56: "8F", .55: "8C", .54: "8A", .53: "87", .52: "85", .51: "82", "0.50": "80", .49: "7D", .48: "7A", .47: "78", .46: "75", .45: "73", .44: "70", .43: "6E", .42: "6B", .41: "69", "0.40": "66", .39: "63", .38: "61", .37: "5E", .36: "5C", .35: "59", .34: "57", .33: "54", .32: "52", .31: "4F", "0.30": "4D", .29: "4A", .28: "47", .27: "45", .26: "42", .25: "40", .24: "3D", .23: "3B", .22: "38", .21: "36", "0.20": "33", .19: "30", .18: "2E", .17: "2B", .16: "29", .15: "26", .14: "24", .13: "21", .12: "1F", .11: "1C", "0.10": "1A", .09: "17", .08: "14", .07: "12", .06: "0F", .05: "0D", .04: "0A", .03: "08", .02: "05", .01: "03", "0.00": "00" }; function toHexAlphaColor(r, e) { return 1 === e ? r : void 0 !== e && e < 1 ? "".concat(r).concat(alphaHexMap[e.toFixed(2)]).toLowerCase() : r; } module.exports = { toStringHexColor: toStringHexColor, toHexColor: toHexColor, toRGBcolor: toRGBcolor, transformColor: transformColor, isHexColor: isHexColor, isKeyColor: isKeyColor, isRgbColor: isRgbColor, rgb2object: rgb2object, objRGB2str: objRGB2str, keyColorToHex: keyColorToHex, gradientColor: gradientColor, toHexAlphaColor: toHexAlphaColor };