From af55c59c3436f688212610cf2ec0f9cbb612c6bf Mon Sep 17 00:00:00 2001 From: ful1e5 <24286590+ful1e5@users.noreply.github.com> Date: Fri, 19 Feb 2021 17:05:19 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=88=20Custom=20colors=20in=20Bibata's?= =?UTF-8?q?=20svg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In previous version, svg's colors replaced by batch proccess.Thats also consume more memory. ColoredSvgGenerator class shrink down to simple function. --- bitmapper/packages/core/src/index.ts | 53 ++++++++++++++++++++++++-- bitmapper/packages/core/src/types.ts | 20 ++++++++++ bitmapper/packages/modern/src/index.ts | 9 ++--- 3 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 bitmapper/packages/core/src/types.ts diff --git a/bitmapper/packages/core/src/index.ts b/bitmapper/packages/core/src/index.ts index 588aa924..9a3bdff1 100644 --- a/bitmapper/packages/core/src/index.ts +++ b/bitmapper/packages/core/src/index.ts @@ -1,5 +1,52 @@ -const main = () => { - console.log("Bibata Core"); +import { Colors } from "./types"; + +/** + * Default Key Colors for generating colored svg. + * base="#00FF00" (Green) + * outline="#0000FF" (Blue) + * watch.background="#FF0000" (Red) + * */ +const defaultKeyColors: Colors = { + base: "#00FF00", + outline: "#0000FF", + watch: { + background: "#FF0000", + }, }; -export { main }; +/** + * Customize colors of svg code. + * @param {string} content SVG code. + * @param {Colors} colors Customize colors. + * @param {Colors} [keys] Colors Key, That was written SVG code. + * @returns {string} SVG code with colors. + */ +const colorSvg = ( + content: string, + colors: Colors, + keys: Colors = defaultKeyColors +): string => { + content = content + .replace(new RegExp(keys.base, "ig"), colors.base) + .replace(new RegExp(keys.outline, "ig"), colors.outline); + + try { + // === trying to replace `watch` color === + + if (!colors.watch?.background) { + throw new Error(""); + } + const { background: b } = colors.watch; + content = content.replace(new RegExp(keys.watch!.background, "ig"), b); // Watch Background + } catch (error) { + // === on error => replace `watch` color as `base` === + + content = content.replace( + new RegExp(keys.watch!.background, "ig"), + colors.base + ); + } + return content; +}; + +export { colorSvg }; diff --git a/bitmapper/packages/core/src/types.ts b/bitmapper/packages/core/src/types.ts new file mode 100644 index 00000000..cd5ddb33 --- /dev/null +++ b/bitmapper/packages/core/src/types.ts @@ -0,0 +1,20 @@ +/** + * Hex Colors in string Format. + * + * `Example: `"#FFFFFF" + */ +type HexColor = string; + +/** + * @Colors expect `base`, `outline` & `watch-background` colors in **HexColor** Format. + * @default background is `base` color. + */ +type Colors = { + base: HexColor; + outline: HexColor; + watch?: { + background: HexColor; + }; +}; + +export { Colors }; diff --git a/bitmapper/packages/modern/src/index.ts b/bitmapper/packages/modern/src/index.ts index 81bea90a..7d395430 100644 --- a/bitmapper/packages/modern/src/index.ts +++ b/bitmapper/packages/modern/src/index.ts @@ -1,8 +1,5 @@ -import { main } from "bibata-core"; - -const hmm = async () => { - main(); - console.log("Bibata modern"); +const main = async () => { + console.log("Bibata Modern"); }; -hmm(); +main();