Skip to content

Commit 3343554

Browse files
committed
adding hextorgb and rgbtohex functions
1 parent 15ec0c3 commit 3343554

File tree

9 files changed

+79
-0
lines changed

9 files changed

+79
-0
lines changed

lib/main.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import randomColor from "./randoms/randomColor";
2222
import randomHsl from "./randoms/randomHsl";
2323
import isHappyNumber from "./numbers/isHappyNumber";
2424
import randomPassword from "./randoms/randomPassword";
25+
import rgbToHex from "./strings/rgbToHex";
26+
import hexToRgb from "./strings/hexToRgb";
2527
declare const functionality: {
2628
sumOfArray: typeof sumOfArray;
2729
capitalize: typeof capitalize;
@@ -47,6 +49,8 @@ declare const functionality: {
4749
randomHsl: typeof randomHsl;
4850
isHappyNumber: typeof isHappyNumber;
4951
randomPassword: typeof randomPassword;
52+
rgbToHex: typeof rgbToHex;
53+
hexToRgb: typeof hexToRgb;
5054
};
5155
declare global {
5256
interface Window {

lib/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import randomColor from "./randoms/randomColor";
2222
import randomHsl from "./randoms/randomHsl";
2323
import isHappyNumber from "./numbers/isHappyNumber";
2424
import randomPassword from "./randoms/randomPassword";
25+
import rgbToHex from "./strings/rgbToHex";
26+
import hexToRgb from "./strings/hexToRgb";
2527
const functionality = {
2628
sumOfArray,
2729
capitalize,
@@ -47,6 +49,8 @@ const functionality = {
4749
randomHsl,
4850
isHappyNumber,
4951
randomPassword,
52+
rgbToHex,
53+
hexToRgb,
5054
};
5155
window.functionality = functionality;
5256
// export * from "./sumOfArray";

lib/strings/hexToRgb.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* converts a hex color to rgb
3+
*
4+
* @param {string} hex
5+
* @returns {number[]}
6+
*/
7+
export default function hexToRgb(hex: string): number[];

lib/strings/hexToRgb.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* converts a hex color to rgb
3+
*
4+
* @param {string} hex
5+
* @returns {number[]}
6+
*/
7+
export default function hexToRgb(hex) {
8+
return hex
9+
.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (m, r, g, b) => "#" + r + r + g + g + b + b)
10+
.substring(1)
11+
.match(/.{2}/g)
12+
.map((x) => parseInt(x, 16));
13+
}

lib/strings/rgbToHex.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Converts an RGB color value to a hexadecimal string.
3+
*
4+
* @param {number} r
5+
* @param {number} g
6+
* @param {number} b
7+
* @returns {string}
8+
*/
9+
export default function rgbToHex(r: number, g: number, b: number): string;

lib/strings/rgbToHex.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Converts an RGB color value to a hexadecimal string.
3+
*
4+
* @param {number} r
5+
* @param {number} g
6+
* @param {number} b
7+
* @returns {string}
8+
*/
9+
export default function rgbToHex(r, g, b) {
10+
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
11+
}

project/ts/functionality/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import randomColor from "./randoms/randomColor";
2222
import randomHsl from "./randoms/randomHsl";
2323
import isHappyNumber from "./numbers/isHappyNumber";
2424
import randomPassword from "./randoms/randomPassword";
25+
import rgbToHex from "./strings/rgbToHex";
26+
import hexToRgb from "./strings/hexToRgb";
2527

2628
const functionality = {
2729
sumOfArray,
@@ -48,6 +50,8 @@ const functionality = {
4850
randomHsl,
4951
isHappyNumber,
5052
randomPassword,
53+
rgbToHex,
54+
hexToRgb,
5155
};
5256

5357
declare global {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* converts a hex color to rgb
3+
*
4+
* @param {string} hex
5+
* @returns {number[]}
6+
*/
7+
export default function hexToRgb(hex: string) {
8+
return hex
9+
.replace(
10+
/^#?([a-f\d])([a-f\d])([a-f\d])$/i,
11+
(m, r, g, b) => "#" + r + r + g + g + b + b
12+
)
13+
.substring(1)
14+
.match(/.{2}/g)
15+
.map((x) => parseInt(x, 16));
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Converts an RGB color value to a hexadecimal string.
3+
*
4+
* @param {number} r
5+
* @param {number} g
6+
* @param {number} b
7+
* @returns {string}
8+
*/
9+
export default function rgbToHex(r: number, g: number, b: number): string {
10+
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
11+
}

0 commit comments

Comments
 (0)