UNPKG

@iimm/shared

Version:

shared utils on browser and react env

265 lines (263 loc) 9.44 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/cjs/html/color/convert/index.ts var convert_exports = {}; __export(convert_exports, { colorToRGBObjColor: () => colorToRGBObjColor, hslToRgb: () => hslToRgb, hsvToRgb: () => hsvToRgb, rgbToHex: () => rgbToHex, rgbToHsv: () => rgbToHsv, rgbaToArgbHex: () => rgbaToArgbHex, rgbaToHex: () => rgbaToHex, stringColorToObjectColor: () => stringColorToObjectColor }); module.exports = __toCommonJS(convert_exports); var import_defines = require("../defines"); var import_numericalHandler = require("./numericalHandler"); var stringColorToObjectColor = (color = "") => { color = color.trim().toLowerCase(); let named = false; if (import_defines.ColorNameHexs[color]) { color = import_defines.ColorNameHexs[color]; named = true; } else if (color === "transparent") { return { r: 0, g: 0, b: 0, a: 0, format: "name", type: "rgba" }; } let match; if (match = import_defines.ColorMatchers.rgb.exec(color)) { return { r: match[1], g: match[2], b: match[3], type: "rgb" }; } if (match = import_defines.ColorMatchers.rgba.exec(color)) { return { r: match[1], g: match[2], b: match[3], a: match[4], type: "rgba" }; } if (match = import_defines.ColorMatchers.hsl.exec(color)) { return { h: match[1], s: match[2], l: match[3], type: "hsl" }; } if (match = import_defines.ColorMatchers.hsla.exec(color)) { return { h: match[1], s: match[2], l: match[3], a: match[4], type: "hsla" }; } if (match = import_defines.ColorMatchers.hsv.exec(color)) { return { h: match[1], s: match[2], v: match[3], type: "hsv" }; } if (match = import_defines.ColorMatchers.hsva.exec(color)) { return { h: match[1], s: match[2], v: match[3], a: match[4], type: "hsva" }; } if (match = import_defines.ColorMatchers.hex8.exec(color)) { return { r: (0, import_numericalHandler.parseIntFromHex)(match[1]), g: (0, import_numericalHandler.parseIntFromHex)(match[2]), b: (0, import_numericalHandler.parseIntFromHex)(match[3]), a: (0, import_numericalHandler.convertHexToDecimal)(match[4]), format: named ? "name" : "hex8", type: "rgba" }; } if (match = import_defines.ColorMatchers.hex6.exec(color)) { return { r: (0, import_numericalHandler.parseIntFromHex)(match[1]), g: (0, import_numericalHandler.parseIntFromHex)(match[2]), b: (0, import_numericalHandler.parseIntFromHex)(match[3]), format: named ? "name" : "hex", type: "rgb" }; } if (match = import_defines.ColorMatchers.hex4.exec(color)) { return { r: (0, import_numericalHandler.parseIntFromHex)(match[1] + "" + match[1]), g: (0, import_numericalHandler.parseIntFromHex)(match[2] + "" + match[2]), b: (0, import_numericalHandler.parseIntFromHex)(match[3] + "" + match[3]), a: (0, import_numericalHandler.convertHexToDecimal)(match[4] + "" + match[4]), format: named ? "name" : "hex8", type: "rgba" }; } if (match = import_defines.ColorMatchers.hex3.exec(color)) { return { r: (0, import_numericalHandler.parseIntFromHex)(match[1] + "" + match[1]), g: (0, import_numericalHandler.parseIntFromHex)(match[2] + "" + match[2]), b: (0, import_numericalHandler.parseIntFromHex)(match[3] + "" + match[3]), format: named ? "name" : "hex", type: "rgb" }; } return false; }; var hslToRgb = (h, s, l) => { let r, g, b; h = (0, import_numericalHandler.bound01)(h, 360); s = (0, import_numericalHandler.bound01)(s, 100); l = (0, import_numericalHandler.bound01)(l, 100); function hue2rgb(p, q, t) { if (t < 0) t += 1; if (t > 1) t -= 1; if (t < 1 / 6) return p + (q - p) * 6 * t; if (t < 1 / 2) return q; if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; return p; } if (s === 0) { r = g = b = l; } else { const q = l < 0.5 ? l * (1 + s) : l + s - l * s; const p = 2 * l - q; r = hue2rgb(p, q, h + 1 / 3); g = hue2rgb(p, q, h); b = hue2rgb(p, q, h - 1 / 3); } return { r: r * 255, g: g * 255, b: b * 255 }; }; var rgbToHsv = (r, g, b) => { r = (0, import_numericalHandler.bound01)(r, 255); g = (0, import_numericalHandler.bound01)(g, 255); b = (0, import_numericalHandler.bound01)(b, 255); const max = Math.max(r, g, b), min = Math.min(r, g, b); let h; const v = max; const d = max - min; const s = max === 0 ? 0 : d / max; if (max === min) { h = 0; } else { switch (max) { case r: h = (g - b) / d + (g < b ? 6 : 0); break; case g: h = (b - r) / d + 2; break; case b: h = (r - g) / d + 4; break; } h /= 6; } return { h, s, v }; }; var hsvToRgb = (h, s, v) => { h = (0, import_numericalHandler.bound01)(h, 360) * 6; s = (0, import_numericalHandler.bound01)(s, 100); v = (0, import_numericalHandler.bound01)(v, 100); const i = Math.floor(h), f = h - i, p = v * (1 - s), q = v * (1 - f * s), t = v * (1 - (1 - f) * s), mod = i % 6, r = [v, q, p, p, t, v][mod], g = [t, v, v, q, p, p][mod], b = [p, p, t, v, v, q][mod]; return { r: r * 255, g: g * 255, b: b * 255 }; }; var rgbToHex = (r, g, b, allow3Char) => { const hex = [ (0, import_numericalHandler.pad2)(Math.round(+r).toString(16)), (0, import_numericalHandler.pad2)(Math.round(+g).toString(16)), (0, import_numericalHandler.pad2)(Math.round(+b).toString(16)) ]; if (allow3Char && hex[0].charAt(0) === hex[0].charAt(1) && hex[1].charAt(0) === hex[1].charAt(1) && hex[2].charAt(0) === hex[2].charAt(1)) { return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0); } return hex.join(""); }; var rgbaToHex = (r, g, b, a, allow4Char) => { const hex = [ (0, import_numericalHandler.pad2)(Math.round(+r).toString(16)), (0, import_numericalHandler.pad2)(Math.round(+g).toString(16)), (0, import_numericalHandler.pad2)(Math.round(+b).toString(16)), (0, import_numericalHandler.pad2)((0, import_numericalHandler.convertDecimalToHex)(a)) ]; if (allow4Char && hex[0].charAt(0) === hex[0].charAt(1) && hex[1].charAt(0) === hex[1].charAt(1) && hex[2].charAt(0) === hex[2].charAt(1) && hex[3].charAt(0) === hex[3].charAt(1)) { return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0) + hex[3].charAt(0); } return hex.join(""); }; var rgbaToArgbHex = (r, g, b, a) => { const hex = [ (0, import_numericalHandler.pad2)((0, import_numericalHandler.convertDecimalToHex)(a)), (0, import_numericalHandler.pad2)(Math.round(+r).toString(16)), (0, import_numericalHandler.pad2)(Math.round(+g).toString(16)), (0, import_numericalHandler.pad2)(Math.round(+b).toString(16)) ]; return hex.join(""); }; var isValidCSSUnit = (color) => !!import_defines.ColorMatchers.CSS_UNIT.exec(color); function rgbToRgb(r, g, b) { return { r: (0, import_numericalHandler.bound01)(r, 255) * 255, g: (0, import_numericalHandler.bound01)(g, 255) * 255, b: (0, import_numericalHandler.bound01)(b, 255) * 255 }; } function convertToPercentage(n) { if (n <= 1) { n = n * 100 + "%"; } return n; } var colorToRGBObjColor = (color) => { let rgb = { r: 0, g: 0, b: 0 }; let a = 1; let s = null; let v = null; let l = null; let ok = false; let format = false; if (typeof color === "string") { color = stringColorToObjectColor(color); } if (typeof color === "object") { if (isValidCSSUnit(color.r) && isValidCSSUnit(color.g) && isValidCSSUnit(color.b)) { rgb = rgbToRgb(color.r, color.g, color.b); ok = true; format = String(color.r).slice(-1) === "%" ? "prgb" : "rgb"; } else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.v)) { s = convertToPercentage(color.s); v = convertToPercentage(color.v); rgb = hsvToRgb(color.h, s, v); ok = true; format = "hsv"; } else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.l)) { s = convertToPercentage(color.s); l = convertToPercentage(color.l); rgb = hslToRgb(color.h, s, l); ok = true; format = "hsl"; } if (color.hasOwnProperty("a")) { a = color.a; } } a = (0, import_numericalHandler.boundAlpha)(a); return { ok, format: color.format || format, r: Math.min(255, Math.max(rgb.r, 0)), g: Math.min(255, Math.max(rgb.g, 0)), b: Math.min(255, Math.max(rgb.b, 0)), a }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { colorToRGBObjColor, hslToRgb, hsvToRgb, rgbToHex, rgbToHsv, rgbaToArgbHex, rgbaToHex, stringColorToObjectColor });