@yookue/ts-lang-utils
Advanced tools
Comparing version 0.1.60 to 0.1.61
@@ -6,2 +6,4 @@ export declare abstract class ColorUtils { | ||
static hslToHex(hsl?: string | [h: number, s: number, l: number]): string | undefined; | ||
static reverseHex(hex?: string): string | undefined; | ||
static reverseRgb(rgb?: string | [r: number, g: number, b: number]): string | undefined; | ||
} |
@@ -50,2 +50,3 @@ var __defProp = Object.defineProperty; | ||
* ```ts | ||
* ColorUtils.hexToRgb('#fff'); // 'rgb(255, 255, 255)' | ||
* ColorUtils.hexToRgb('#ff0000'); // 'rgb(255, 0, 0)' | ||
@@ -59,11 +60,11 @@ * ``` | ||
const rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; | ||
const rep = hex.replace(rgx, (m, r2, g2, b2) => r2 + r2 + g2 + g2 + b2 + b2); | ||
const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(rep); | ||
if (!rgb) { | ||
const rep = hex.replace(rgx, (m, r, g, b) => r + r + g + g + b + b); | ||
const arr = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(rep); | ||
if (!arr || arr.length !== 4) { | ||
return void 0; | ||
} | ||
const r = Number.parseInt(rgb[1], 16); | ||
const g = Number.parseInt(rgb[2], 16); | ||
const b = Number.parseInt(rgb[3], 16); | ||
return `rgb(${r}, ${g}, ${b})`; | ||
const hexR = Number.parseInt(arr[1], 16); | ||
const hexG = Number.parseInt(arr[2], 16); | ||
const hexB = Number.parseInt(arr[3], 16); | ||
return `rgb(${hexR}, ${hexG}, ${hexB})`; | ||
} | ||
@@ -139,2 +140,60 @@ /** | ||
} | ||
/** | ||
* Returns the reversed hex color from the given color | ||
* | ||
* @param hex the hex color to inspect | ||
* | ||
* @returns the reversed hex color from the given color | ||
* | ||
* @example | ||
* ```ts | ||
* ColorUtils.reverseHex('#000'); // 'rgb(255, 255, 255)' | ||
* ColorUtils.reverseHex('#fff'); // 'rgb(0, 0, 0)' | ||
* ``` | ||
*/ | ||
static reverseHex(hex) { | ||
if (!hex) { | ||
return void 0; | ||
} | ||
const rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; | ||
const rep = hex.replace(rgx, (m, r, g, b) => r + r + g + g + b + b); | ||
const arr = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(rep); | ||
if (!arr || arr.length !== 4) { | ||
return void 0; | ||
} | ||
const hexR = (255 - Number.parseInt(arr[1], 16)).toString(16).padStart(2, "0"); | ||
const hexG = (255 - Number.parseInt(arr[2], 16)).toString(16).padStart(2, "0"); | ||
const hexB = (255 - Number.parseInt(arr[3], 16)).toString(16).padStart(2, "0"); | ||
return `#${hexR}${hexG}${hexB}`; | ||
} | ||
/** | ||
* Returns the reversed rgb color from the given color | ||
* | ||
* @param rgb the rgb color to inspect | ||
* | ||
* @returns the reversed rgb color from the given color | ||
* | ||
* @example | ||
* ```ts | ||
* ColorUtils.reverseRgb('rgb(0, 0, 0)'); // 'rgb(255, 255, 255)' | ||
* ColorUtils.reverseRgb([255, 255, 255]); // 'rgb(0, 0, 0)' | ||
* ``` | ||
*/ | ||
static reverseRgb(rgb) { | ||
if (!rgb) { | ||
return void 0; | ||
} | ||
let alias = null; | ||
if (typeof rgb === "string") { | ||
const arr = rgb.match(/\d+/g); | ||
if (!arr || arr.length !== 3) { | ||
return void 0; | ||
} | ||
alias = arr.map(Number); | ||
} else { | ||
alias = rgb; | ||
} | ||
const [r, g, b] = alias; | ||
return `rgb(${255 - r}, ${255 - g}, ${255 - b})`; | ||
} | ||
}; | ||
@@ -141,0 +200,0 @@ // Annotate the CommonJS export names for ESM import in node: |
@@ -6,2 +6,4 @@ export declare abstract class ColorUtils { | ||
static hslToHex(hsl?: string | [h: number, s: number, l: number]): string | undefined; | ||
static reverseHex(hex?: string): string | undefined; | ||
static reverseRgb(rgb?: string | [r: number, g: number, b: number]): string | undefined; | ||
} |
@@ -23,10 +23,10 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; | ||
}); | ||
var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(rep); | ||
if (!rgb) { | ||
var arr = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(rep); | ||
if (!arr || arr.length !== 4) { | ||
return undefined; | ||
} | ||
var r = Number.parseInt(rgb[1], 16); | ||
var g = Number.parseInt(rgb[2], 16); | ||
var b = Number.parseInt(rgb[3], 16); | ||
return "rgb(".concat(r, ", ").concat(g, ", ").concat(b, ")"); | ||
var hexR = Number.parseInt(arr[1], 16); | ||
var hexG = Number.parseInt(arr[2], 16); | ||
var hexB = Number.parseInt(arr[3], 16); | ||
return "rgb(".concat(hexR, ", ").concat(hexG, ", ").concat(hexB, ")"); | ||
} | ||
@@ -88,4 +88,46 @@ }, { | ||
} | ||
}, { | ||
key: "reverseHex", | ||
value: function reverseHex(hex) { | ||
if (!hex) { | ||
return undefined; | ||
} | ||
var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; | ||
var rep = hex.replace(rgx, function (m, r, g, b) { | ||
return r + r + g + g + b + b; | ||
}); | ||
var arr = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(rep); | ||
if (!arr || arr.length !== 4) { | ||
return undefined; | ||
} | ||
var hexR = (255 - Number.parseInt(arr[1], 16)).toString(16).padStart(2, '0'); | ||
var hexG = (255 - Number.parseInt(arr[2], 16)).toString(16).padStart(2, '0'); | ||
var hexB = (255 - Number.parseInt(arr[3], 16)).toString(16).padStart(2, '0'); | ||
return "#".concat(hexR).concat(hexG).concat(hexB); | ||
} | ||
}, { | ||
key: "reverseRgb", | ||
value: function reverseRgb(rgb) { | ||
if (!rgb) { | ||
return undefined; | ||
} | ||
var alias = null; | ||
if (typeof rgb === 'string') { | ||
var arr = rgb.match(/\d+/g); | ||
if (!arr || arr.length !== 3) { | ||
return undefined; | ||
} | ||
alias = arr.map(Number); | ||
} else { | ||
alias = rgb; | ||
} | ||
var _alias5 = alias, | ||
_alias6 = _slicedToArray(_alias5, 3), | ||
r = _alias6[0], | ||
g = _alias6[1], | ||
b = _alias6[2]; | ||
return "rgb(".concat(255 - r, ", ").concat(255 - g, ", ").concat(255 - b, ")"); | ||
} | ||
}]); | ||
return ColorUtils; | ||
}(); |
{ | ||
"name": "@yookue/ts-lang-utils", | ||
"version": "0.1.60", | ||
"version": "0.1.61", | ||
"title": "TsLangUtils", | ||
@@ -5,0 +5,0 @@ "description": "Common lang utilities for typescript", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
373861
7678