UNPKG

iking-utils-pro

Version:

金合前端工具库

2 lines (1 loc) 2.37 kB
"use strict";function t(t){return/^#([0-9a-fA-F]{3}|[0-9a-fA-f]{6})$/.test(t)}function r(r){let n=r.toLowerCase();if(t(r)){if(4===n.length){let t="#";for(let r=1;r<4;r+=1)t+=n.slice(r,r+1).concat(n.slice(r,r+1));n=t}const t=[];for(let r=1;r<7;r+=2)t.push(parseInt("0x"+n.slice(r,r+2)));return"RGB("+t.join(",")+")"}return n}function n(t,r){const n=parseInt(t,16)+r,e=n>255?255:n;return e.toString(16).length>1?e.toString(16):`0${e.toString(16)}`}function e(t,r,n){const e=[t,r,n].map((t=>(t/=255)<=.03928?t/12.92:Math.pow((t+.055)/1.055,2.4)));return.2126*e[0]+.7152*e[1]+.0722*e[2]}function s(t,r){const n=parseInt(t,16)-r,e=n<0?0:n;return e.toString(16).length>1?e.toString(16):`0${e.toString(16)}`}Object.defineProperty(exports,"__esModule",{value:!0}),exports.calculateBestTextColor=function(t){const n=r(t.substring(1));var s,o;return(s=n.split(","),o=[0,0,0],(e(~~s[0],~~s[1],~~s[2])+.05)/(e(o[0],o[1],o[2])+.05))>=12?"#000000":"#FFFFFF"},exports.colorIsDark=function(n){if(!t(n))return;const[e,s,o]=r(n).replace(/(?:\(|\)|rgb|RGB)*/g,"").split(",").map((t=>Number(t)));return.299*e+.578*s+.114*o<192},exports.darken=function(t,r){return t=t.indexOf("#")>=0?t.substring(1,t.length):t,r=Math.trunc(255*r/100),`#${s(t.substring(0,2),r)}${s(t.substring(2,4),r)}${s(t.substring(4,6),r)}`},exports.hexToRGB=r,exports.hexToRGBA=function(r,n){let e=r.toLowerCase();if(t(r)){if(4===e.length){let t="#";for(let r=1;r<4;r+=1)t+=e.slice(r,r+1).concat(e.slice(r,r+1));e=t}const t=[];for(let r=1;r<7;r+=2)t.push(parseInt("0x"+e.slice(r,r+2)));return`RGBA(${t.join(",")},${n})`}return e},exports.isHexColor=t,exports.lighten=function(t,r){return t=t.indexOf("#")>=0?t.substring(1,t.length):t,r=Math.trunc(255*r/100),`#${n(t.substring(0,2),r)}${n(t.substring(2,4),r)}${n(t.substring(4,6),r)}`},exports.rgbToHex=function(t,r,n){const e=(t<<16|r<<8|n).toString(16);return"#"+new Array(Math.abs(e.length-7)).join("0")+e},exports.setOpacity=function(t,r=1){const n=t.toLocaleUpperCase();if(/^#[0-9A-F]{6}$/i.test(n)){let t=n.slice(1);return`rgba(${parseInt(t.substring(0,2),16)}, ${parseInt(t.substring(2,4),16)}, ${parseInt(t.substring(4,6),16)}, ${r})`}if(/^rgb\(\d{1,3}, \d{1,3}, \d{1,3}\)$/i.test(n)){let t=n.match(/\d+/g)?.map(Number)||[];return`rgba(${t[0]}, ${t[1]}, ${t[2]}, ${r})`}throw new Error("Invalid color format. Only hexadecimal and RGB formats are supported.")};