Skip to content

Commit b984daa

Browse files
committed
v1.1.0 - ready to publish
1 parent 20b0175 commit b984daa

File tree

10 files changed

+41
-32
lines changed

10 files changed

+41
-32
lines changed

DynamicColors.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
# Dynamic Colors
44

5-
**Dynamic Colors** is a JavaScript library that can dynamically generate color theme from a single HEX color and it provides a range of useful APIs for creating, managing, and manipulating color themes such as [_create()_](#create), [_remove()_](#remove), [_setAutoTheme()_](#setautotheme), [_setDarkTheme()_](#setdarktheme), [_setLightTheme()_](#setlighttheme), [_themeCycle()_](#themecycle).
5+
**Dynamic Colors** is a JavaScript library that can dynamically generate color theme from a single HEX color and it provides a range of useful APIs for creating, managing, and manipulating color themes such as [_create()_](#create), [_remove()_](#remove), [_setAutoTheme()_](#setautotheme), [_setDarkTheme()_](#setdarktheme), [_setLightTheme()_](#setlighttheme), [_themeCycle()_](#themecycle), [_isInstance()_](#isinstance).
66

77
## Installation
88

99
[![npm (scoped)](https://img.shields.io/npm/v/dynamic-colors)](https://www.npmjs.com/package/dynamic-colors)
10-
[![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/dynamic-colors)](https://bundlephobia.com/package/dynamic-colors@1.0.4)
10+
[![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/dynamic-colors)](https://bundlephobia.com/package/dynamic-colors@1.1.0)
1111
[![npm](https://img.shields.io/npm/dy/dynamic-colors)](https://www.npmjs.com/package/dynamic-colors)
12-
[![jsDelivr hits (npm scoped)](https://img.shields.io/jsdelivr/gh/hy/patelka2211/dynamic-colors)](https://cdn.jsdelivr.net/gh/patelka2211/dynamic-colors@1.0.4/)
12+
[![jsDelivr hits (npm scoped)](https://img.shields.io/jsdelivr/gh/hy/patelka2211/dynamic-colors)](https://cdn.jsdelivr.net/gh/patelka2211/dynamic-colors@1.1.0/)
1313

1414
To install Dynamic Colors using npm, run the following command:
1515

1616
```sh
1717
npm i dynamic-colors
1818
```
1919

20-
Alternatively, you can include [Dynnamic Colors's IIFE file](https://cdn.jsdelivr.net/gh/patelka2211/dynamic-colors@1.0.4/DynamicColors.js) in your website using a `<script>` tag:
20+
Alternatively, you can include [Dynnamic Colors's IIFE file](https://cdn.jsdelivr.net/gh/patelka2211/dynamic-colors@1.1.0/DynamicColors.js) in your website using a `<script>` tag:
2121

2222
```html
23-
<script src="https://cdn.jsdelivr.net/gh/patelka2211/dynamic-colors@1.0.4/DynamicColors.js"></script>
23+
<script src="https://cdn.jsdelivr.net/gh/patelka2211/dynamic-colors@1.1.0/DynamicColors.js"></script>
2424
```
2525

2626
## Available APIs
@@ -83,6 +83,10 @@ The [themeCycle()](#themecycle) function cycles through different themes based o
8383
- If the current theme is `Light`, the next theme will be `Dark`.
8484
- If the current theme is `Dark`, the next theme will be `Auto`.
8585

86+
### isInstance()
87+
88+
Determines whether the given object is an instance of [`DynamicColors`](#dynamiccolors-class). Returns `true` if the object is an instance of [`DynamicColors`](#dynamiccolors-class), otherwise `false`.
89+
8690
## Internally used functionalities
8791

8892
### DynamicColors class

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dynamic-colors",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "Dynamic Colors is a JavaScript library that can dynamically generate color theme from a single HEX color and it provides a range of useful APIs for creating, managing, and manipulating color themes.",
55
"main": "index.js",
66
"module": "index.js",

src/dynamicColors.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ export class DynamicColors {
3030
this.styleTag.setAttribute("id", id);
3131
document.head.appendChild(this.styleTag);
3232
this.id = id;
33+
this.setColor(HEXColor);
34+
addInstance(this);
3335
} else
3436
throw Error(
35-
`Unable to create another 'DynamicColors' instance with the name '${name}'. To modify the color of the tag with the name '${name}', please use the 'setColor()' method on the existing instance.`
37+
`Unable to create another 'DynamicColors' instance with the name '${name}'.\n\nTo modify the color of the tag with the name '${name}', please use the 'setColor' method on the existing instance.`
3638
);
37-
38-
this.setColor(HEXColor);
39-
addInstance(this);
4039
}
4140

4241
/**
@@ -47,14 +46,15 @@ export class DynamicColors {
4746
public setColor(HEXColor: string) {
4847
let color = validateHEXColor(HEXColor);
4948

50-
if (!(typeof color === "boolean" && !color) && this.id !== undefined) {
51-
this.styleTag.setAttribute("dc-color", color);
49+
if (color === false)
50+
throw Error(`"${HEXColor}" is not valid HEX color.`);
5251

53-
const { css, theme2x } = getThemeCSSFromColor(this.id, color);
52+
if (this.id === undefined) return;
5453

55-
this.styleTag.innerHTML = css;
56-
this.styleTag.setAttribute("dc-theme", theme2x);
57-
} else throw Error(`"${HEXColor}" is not valid HEX color.`);
54+
const { css, theme2x } = getThemeCSSFromColor(this.id, color);
55+
this.styleTag.innerHTML = css;
56+
this.styleTag.setAttribute("dc-color", color);
57+
this.styleTag.setAttribute("dc-theme", theme2x);
5858
}
5959

6060
/**
@@ -116,7 +116,7 @@ export function create(name: string, HEXColor: string): DynamicColors {
116116
/**
117117
* Removes a DynamicColors instance.
118118
* @param {DynamicColors | string} dcTagInstanceOrName The DynamicColors `instance` or `name` to remove.
119-
* @returns {Promise<boolean>} `true` if the instance is removed, `false` otherwise.
119+
* @returns {Promise<boolean>} `true` if the instance is removed, otherwise `false`.
120120
*/
121121
export async function remove(
122122
dcTagInstanceOrName: DynamicColors | string
@@ -138,3 +138,13 @@ export async function remove(
138138
return false;
139139
}
140140
}
141+
142+
/**
143+
* Determines whether the given object is an instance of `DynamicColors`.
144+
*
145+
* @param {unknown} object The object to check.
146+
* @returns {boolean} Returns `true` if the object is an instance of `DynamicColors`, otherwise `false`.
147+
*/
148+
export function isInstance(object: unknown): boolean {
149+
return object instanceof DynamicColors;
150+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export {
44
setLightTheme,
55
themeCycle,
66
} from "./OSTheme";
7-
export { create, remove } from "./dynamicColors";
7+
export { create, isInstance, remove } from "./dynamicColors";

src/instances.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function addInstance(dynamicColors: DynamicColors) {
1818
/**
1919
* Removes a DynamicColors instance from the instances array and removes its associated HTML element.
2020
* @param {DynamicColors} dynamicColors The DynamicColors instance to remove.
21-
* @returns {boolean} `true` if the instance is removed, `false` otherwise.
21+
* @returns {boolean} `true` if the instance is removed, otherwise `false`.
2222
*/
2323
export function removeInstance(dynamicColors: DynamicColors): boolean {
2424
if (dynamicColors.dcID !== undefined && isLocked(dynamicColors.dcID))

src/lockMechanism.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function removeLock(DynamicColorsID: string) {
2020
/**
2121
* Check if DynamicColors instance is locked or not.
2222
* @param DynamicColorsID
23-
* @returns {boolean} `true` if instance is locked, `false` otherwise.
23+
* @returns {boolean} `true` if instance is locked, otherwise `false`.
2424
*/
2525
export function isLocked(DynamicColorsID: string): boolean {
2626
let lock = lockList.includes(DynamicColorsID);

src/validateHEX.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Validates a HEX color value.
33
* @param {string} color The color value to validate.
4-
* @returns {string | false} The validated color value if it is a valid HEX color, or `false` otherwise.
4+
* @returns {string | false} The validated color value if it is a valid HEX color, or otherwise `false`.
55
*/
66
export function validateHEXColor(color: string): string | false {
77
if (/^#([0-9A-Fa-f]{3}){1,2}$/i.test(color)) return color.toLowerCase();

tsconfig.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
"module": "ES6",
66
"rootDir": "./src",
77
"allowJs": true,
8-
"declaration": true,
98
"outDir": "./lib",
10-
"strict": true,
11-
"moduleResolution": "nodenext"
12-
// "esModuleInterop": true,
13-
// "forceConsistentCasingInFileNames": true,
14-
// "skipLibCheck": true
9+
"strict": true
1510
},
1611
"include": ["src"],
1712
"exclude": ["node_modules", "test"]

0 commit comments

Comments
 (0)