Skip to content

Commit f448515

Browse files
committed
v1.1.1 - ready to publish
1 parent b984daa commit f448515

File tree

6 files changed

+37
-45
lines changed

6 files changed

+37
-45
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
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/[email protected].0)
10+
[![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/dynamic-colors)](https://bundlephobia.com/package/[email protected].1)
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/[email protected].0/)
12+
[![jsDelivr hits (npm scoped)](https://img.shields.io/jsdelivr/gh/hy/patelka2211/dynamic-colors)](https://cdn.jsdelivr.net/gh/patelka2211/[email protected].1/)
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/[email protected].0/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/[email protected].1/DynamicColors.js) in your website using a `<script>` tag:
2121

2222
```html
23-
<script src="https://cdn.jsdelivr.net/gh/patelka2211/[email protected].0/DynamicColors.js"></script>
23+
<script src="https://cdn.jsdelivr.net/gh/patelka2211/[email protected].1/DynamicColors.js"></script>
2424
```
2525

2626
## Available APIs

package-lock.json

Lines changed: 2 additions & 2 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.1.0",
3+
"version": "1.1.1",
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/OSTheme.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ function OSThemeChangesListenerCallback(event: MediaQueryListEvent): void {
5050
/**
5151
* Media query list for monitoring color scheme changes.
5252
*/
53-
let colorSchemeMediaQuery: MediaQueryList | null;
53+
let colorSchemeMediaQuery: MediaQueryList | null = null;
5454

5555
/**
5656
* Common function for setting theme (light or dark).
5757
* @param {LightOrDark} theme The theme to set (light or dark).
5858
* @returns {void}
5959
*/
60-
function commonForSetTheme(theme: LightOrDark): void {
60+
function commonForLightAndDarkTheme(theme: LightOrDark): void {
6161
theme3x = theme;
6262

6363
changeThemeInDOM(theme);
@@ -73,41 +73,43 @@ function commonForSetTheme(theme: LightOrDark): void {
7373

7474
/**
7575
* Sets the theme to light.
76-
* @returns {void}
76+
* @returns {'light'} Returns "light"
7777
*/
78-
export function setLightTheme(): void {
79-
if (theme3x === "light") return;
80-
commonForSetTheme("light");
78+
export function setLightTheme(): "light" {
79+
if (theme3x !== "light") commonForLightAndDarkTheme("light");
80+
return "light";
8181
}
8282

8383
/**
8484
* Sets the theme to dark.
85-
* @returns {void}
85+
* @returns {'dark'} Returns "dark"
8686
*/
87-
export function setDarkTheme(): void {
88-
if (theme3x === "dark") return;
89-
commonForSetTheme("dark");
87+
export function setDarkTheme(): "dark" {
88+
if (theme3x !== "dark") commonForLightAndDarkTheme("dark");
89+
return "dark";
9090
}
9191

9292
/**
93-
* Sets the theme to auto, which adjusts based on the OS theme.
94-
* @returns {void}
93+
* Sets the automatic theme based on the user's system preference.
94+
* @returns {LightOrDark} The current theme ("light", "dark").
9595
*/
96-
export function setAutoTheme(): void {
97-
if (theme3x === "auto" || window.matchMedia === undefined) return;
98-
96+
export function setAutoTheme(): LightOrDark {
97+
if (window.matchMedia === undefined) return setLightTheme();
98+
if (theme3x === "auto" && colorSchemeMediaQuery !== null)
99+
return colorSchemeMediaQuery.matches ? "dark" : "light";
99100
theme3x = "auto";
100-
101101
colorSchemeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
102+
let theme: LightOrDark = colorSchemeMediaQuery.matches ? "dark" : "light";
102103

103104
// Check initial color scheme
104-
changeThemeInDOM(colorSchemeMediaQuery.matches ? "dark" : "light");
105+
changeThemeInDOM(theme);
105106

106107
// Watch for changes in color scheme
107108
colorSchemeMediaQuery.addEventListener(
108109
"change",
109110
OSThemeChangesListenerCallback
110111
);
112+
return theme;
111113
}
112114

113115
/**
@@ -121,12 +123,5 @@ export function themeCycle(): Theme {
121123
return theme3x;
122124
}
123125

124-
function runAfterLoad() {
125-
setAutoTheme();
126-
setTimeout(() => {
127-
window.removeEventListener("load", runAfterLoad);
128-
}, 1000);
129-
}
130-
131-
// Event listener function to set the theme to auto on page load.
132-
window.addEventListener("load", runAfterLoad);
126+
// Set auto theme as default.
127+
setAutoTheme();

src/hex2rgb.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ export function hex2rgb(hex: string) {
33
hex = hex.replace("#", "");
44

55
// Ensure the remaining string has a valid length
6-
if (hex.length !== 3 && hex.length !== 6) {
7-
return;
8-
}
6+
if (hex.length !== 3 && hex.length !== 6) return;
97

108
// Expand shorthand if needed
11-
if (hex.length === 3) {
9+
if (hex.length === 3)
1210
hex = hex
1311
.split("")
1412
.map((char) => char + char)
1513
.join("");
16-
}
14+
15+
let colorList: number[] = ["r", "g", "b"].map((value, index) =>
16+
parseInt(hex.slice(index * 2, index * 2 + 2), 16)
17+
);
1718

1819
// Return the RGB values as an object
19-
return {
20-
r: parseInt(hex.slice(0, 2), 16),
21-
g: parseInt(hex.slice(2, 4), 16),
22-
b: parseInt(hex.slice(4, 6), 16),
23-
};
20+
return { r: colorList[0], g: colorList[1], b: colorList[2] };
2421
}

0 commit comments

Comments
 (0)