Skip to content

Commit ad92674

Browse files
committed
function to generate hex colors
1 parent ad94cc7 commit ad92674

12 files changed

+98
-11
lines changed

README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# generate-random-color
22

3-
NPM package for generating random RGB, RGBA, HSL and HSLA colors.
3+
NPM package for generating random HEX, RGB, RGBA, HSL and HSLA colors.
44

55
## Install
66

@@ -16,7 +16,11 @@ const generateRandomColor = require('generate-random-color');
1616
import generateRandomColor from 'generate-random-color';
1717

1818
// The function can either be called by simply calling the function like this
19+
generateRandomColor.hex();
1920
generateRandomColor.rgb();
21+
generateRandomColor.rgba();
22+
generateRandomColor.hsl();
23+
generateRandomColor.hsla();
2024

2125
// or by passing optional min max values.
2226
generateRandomColor.rgb({
@@ -31,22 +35,30 @@ generateRandomColor.rgb({
3135
b: { min: 0, max: 100 },
3236
});
3337

34-
// RGBA
38+
// HEX can be passed a specific value. If no input is given the value will be between 00 - FF
39+
// Returns #FF69B4
40+
generateRandomColor.hex({
41+
r: 'ff',
42+
g: '69',
43+
b: 'b4',
44+
});
45+
46+
// RGBA default values
3547
generateRandomColor.rgba({
3648
r: { min: 0, max: 255 },
3749
g: { min: 0, max: 255 },
3850
b: { min: 0, max: 255 },
3951
a: { min: 0, max: 1 },
4052
});
4153

42-
// HSL
54+
// HSL default values
4355
generateRandomColor.hsl({
4456
h: { min: 0, max: 360 },
4557
s: { min: 0, max: 100 },
4658
l: { min: 0, max: 100 },
4759
});
4860

49-
// HSLA
61+
// HSLA default values
5062
generateRandomColor.hsla({
5163
h: { min: 0, max: 360 },
5264
s: { min: 0, max: 100 },

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
],
2525
"scripts": {
2626
"start": "ts-node src/index.ts",
27-
"tet": "ts-node src/__tests__/tet.ts",
2827
"build": "tsc -b",
2928
"format": "prettier --write . ",
3029
"lint": "eslint src/**/*.ts",

src/__tests__/hex.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { hex } from '../index';
2+
3+
test('HEX: Typeof', () => {
4+
expect(typeof hex()).toBe('string');
5+
});
6+
7+
test('HEX: Typeof', () => {
8+
expect(hex({ r: 'ff', g: 'ff', b: 'ff' }) === '#FFFFFF').toBe(true);
9+
});

src/__tests__/hsl.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { hsl } from '../index';
22

3-
test('Typeof HSL', () => {
3+
test('HSL: Typeof', () => {
44
expect(typeof hsl()).toBe('string');
55
});

src/__tests__/hsla.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { hsla } from '../index';
22

3-
test('Typeof HSLA', () => {
3+
test('HSLA: Typeof', () => {
44
expect(typeof hsla()).toBe('string');
55
});

src/__tests__/rgb.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { rgb } from '../index';
22

3-
test('Typeof rgb', () => {
3+
test('RGB: Typeof', () => {
44
expect(typeof rgb()).toBe('string');
55
});

src/__tests__/rgba.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { rgba } from '../index';
22

3-
test('Typeof rgba', () => {
3+
test('RGBA: Typeof', () => {
44
expect(typeof rgba()).toBe('string');
55
});

src/color-types/hex.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import generateNum from '../helpers/generateNum';
2+
3+
type HEX = {
4+
r?: string;
5+
g?: string;
6+
b?: string;
7+
};
8+
9+
function generateHex() {
10+
const generate = () => {
11+
const value = generateNum(16, { min: 0, max: 15 });
12+
13+
return convertNumberToHex(value);
14+
};
15+
16+
return `${generate()}${generate()}`;
17+
}
18+
19+
function convertNumberToHex(input: number) {
20+
switch (input) {
21+
case 10:
22+
return 'A';
23+
24+
case 11:
25+
return 'B';
26+
27+
case 12:
28+
return 'C';
29+
30+
case 13:
31+
return 'D';
32+
33+
case 14:
34+
return 'E';
35+
36+
case 15:
37+
return 'F';
38+
39+
default:
40+
return input.toString();
41+
}
42+
}
43+
44+
export default function (input?: HEX): string {
45+
const r = input?.r ? input.r : generateHex();
46+
const g = input?.g ? input.g : generateHex();
47+
const b = input?.b ? input.b : generateHex();
48+
49+
return `#${r.toUpperCase()}${g.toUpperCase()}${b.toUpperCase()}`;
50+
}

src/color-types/hsl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ type HSL = {
77
};
88

99
export default function (input?: HSL): string {
10-
return `hsl(${generateNum(361, input?.h)}, ${generateNum(101, input?.s)}%, ${generateNum(101, input?.h)}%)`;
10+
return `hsl(${generateNum(361, input?.h)}, ${generateNum(101, input?.s)}%, ${generateNum(101, input?.l)}%)`;
1111
}

src/color-types/hsla.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ type HSLA = {
1111
export default function (input?: HSLA): string {
1212
return `hsla(${generateNum(361, input?.h)}, ${generateNum(101, input?.s)}%, ${generateNum(
1313
101,
14-
input?.h,
14+
input?.l,
1515
)}%, ${generateFloat(1, input?.a)})`;
1616
}

src/helpers/generateNum.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type minMax = {
44
};
55

66
export default function (trueMax: number, numRange?: minMax): number {
7+
if (numRange?.max) ++numRange.max;
8+
79
const max = numRange?.max != undefined && numRange.max <= trueMax ? numRange.max : trueMax;
810

911
const min = numRange?.min != undefined && numRange.min >= 0 ? numRange.min : 0;

src/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import genHSL from './color-types/hsl';
22
import genHSLA from './color-types/hsla';
33
import genRGB from './color-types/rgb';
44
import genRGBA from './color-types/rgba';
5+
import genHEX from './color-types/hex';
56

67
type HSL = {
78
h?: { min?: number; max?: number };
@@ -28,6 +29,12 @@ type RGBA = {
2829
a?: { min?: number; max?: number };
2930
};
3031

32+
type HEX = {
33+
r?: string;
34+
g?: string;
35+
b?: string;
36+
};
37+
3138
export const hsl = (input?: HSL): string => {
3239
const base = {
3340
h: {
@@ -111,3 +118,11 @@ export const rgba = (input?: RGBA): string => {
111118

112119
return genRGBA(base);
113120
};
121+
122+
export const hex = (input?: HEX): string => {
123+
if (input) {
124+
return genHEX(input);
125+
} else {
126+
return genHEX();
127+
}
128+
};

0 commit comments

Comments
 (0)