Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dsedinkin committed Aug 8, 2022
0 parents commit c629c43
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 SEDINKIN

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { THexToRgbValue, THexToRgb } from "./type";
export declare const hexToRgb: (hex: THexToRgbValue) => THexToRgb;
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hexToRgb = void 0;
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 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;
}
};
exports.hexToRgb = hexToRgb;
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "hex-to-rgb",
"version": "1.0.0",
"description": "",
"main": "./index.js",
"types": "./index.d.ts",
"license": "MIT",
"author": {
"name": "Дмитрий Сединкин",
"url": "https://github.com/dsedinkin"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dsedinkin/hex-to-rgb.git"
},
"dependencies": {}
}
6 changes: 6 additions & 0 deletions type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare type TFormatValue = string;
export declare type TFormat = number;
export declare type TAlphaValue = string;
export declare type TAlpha = string | undefined;
export declare type THexToRgbValue = string;
export declare type THexToRgb = string | undefined;
2 changes: 2 additions & 0 deletions type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

0 comments on commit c629c43

Please sign in to comment.