Skip to content

Commit

Permalink
feat: add JSDoc and TS defs
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisCodesThings committed May 1, 2024
1 parent 33ad208 commit 25012c7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions index.d.ts
@@ -0,0 +1 @@
export { default } from "./src/cssHexColorToRGBA.js";
2 changes: 1 addition & 1 deletion index.js
@@ -1,2 +1,2 @@
export { default } from "./src/cssHexColorToRGB.js";
export { default } from "./src/cssHexColorToRGBA.js";

17 changes: 0 additions & 17 deletions src/cssHexColorToRGB.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/cssHexColorToRGBA.d.ts
@@ -0,0 +1,6 @@
/**
* Converts a colour from a CSS hex colour code to an RGBA array
* @param {string} col CSS hex colour code
* @returns {[number, number, number, number]}
*/
export default function _default(col: string): [number, number, number, number];
19 changes: 19 additions & 0 deletions src/cssHexColorToRGBA.js
@@ -0,0 +1,19 @@
// @ts-check

import expandCSSHexColor from "@chriscodesthings/expand-css-hex-color";
import { roundToPrecision } from "more-rounding";

/**
* Converts a colour from a CSS hex colour code to an RGBA array
* @param {string} col CSS hex colour code
* @returns {[number, number, number, number]}
*/
export default function (col) {
const hex = expandCSSHexColor(col).slice(1);
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
const a = roundToPrecision(parseInt(hex.slice(6, 8), 16) / 255, 2);

return [r, g, b, a];
}

0 comments on commit 25012c7

Please sign in to comment.