From d8cd23d27e3c510ba4b6e8b3a0f6c95c80081eb2 Mon Sep 17 00:00:00 2001 From: Kamil Chmielewski Date: Fri, 30 Aug 2024 15:29:19 +0200 Subject: [PATCH] feat(exd-118): added random color generator function and implemented into tag.color service --- src/canvas/canvas-tag.service.ts | 3 ++- src/helpers/random-color-generator.ts | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 src/helpers/random-color-generator.ts diff --git a/src/canvas/canvas-tag.service.ts b/src/canvas/canvas-tag.service.ts index 111def2..2eaefdd 100644 --- a/src/canvas/canvas-tag.service.ts +++ b/src/canvas/canvas-tag.service.ts @@ -13,6 +13,7 @@ import { PagedResult, } from '../common/pageable.utils'; import { Uuid } from '../common/common.interface'; +import { randomColorGenerator } from '../helpers/random-color-generator'; @Injectable() export class CanvasTagService { @@ -34,7 +35,7 @@ export class CanvasTagService { const tag = new CanvasTagEntity(); tag.name = tagName; tag.description = command.description?.trim(); - tag.color = command.color?.trim(); + tag.color = command.color?.trim() || randomColorGenerator().trim(); await this.canvasTagRepository.save(tag); return tag; } diff --git a/src/helpers/random-color-generator.ts b/src/helpers/random-color-generator.ts new file mode 100644 index 0000000..cf2c5ab --- /dev/null +++ b/src/helpers/random-color-generator.ts @@ -0,0 +1,6 @@ +export const randomColorGenerator = () => { + const color = Math.floor(Math.random() * 16777215) + .toString(16) + .padStart(6, '0'); + return `#${color}`; +};