-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
46 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
import { THexToRgbValue, THexToRgb } from "./type"; | ||
export declare const hexToRgb: (hex: THexToRgbValue) => THexToRgb; | ||
type THexToRgbValue = string; | ||
type THexToRgb = string | undefined; | ||
|
||
declare const hexToRgb: (hex: THexToRgbValue) => THexToRgb; | ||
|
||
export { hexToRgb }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hexToRgb = void 0; | ||
'use strict'; | ||
|
||
const format = (string) => parseInt(string, 16); | ||
const alpha = (string, formatString = format(string)) => string === "00" ? "0%" : (formatString ? formatString === 255 ? undefined : (+(formatString / 255).toPrecision(2) * 100) + "%" : undefined); | ||
const alpha = (string, formatString = format(string)) => string === "00" ? "0%" : formatString ? formatString === 255 ? void 0 : +(formatString / 255).toPrecision(2) * 100 + "%" : void 0; | ||
const hexToRgb = (hex) => { | ||
try { | ||
hex = hex.replace("#", ""); | ||
if (hex.length === 3 || hex.length === 4) { | ||
const { r, g, b, a } = { r: format(hex.substring(0, 1).repeat(2)), g: format(hex.substring(1, 2).repeat(2)), b: format(hex.substring(2, 3).repeat(2)), a: alpha(hex.substring(3, 4).repeat(2)) }; | ||
return `rgb(${r} ${g} ${b}${a ? " / " + a : ""})`; | ||
} | ||
else if (hex.length === 6 || hex.length === 8) { | ||
const { r, g, b, a } = { r: format(hex.substring(0, 2)), g: format(hex.substring(2, 4)), b: format(hex.substring(4, 6)), a: alpha(hex.substring(6, 8)) }; | ||
return `rgb(${r} ${g} ${b}${a ? " / " + a : ""})`; | ||
} | ||
else | ||
throw Error("Недопустимый формат hex."); | ||
} | ||
catch (err) { | ||
console.error("lib hexToRgb: ", err); | ||
return undefined; | ||
} | ||
try { | ||
hex = hex.replace("#", ""); | ||
if (hex.length === 3 || hex.length === 4) { | ||
const { r, g, b, a } = { r: format(hex.substring(0, 1).repeat(2)), g: format(hex.substring(1, 2).repeat(2)), b: format(hex.substring(2, 3).repeat(2)), a: alpha(hex.substring(3, 4).repeat(2)) }; | ||
return `rgb(${r} ${g} ${b}${a ? " / " + a : ""})`; | ||
} else if (hex.length === 6 || hex.length === 8) { | ||
const { r, g, b, a } = { r: format(hex.substring(0, 2)), g: format(hex.substring(2, 4)), b: format(hex.substring(4, 6)), a: alpha(hex.substring(6, 8)) }; | ||
return `rgb(${r} ${g} ${b}${a ? " / " + a : ""})`; | ||
} else | ||
throw Error("\u041D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u044B\u0439 \u0444\u043E\u0440\u043C\u0430\u0442 hex."); | ||
} catch (err) { | ||
return void 0; | ||
} | ||
}; | ||
|
||
exports.hexToRgb = hexToRgb; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const format = (string) => parseInt(string, 16); | ||
const alpha = (string, formatString = format(string)) => string === "00" ? "0%" : formatString ? formatString === 255 ? void 0 : +(formatString / 255).toPrecision(2) * 100 + "%" : void 0; | ||
const hexToRgb = (hex) => { | ||
try { | ||
hex = hex.replace("#", ""); | ||
if (hex.length === 3 || hex.length === 4) { | ||
const { r, g, b, a } = { r: format(hex.substring(0, 1).repeat(2)), g: format(hex.substring(1, 2).repeat(2)), b: format(hex.substring(2, 3).repeat(2)), a: alpha(hex.substring(3, 4).repeat(2)) }; | ||
return `rgb(${r} ${g} ${b}${a ? " / " + a : ""})`; | ||
} else if (hex.length === 6 || hex.length === 8) { | ||
const { r, g, b, a } = { r: format(hex.substring(0, 2)), g: format(hex.substring(2, 4)), b: format(hex.substring(4, 6)), a: alpha(hex.substring(6, 8)) }; | ||
return `rgb(${r} ${g} ${b}${a ? " / " + a : ""})`; | ||
} else | ||
throw Error("\u041D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u044B\u0439 \u0444\u043E\u0440\u043C\u0430\u0442 hex."); | ||
} catch (err) { | ||
return void 0; | ||
} | ||
}; | ||
|
||
export { hexToRgb }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
{ | ||
"name": "hex-to-rgb", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
"main": "./index.js", | ||
"types": "./index.d.ts", | ||
"license": "MIT", | ||
"author": { | ||
"name": "Дмитрий Сединкин", | ||
"url": "https://github.com/dsedinkin" | ||
"name": "SedLab", | ||
"url": "https://github.com/sedlab" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/dsedinkin/hex-to-rgb.git" | ||
"url": "git+https://github.com/sedlab/hex-to-rgb.git" | ||
}, | ||
"dependencies": {} | ||
} |