UNPKG

mine-h5-ui

Version:

一款轻量级、模块化基于 Vue3.x 的 H5 前端 UI 组件库 👍

80 lines (79 loc) 2.17 kB
import { colorMode as b } from "./enum.js"; /*! * mine-h5-ui v2.15.0 * Copyright (c) 2025 biaov * @license MIT */ const w = ([r, o, t]) => { r = r / 360 * 6, o = o / 100, t = t / 100; const s = Math.floor(r), e = r - s, n = t * (1 - o), a = t * (1 - e * o), c = t * (1 - (1 - e) * o), h = s % 6, f = [t, a, n, n, c, t][h], d = [c, t, t, a, n, n][h], m = [n, n, c, t, t, a][h]; return [Math.round(f * 255), Math.round(d * 255), Math.round(m * 255)]; }, u = ([r, o, t]) => { r = +r / 255, o = +o / 255, t = +t / 255; let s, e, n; const a = Math.min(r, o, t); n = Math.max(r, o, t); const c = n, h = c - a; if (c == a) s = 0; else { switch (c) { case r: s = (o - t) / h + (o < t ? 6 : 0); break; case o: s = 2 + (t - r) / h; break; case t: s = 4 + (r - o) / h; break; } s = Math.round(s * 60); } return c == 0 ? e = 0 : e = 1 - a / c, e = Math.round(e * 100), n = Math.round(n * 100), [s, e, n]; }, $ = (r) => r.slice(1).match(/../g).map((o) => parseInt(o, 16)), M = (r) => `${r.reduce((o, t) => o + t.toString(16).padStart(2, "0"), "#")}`, i = (r, o = !1) => { const t = r.match(/\d+/g); if (!t) throw new Error(`${r} 数值错误`); return o ? t.map(Number) : t; }, l = (r, [o, t]) => { if (!(o && t) || o === t) return r; let s, e; switch (o) { case b.hex: if (!/^#[0-9a-zA-Z]{6}$/.test(`${r}`)) throw new Error(`${r} 颜色值错误或不全`); e = $(r); break; case b.rgb: e = i(r, !0); break; case b.hsb: { const n = i(r, !0); if (!n) throw new Error(`${r} 不是一个 hsb 格式`); e = w(n); } break; default: throw new Error(`${o} 色彩模式错误`); } switch (t) { case b.hex: s = M(e); break; case b.rgb: s = `rgb(${e.join()})`; break; case b.hsb: { const [n, a, c] = u(e); s = `hsb(${n},${a}%,${c}%)`; } break; default: throw new Error(`${t} 色彩模式错误`); } return s; }; export { l as useColorTransform };