Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dsedinkin committed Jul 11, 2023
1 parent c629c43 commit 21580d6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 56 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

8 changes: 6 additions & 2 deletions index.d.ts
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 };
38 changes: 17 additions & 21 deletions index.js
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;
19 changes: 19 additions & 0 deletions index.mjs
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 };
8 changes: 4 additions & 4 deletions package.json
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": {}
}
6 changes: 0 additions & 6 deletions type.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions type.js

This file was deleted.

0 comments on commit 21580d6

Please sign in to comment.