diff --git a/src/aimless.ts b/src/aimless.ts index bc2afcb..439df05 100644 --- a/src/aimless.ts +++ b/src/aimless.ts @@ -15,3 +15,4 @@ export { default as uniqFuncIntRange } from './uniq-func-int-range' export { default as uniqFuncSequence } from './uniq-func-sequence' export { uuid, uuidWithEngine } from './uuid' export { weighted, weightWithEngine } from './weighted' +export { rgba } from './rgba' diff --git a/src/rgba.ts b/src/rgba.ts new file mode 100644 index 0000000..3f5a0b8 --- /dev/null +++ b/src/rgba.ts @@ -0,0 +1,13 @@ +import { intRange } from './int-range' + +const rgba = (alpha?: number) => { + const r = intRange(0, 255) + const g = intRange(0, 255) + const b = intRange(0, 255) + if (alpha === undefined || alpha === null) return `rgb(${r}, ${g}, ${b})` + return `rgba(${r}, ${g}, ${b}, ${alpha})` +} + +export { + rgba +} diff --git a/test/index.js b/test/index.js index 694afdf..07b0709 100644 --- a/test/index.js +++ b/test/index.js @@ -28,7 +28,8 @@ const { uniqFuncSequence, uniqFuncIntRange, weighted, - weightWithEngine + weightWithEngine, + rgba } = require('../dist/aimless') /* eslint-env mocha */ @@ -175,6 +176,11 @@ describe('aimless', () => { expect([true, false]).to.include(bool()) }) + it('should produce a random rgb(a) color string', () => { + expect(rgba().replace(/\s+/g, '')).to.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/) + expect(rgba(0.5).replace(/\s+/g, '')).to.match(/^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),([01](\.\d+)?)\)$/) + }) + it('should produce a sign (-1 or 1)', () => { for (let i = 0; i < 100; i++) { expect([-1, 1]).to.include(sign())