UNPKG

@meleon/uni-ui

Version:

A uniapp components library written in vue3 and typescript

1 lines 1.65 kB
import{colord,extend}from"colord";import namesPlugin from"colord/plugins/names";import mixPlugin from"colord/plugins/mix";extend([namesPlugin,mixPlugin]);const hueStep=2,saturationStep=16,saturationStep2=5,brightnessStep1=5,brightnessStep2=15,lightColorCount=5,darkColorCount=4;export function getColorPalette(o,t){const r=colord(o);if(!r.isValid())throw Error("invalid input color value");if(6===t)return colord(r).toHex();const e=t<6,n=r.toHsv(),i=e?6-t:t-5-1,l={h:getHue(n,i,e),s:getSaturation(n,i,e),v:getValue(n,i,e)};return colord(l).toHex()}const darkColorMap=[{index:7,opacity:.15},{index:6,opacity:.25},{index:5,opacity:.3},{index:5,opacity:.45},{index:5,opacity:.65},{index:5,opacity:.85},{index:4,opacity:.9},{index:3,opacity:.95},{index:2,opacity:.97},{index:1,opacity:.98}];export function getColorPalettes(o,t=!1,r="#141414"){const e=[1,2,3,4,5,6,7,8,9,10].map((t=>getColorPalette(o,t)));if(t){return darkColorMap.map((({index:o,opacity:t})=>colord(r).mix(e[o],t))).map((o=>colord(o).toHex()))}return e}function getHue(o,t,r){let e;const n=Math.round(o.h);return e=n>=60&&n<=240?r?n-2*t:n+2*t:r?n+2*t:n-2*t,e<0&&(e+=360),e>=360&&(e-=360),e}function getSaturation(o,t,r){if(0===o.h&&0===o.s)return o.s;let e;return e=r?o.s-16*t:4===t?o.s+16:o.s+5*t,e>100&&(e=100),r&&5===t&&e>10&&(e=10),e<6&&(e=6),e}function getValue(o,t,r){let e;return e=r?o.v+5*t:o.v-15*t,e>100&&(e=100),e}export function addColorAlpha(o,t){return colord(o).alpha(t).toHex()}export function mixColor(o,t,r){return colord(o).mix(t,r).toHex()}export function isWhiteColor(o){return colord(o).isEqual("#ffffff")}export function getRgbOfColor(o){return colord(o).toRgb()}