@vtrbo/utils-color
Advanced tools
| "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/color.ts | ||
| var _utilsis = require('@vtrbo/utils-is'); | ||
| function rgbaToHex(rgba) { | ||
| if (_utilsis.isColor.call(void 0, rgba, "HEX")) | ||
| return rgba; | ||
| if (!_utilsis.isColor.call(void 0, rgba, "RGB") && !_utilsis.isColor.call(void 0, rgba, "RGBA")) | ||
| return ""; | ||
| const rgbaValue = rgba.replace("rgba(", "").replace("rgb(", "").replace(")", ""); | ||
| const [r, g, b, a] = rgbaValue.split(",").map((m) => +m); | ||
| if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) | ||
| return `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}${a || a === 0 ? (a * 255 | 1 << 8).toString(16).slice(1) : ""}`; | ||
| return ""; | ||
| } | ||
| function hexToRgba(hex) { | ||
| if (_utilsis.isColor.call(void 0, hex, "RGB") || _utilsis.isColor.call(void 0, hex, "RGBA")) | ||
| return hex; | ||
| if (!_utilsis.isColor.call(void 0, hex, "HEX")) | ||
| return ""; | ||
| const getSingle = (start, end) => Number.parseInt(`0x${hex.slice(start, end)}${hex.slice(start, end)}`); | ||
| const getDouble = (start, end) => Number.parseInt(`0x${hex.slice(start, end)}`); | ||
| const getAlpha = (start, end, fn) => Math.round(fn(start, end) / 255 * 100) / 100; | ||
| const hexMap = { | ||
| 4: `rgb(${getSingle(1, 2)}, ${getSingle(2, 3)}, ${getSingle(3, 4)})`, | ||
| 5: `rgba(${getSingle(1, 2)}, ${getSingle(2, 3)}, ${getSingle(3, 4)}, ${getAlpha(4, 5, getSingle)})`, | ||
| 7: `rgb(${getDouble(1, 3)}, ${getDouble(3, 5)}, ${getDouble(5, 7)})`, | ||
| 9: `rgba(${getDouble(1, 3)}, ${getDouble(3, 5)}, ${getDouble(5, 7)}, ${getAlpha(7, 9, getDouble)})` | ||
| }; | ||
| return hexMap[hex.length] || ""; | ||
| } | ||
| function lighten(color, level = 10) { | ||
| if (!_utilsis.isColor.call(void 0, color, "HEX") && !_utilsis.isColor.call(void 0, color, "RGB") && !_utilsis.isColor.call(void 0, color, "RGBA")) | ||
| return ""; | ||
| let rgbaColor = ""; | ||
| let rgba = []; | ||
| let type = "RGBA"; | ||
| if (_utilsis.isColor.call(void 0, color, "HEX")) { | ||
| rgbaColor = hexToRgba(color); | ||
| type = "HEX"; | ||
| } else { | ||
| rgbaColor = color; | ||
| type = _utilsis.isColor.call(void 0, color, "RGBA") ? "RGBA" : "RGB"; | ||
| } | ||
| const rgbaValue = rgbaColor.replace("rgba(", "").replace("rgb(", "").replace(")", ""); | ||
| rgba = rgbaValue.split(",").map((m) => +m); | ||
| for (let i = 0; i < 3; i++) | ||
| rgba[i] = Math.floor((255 - rgba[i]) * level / 10 + rgba[i]); | ||
| const typeMap = { | ||
| HEX: "", | ||
| RGB: `rgb(${rgba.join(", ")})`, | ||
| RGBA: `rgba(${rgba.join(", ")})` | ||
| }; | ||
| const lightenColor = typeMap[type]; | ||
| return lightenColor || rgbaToHex(rgba.length === 3 ? typeMap.RGB : typeMap.RGBA); | ||
| } | ||
| function darken(color, level = 0) { | ||
| if (!_utilsis.isColor.call(void 0, color, "HEX") && !_utilsis.isColor.call(void 0, color, "RGB") && !_utilsis.isColor.call(void 0, color, "RGBA")) | ||
| return ""; | ||
| let rgbaColor = ""; | ||
| let rgba = []; | ||
| let type = "RGBA"; | ||
| if (_utilsis.isColor.call(void 0, color, "HEX")) { | ||
| rgbaColor = hexToRgba(color); | ||
| type = "HEX"; | ||
| } else { | ||
| rgbaColor = color; | ||
| type = _utilsis.isColor.call(void 0, color, "RGBA") ? "RGBA" : "RGB"; | ||
| } | ||
| const rgbaValue = rgbaColor.replace("rgba(", "").replace("rgb(", "").replace(")", ""); | ||
| rgba = rgbaValue.split(",").map((m) => +m); | ||
| for (let i = 0; i < 3; i++) | ||
| rgba[i] = Math.floor(rgba[i] * (10 - level) / 10); | ||
| const typeMap = { | ||
| HEX: "", | ||
| RGB: `rgb(${rgba.join(", ")})`, | ||
| RGBA: `rgba(${rgba.join(", ")})` | ||
| }; | ||
| const darkenColor = typeMap[type]; | ||
| return darkenColor || rgbaToHex(rgba.length === 3 ? typeMap.RGB : typeMap.RGBA); | ||
| } | ||
| exports.darken = darken; exports.hexToRgba = hexToRgba; exports.lighten = lighten; exports.rgbaToHex = rgbaToHex; |
| declare function rgbaToHex(rgba: string): string; | ||
| declare function hexToRgba(hex: string): string; | ||
| declare function lighten(color: string, level?: number): string; | ||
| declare function darken(color: string, level?: number): string; | ||
| export { darken, hexToRgba, lighten, rgbaToHex }; |
| declare function rgbaToHex(rgba: string): string; | ||
| declare function hexToRgba(hex: string): string; | ||
| declare function lighten(color: string, level?: number): string; | ||
| declare function darken(color: string, level?: number): string; | ||
| export { darken, hexToRgba, lighten, rgbaToHex }; |
| // src/color.ts | ||
| import { isColor } from "@vtrbo/utils-is"; | ||
| function rgbaToHex(rgba) { | ||
| if (isColor(rgba, "HEX")) | ||
| return rgba; | ||
| if (!isColor(rgba, "RGB") && !isColor(rgba, "RGBA")) | ||
| return ""; | ||
| const rgbaValue = rgba.replace("rgba(", "").replace("rgb(", "").replace(")", ""); | ||
| const [r, g, b, a] = rgbaValue.split(",").map((m) => +m); | ||
| if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) | ||
| return `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}${a || a === 0 ? (a * 255 | 1 << 8).toString(16).slice(1) : ""}`; | ||
| return ""; | ||
| } | ||
| function hexToRgba(hex) { | ||
| if (isColor(hex, "RGB") || isColor(hex, "RGBA")) | ||
| return hex; | ||
| if (!isColor(hex, "HEX")) | ||
| return ""; | ||
| const getSingle = (start, end) => Number.parseInt(`0x${hex.slice(start, end)}${hex.slice(start, end)}`); | ||
| const getDouble = (start, end) => Number.parseInt(`0x${hex.slice(start, end)}`); | ||
| const getAlpha = (start, end, fn) => Math.round(fn(start, end) / 255 * 100) / 100; | ||
| const hexMap = { | ||
| 4: `rgb(${getSingle(1, 2)}, ${getSingle(2, 3)}, ${getSingle(3, 4)})`, | ||
| 5: `rgba(${getSingle(1, 2)}, ${getSingle(2, 3)}, ${getSingle(3, 4)}, ${getAlpha(4, 5, getSingle)})`, | ||
| 7: `rgb(${getDouble(1, 3)}, ${getDouble(3, 5)}, ${getDouble(5, 7)})`, | ||
| 9: `rgba(${getDouble(1, 3)}, ${getDouble(3, 5)}, ${getDouble(5, 7)}, ${getAlpha(7, 9, getDouble)})` | ||
| }; | ||
| return hexMap[hex.length] || ""; | ||
| } | ||
| function lighten(color, level = 10) { | ||
| if (!isColor(color, "HEX") && !isColor(color, "RGB") && !isColor(color, "RGBA")) | ||
| return ""; | ||
| let rgbaColor = ""; | ||
| let rgba = []; | ||
| let type = "RGBA"; | ||
| if (isColor(color, "HEX")) { | ||
| rgbaColor = hexToRgba(color); | ||
| type = "HEX"; | ||
| } else { | ||
| rgbaColor = color; | ||
| type = isColor(color, "RGBA") ? "RGBA" : "RGB"; | ||
| } | ||
| const rgbaValue = rgbaColor.replace("rgba(", "").replace("rgb(", "").replace(")", ""); | ||
| rgba = rgbaValue.split(",").map((m) => +m); | ||
| for (let i = 0; i < 3; i++) | ||
| rgba[i] = Math.floor((255 - rgba[i]) * level / 10 + rgba[i]); | ||
| const typeMap = { | ||
| HEX: "", | ||
| RGB: `rgb(${rgba.join(", ")})`, | ||
| RGBA: `rgba(${rgba.join(", ")})` | ||
| }; | ||
| const lightenColor = typeMap[type]; | ||
| return lightenColor || rgbaToHex(rgba.length === 3 ? typeMap.RGB : typeMap.RGBA); | ||
| } | ||
| function darken(color, level = 0) { | ||
| if (!isColor(color, "HEX") && !isColor(color, "RGB") && !isColor(color, "RGBA")) | ||
| return ""; | ||
| let rgbaColor = ""; | ||
| let rgba = []; | ||
| let type = "RGBA"; | ||
| if (isColor(color, "HEX")) { | ||
| rgbaColor = hexToRgba(color); | ||
| type = "HEX"; | ||
| } else { | ||
| rgbaColor = color; | ||
| type = isColor(color, "RGBA") ? "RGBA" : "RGB"; | ||
| } | ||
| const rgbaValue = rgbaColor.replace("rgba(", "").replace("rgb(", "").replace(")", ""); | ||
| rgba = rgbaValue.split(",").map((m) => +m); | ||
| for (let i = 0; i < 3; i++) | ||
| rgba[i] = Math.floor(rgba[i] * (10 - level) / 10); | ||
| const typeMap = { | ||
| HEX: "", | ||
| RGB: `rgb(${rgba.join(", ")})`, | ||
| RGBA: `rgba(${rgba.join(", ")})` | ||
| }; | ||
| const darkenColor = typeMap[type]; | ||
| return darkenColor || rgbaToHex(rgba.length === 3 ? typeMap.RGB : typeMap.RGBA); | ||
| } | ||
| export { | ||
| darken, | ||
| hexToRgba, | ||
| lighten, | ||
| rgbaToHex | ||
| }; |
+11
-16
| { | ||
| "name": "@vtrbo/utils-color", | ||
| "type": "module", | ||
| "version": "0.4.0-beta.5", | ||
| "version": "0.4.1-beta.0", | ||
| "description": "Collection of common JavaScript or TypeScript utils.", | ||
@@ -24,15 +24,14 @@ "author": { | ||
| ".": { | ||
| "types": "./index.d.ts", | ||
| "import": "./index.js", | ||
| "require": "./index.cjs" | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.js", | ||
| "require": "./dist/index.cjs" | ||
| } | ||
| }, | ||
| "main": "./index.js", | ||
| "module": "./index.js", | ||
| "types": "./index.d.ts", | ||
| "main": "./dist/index.js", | ||
| "module": "./dist/index.js", | ||
| "types": "./dist/index.d.ts", | ||
| "typesVersions": { | ||
| "*": { | ||
| "*": [ | ||
| "./*", | ||
| "./index.d.ts" | ||
| "./dist/index.d.ts" | ||
| ] | ||
@@ -42,11 +41,7 @@ } | ||
| "files": [ | ||
| "README.md", | ||
| "index.cjs", | ||
| "index.d.cts", | ||
| "index.d.ts", | ||
| "index.js" | ||
| "dist" | ||
| ], | ||
| "dependencies": { | ||
| "@vtrbo/utils-color": "0.4.0-beta.5", | ||
| "@vtrbo/utils-tool": "0.4.0-beta.5" | ||
| "@vtrbo/utils-is": "0.4.1-beta.0", | ||
| "@vtrbo/utils-tool": "0.4.1-beta.0" | ||
| }, | ||
@@ -53,0 +48,0 @@ "scripts": { |
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
9423
326.57%6
200%170
Infinity%0
-100%+ Added
+ Added
+ Added
- Removed
- Removed