@sv443-network/userutils 7.3.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @sv443-network/userutils
2
2
 
3
+ ## 8.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 54ee0ce: Changed `hexToRgb()` and `rgbToHex()` to support `#RGBA` and `#RRGGBBAA` color codes (with an alpha channel).
8
+ Both functions now have an `alpha` value immediately after `blue`, which can be set to `undefined` to restore the old behavior.
9
+
10
+ ### Minor Changes
11
+
12
+ - 54ee0ce: Added parameter `upperCase` (false by default) to `lightenColor()` and `darkenColor()`
13
+
14
+ ### Patch Changes
15
+
16
+ - 54ee0ce: Consolidated behavior of `lightenColor()` and `darkenColor()` when using non-number values
17
+
3
18
  ## 7.3.0
4
19
 
5
20
  ### Minor Changes
package/README.md CHANGED
@@ -69,8 +69,8 @@ View the documentation of previous major releases:
69
69
  - [`tr.getLanguage()`](#trgetlanguage) - returns the currently active language
70
70
  - [`tr.getTranslations()`](#trgettranslations) - returns the translations for the given language or the currently active one
71
71
  - [**Colors:**](#colors)
72
- - [`hexToRgb()`](#hextorgb) - convert a hex color string to an RGB number tuple
73
- - [`rgbToHex()`](#rgbtohex) - convert RGB numbers to a hex color string
72
+ - [`hexToRgb()`](#hextorgb) - convert a hex color string to an RGB or RGBA value tuple
73
+ - [`rgbToHex()`](#rgbtohex) - convert RGB or RGBA values to a hex color string
74
74
  - [`lightenColor()`](#lightencolor) - lighten a CSS color string (hex, rgb or rgba) by a given percentage
75
75
  - [`darkenColor()`](#darkencolor) - darken a CSS color string (hex, rgb or rgba) by a given percentage
76
76
  - [**Utility types for TypeScript:**](#utility-types)
@@ -2154,20 +2154,23 @@ The color functions are used to manipulate and convert colors in various formats
2154
2154
  ### hexToRgb()
2155
2155
  Usage:
2156
2156
  ```ts
2157
- hexToRgb(hex: string): [red: number, green: number, blue: number]
2157
+ hexToRgb(hex: string): [red: number, green: number, blue: number, alpha?: number]
2158
2158
  ```
2159
2159
 
2160
- Converts a hex color string to an RGB color tuple array.
2161
- Accepts the formats `#RRGGBB` and `#RGB`, with or without the hash symbol.
2160
+ Converts a hex color string to an RGB or RGBA color tuple array.
2161
+ The values of R, G and B will be in the range of 0-255, while the alpha value will be in the range of 0-1.
2162
+ Accepts the formats `#RRGGBB`, `#RRGGBBAA`, `#RGB` and `#RGBA`, with or without the hash symbol.
2162
2163
 
2163
2164
  <details><summary><b>Example - click to view</b></summary>
2164
2165
 
2165
2166
  ```ts
2166
2167
  import { hexToRgb } from "@sv443-network/userutils";
2167
2168
 
2168
- hexToRgb("#ff0000"); // [255, 0, 0]
2169
- hexToRgb("0032ef"); // [0, 50, 239]
2170
- hexToRgb("#0f0"); // [0, 255, 0]
2169
+ hexToRgb("#aaff85aa"); // [170, 255, 133, 0.6666666666666666]
2170
+ hexToRgb("#ff0000"); // [255, 0, 0, undefined]
2171
+ hexToRgb("0032ef"); // [0, 50, 239, undefined]
2172
+ hexToRgb("#0f0"); // [0, 255, 0, undefined]
2173
+ hexToRgb("0f0f"); // [0, 255, 0, 1]
2171
2174
  ```
2172
2175
  </details>
2173
2176
 
@@ -2176,10 +2179,10 @@ hexToRgb("#0f0"); // [0, 255, 0]
2176
2179
  ### rgbToHex()
2177
2180
  Usage:
2178
2181
  ```ts
2179
- rgbToHex(red: number, green: number, blue: number, withHash?: boolean, upperCase?: boolean): string
2182
+ rgbToHex(red: number, green: number, blue: number, alpha?: number, withHash?: boolean, upperCase?: boolean): string
2180
2183
  ```
2181
2184
 
2182
- Converts RGB color values to a hex color string.
2185
+ Converts RGB or RGBA color values to a hex color string.
2183
2186
  The `withHash` parameter determines if the hash symbol should be included in the output (true by default).
2184
2187
  The `upperCase` parameter determines if the output should be in uppercase (false by default).
2185
2188
 
@@ -2188,9 +2191,9 @@ The `upperCase` parameter determines if the output should be in uppercase (false
2188
2191
  ```ts
2189
2192
  import { rgbToHex } from "@sv443-network/userutils";
2190
2193
 
2191
- rgbToHex(255, 0, 0); // "#ff0000"
2192
- rgbToHex(255, 0, 0, false); // "ff0000"
2193
- rgbToHex(255, 0, 0, true, true); // "#FF0000"
2194
+ rgbToHex(255, 0, 0); // "#ff0000" (with hash symbol, lowercase)
2195
+ rgbToHex(255, 0, 0, 0.5, false); // "ff000080" (with alpha, no hash symbol, lowercase)
2196
+ rgbToHex(255, 0, 0, undefined, true, true); // "#FF0000" (no alpha, with hash symbol, uppercase)
2194
2197
  ```
2195
2198
  </details>
2196
2199
 
@@ -2199,11 +2202,12 @@ rgbToHex(255, 0, 0, true, true); // "#FF0000"
2199
2202
  ### lightenColor()
2200
2203
  Usage:
2201
2204
  ```ts
2202
- lightenColor(color: string, percent: number): string
2205
+ lightenColor(color: string, percent: number, upperCase?: boolean): string
2203
2206
  ```
2204
2207
 
2205
2208
  Lightens a CSS color value (in hex, RGB or RGBA format) by a given percentage.
2206
2209
  Will not exceed the maximum range (00-FF or 0-255).
2210
+ If the `upperCase` parameter is set to true (default is false), the output will be in uppercase.
2207
2211
  Throws an error if the color format is invalid or not supported.
2208
2212
 
2209
2213
  <details><summary><b>Example - click to view</b></summary>
@@ -2212,6 +2216,7 @@ Throws an error if the color format is invalid or not supported.
2212
2216
  import { lightenColor } from "@sv443-network/userutils";
2213
2217
 
2214
2218
  lightenColor("#ff0000", 20); // "#ff3333"
2219
+ lightenColor("#ff0000", 20, true); // "#FF3333"
2215
2220
  lightenColor("rgb(0, 255, 0)", 50); // "rgb(128, 255, 128)"
2216
2221
  lightenColor("rgba(0, 255, 0, 0.5)", 50); // "rgba(128, 255, 128, 0.5)"
2217
2222
  ```
@@ -2222,11 +2227,12 @@ lightenColor("rgba(0, 255, 0, 0.5)", 50); // "rgba(128, 255, 128, 0.5)"
2222
2227
  ### darkenColor()
2223
2228
  Usage:
2224
2229
  ```ts
2225
- darkenColor(color: string, percent: number): string
2230
+ darkenColor(color: string, percent: number, upperCase?: boolean): string
2226
2231
  ```
2227
2232
 
2228
2233
  Darkens a CSS color value (in hex, RGB or RGBA format) by a given percentage.
2229
2234
  Will not exceed the maximum range (00-FF or 0-255).
2235
+ If the `upperCase` parameter is set to true (default is false), the output will be in uppercase.
2230
2236
  Throws an error if the color format is invalid or not supported.
2231
2237
 
2232
2238
  <details><summary><b>Example - click to view</b></summary>
@@ -2235,6 +2241,7 @@ Throws an error if the color format is invalid or not supported.
2235
2241
  import { darkenColor } from "@sv443-network/userutils";
2236
2242
 
2237
2243
  darkenColor("#ff0000", 20); // "#cc0000"
2244
+ darkenColor("#ff0000", 20, true); // "#CC0000"
2238
2245
  darkenColor("rgb(0, 255, 0)", 50); // "rgb(0, 128, 0)"
2239
2246
  darkenColor("rgba(0, 255, 0, 0.5)", 50); // "rgba(0, 128, 0, 0.5)"
2240
2247
  ```
@@ -8,7 +8,7 @@
8
8
  // ==UserLibrary==
9
9
  // @name UserUtils
10
10
  // @description Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and more
11
- // @version 7.3.0
11
+ // @version 8.0.0
12
12
  // @license MIT
13
13
  // @copyright Sv443 (https://github.com/Sv443)
14
14
 
@@ -130,21 +130,26 @@ var UserUtils = (function (exports) {
130
130
 
131
131
  // lib/colors.ts
132
132
  function hexToRgb(hex) {
133
- hex = hex.trim();
134
- const bigint = parseInt(hex.startsWith("#") ? hex.slice(1) : hex, 16);
133
+ hex = (hex.startsWith("#") ? hex.slice(1) : hex).trim();
134
+ const a = hex.length === 8 || hex.length === 4 ? parseInt(hex.slice(-(hex.length / 4)), 16) / (hex.length === 8 ? 255 : 15) : void 0;
135
+ if (!isNaN(Number(a)))
136
+ hex = hex.slice(0, -(hex.length / 4));
137
+ if (hex.length === 3 || hex.length === 4)
138
+ hex = hex.split("").map((c) => c + c).join("");
139
+ const bigint = parseInt(hex, 16);
135
140
  const r = bigint >> 16 & 255;
136
141
  const g = bigint >> 8 & 255;
137
142
  const b = bigint & 255;
138
- return [clamp(r, 0, 255), clamp(g, 0, 255), clamp(b, 0, 255)];
143
+ return [clamp(r, 0, 255), clamp(g, 0, 255), clamp(b, 0, 255), typeof a === "number" ? clamp(a, 0, 1) : void 0];
139
144
  }
140
- function rgbToHex(red, green, blue, withHash = true, upperCase = false) {
145
+ function rgbToHex(red, green, blue, alpha, withHash = true, upperCase = false) {
141
146
  const toHexVal = (n) => clamp(Math.round(n), 0, 255).toString(16).padStart(2, "0")[upperCase ? "toUpperCase" : "toLowerCase"]();
142
- return `${withHash ? "#" : ""}${toHexVal(red)}${toHexVal(green)}${toHexVal(blue)}`;
147
+ return `${withHash ? "#" : ""}${toHexVal(red)}${toHexVal(green)}${toHexVal(blue)}${alpha ? toHexVal(alpha * 255) : ""}`;
143
148
  }
144
- function lightenColor(color, percent) {
145
- return darkenColor(color, percent * -1);
149
+ function lightenColor(color, percent, upperCase = false) {
150
+ return darkenColor(color, percent * -1, upperCase);
146
151
  }
147
- function darkenColor(color, percent) {
152
+ function darkenColor(color, percent, upperCase = false) {
148
153
  var _a;
149
154
  color = color.trim();
150
155
  const darkenRgb = (r2, g2, b2, percent2) => {
@@ -154,23 +159,21 @@ var UserUtils = (function (exports) {
154
159
  return [r2, g2, b2];
155
160
  };
156
161
  let r, g, b, a;
157
- const isHexCol = color.match(/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/);
162
+ const isHexCol = color.match(/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/);
158
163
  if (isHexCol)
159
- [r, g, b] = hexToRgb(color);
164
+ [r, g, b, a] = hexToRgb(color);
160
165
  else if (color.startsWith("rgb")) {
161
166
  const rgbValues = (_a = color.match(/\d+(\.\d+)?/g)) == null ? void 0 : _a.map(Number);
162
- if (rgbValues)
163
- [r, g, b, a] = rgbValues;
164
- else
167
+ if (!rgbValues)
165
168
  throw new Error("Invalid RGB/RGBA color format");
169
+ [r, g, b, a] = rgbValues;
166
170
  } else
167
171
  throw new Error("Unsupported color format");
168
172
  [r, g, b] = darkenRgb(r, g, b, percent);
169
- const upperCase = color.match(/[A-F]/) !== null;
170
173
  if (isHexCol)
171
- return rgbToHex(r, g, b, color.startsWith("#"), upperCase);
174
+ return rgbToHex(r, g, b, a, color.startsWith("#"), upperCase);
172
175
  else if (color.startsWith("rgba"))
173
- return `rgba(${r}, ${g}, ${b}, ${a})`;
176
+ return `rgba(${r}, ${g}, ${b}, ${a != null ? a : NaN})`;
174
177
  else if (color.startsWith("rgb"))
175
178
  return `rgb(${r}, ${g}, ${b})`;
176
179
  else
package/dist/index.js CHANGED
@@ -110,21 +110,26 @@ function randomizeArray(array) {
110
110
 
111
111
  // lib/colors.ts
112
112
  function hexToRgb(hex) {
113
- hex = hex.trim();
114
- const bigint = parseInt(hex.startsWith("#") ? hex.slice(1) : hex, 16);
113
+ hex = (hex.startsWith("#") ? hex.slice(1) : hex).trim();
114
+ const a = hex.length === 8 || hex.length === 4 ? parseInt(hex.slice(-(hex.length / 4)), 16) / (hex.length === 8 ? 255 : 15) : void 0;
115
+ if (!isNaN(Number(a)))
116
+ hex = hex.slice(0, -(hex.length / 4));
117
+ if (hex.length === 3 || hex.length === 4)
118
+ hex = hex.split("").map((c) => c + c).join("");
119
+ const bigint = parseInt(hex, 16);
115
120
  const r = bigint >> 16 & 255;
116
121
  const g = bigint >> 8 & 255;
117
122
  const b = bigint & 255;
118
- return [clamp(r, 0, 255), clamp(g, 0, 255), clamp(b, 0, 255)];
123
+ return [clamp(r, 0, 255), clamp(g, 0, 255), clamp(b, 0, 255), typeof a === "number" ? clamp(a, 0, 1) : void 0];
119
124
  }
120
- function rgbToHex(red, green, blue, withHash = true, upperCase = false) {
125
+ function rgbToHex(red, green, blue, alpha, withHash = true, upperCase = false) {
121
126
  const toHexVal = (n) => clamp(Math.round(n), 0, 255).toString(16).padStart(2, "0")[upperCase ? "toUpperCase" : "toLowerCase"]();
122
- return `${withHash ? "#" : ""}${toHexVal(red)}${toHexVal(green)}${toHexVal(blue)}`;
127
+ return `${withHash ? "#" : ""}${toHexVal(red)}${toHexVal(green)}${toHexVal(blue)}${alpha ? toHexVal(alpha * 255) : ""}`;
123
128
  }
124
- function lightenColor(color, percent) {
125
- return darkenColor(color, percent * -1);
129
+ function lightenColor(color, percent, upperCase = false) {
130
+ return darkenColor(color, percent * -1, upperCase);
126
131
  }
127
- function darkenColor(color, percent) {
132
+ function darkenColor(color, percent, upperCase = false) {
128
133
  var _a;
129
134
  color = color.trim();
130
135
  const darkenRgb = (r2, g2, b2, percent2) => {
@@ -134,23 +139,21 @@ function darkenColor(color, percent) {
134
139
  return [r2, g2, b2];
135
140
  };
136
141
  let r, g, b, a;
137
- const isHexCol = color.match(/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/);
142
+ const isHexCol = color.match(/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/);
138
143
  if (isHexCol)
139
- [r, g, b] = hexToRgb(color);
144
+ [r, g, b, a] = hexToRgb(color);
140
145
  else if (color.startsWith("rgb")) {
141
146
  const rgbValues = (_a = color.match(/\d+(\.\d+)?/g)) == null ? void 0 : _a.map(Number);
142
- if (rgbValues)
143
- [r, g, b, a] = rgbValues;
144
- else
147
+ if (!rgbValues)
145
148
  throw new Error("Invalid RGB/RGBA color format");
149
+ [r, g, b, a] = rgbValues;
146
150
  } else
147
151
  throw new Error("Unsupported color format");
148
152
  [r, g, b] = darkenRgb(r, g, b, percent);
149
- const upperCase = color.match(/[A-F]/) !== null;
150
153
  if (isHexCol)
151
- return rgbToHex(r, g, b, color.startsWith("#"), upperCase);
154
+ return rgbToHex(r, g, b, a, color.startsWith("#"), upperCase);
152
155
  else if (color.startsWith("rgba"))
153
- return `rgba(${r}, ${g}, ${b}, ${a})`;
156
+ return `rgba(${r}, ${g}, ${b}, ${a != null ? a : NaN})`;
154
157
  else if (color.startsWith("rgb"))
155
158
  return `rgb(${r}, ${g}, ${b})`;
156
159
  else
@@ -1,18 +1,21 @@
1
- /** Converts a hex color string to a tuple of RGB values */
2
- export declare function hexToRgb(hex: string): [red: number, green: number, blue: number];
3
- /** Converts RGB values to a hex color string */
4
- export declare function rgbToHex(red: number, green: number, blue: number, withHash?: boolean, upperCase?: boolean): string;
5
1
  /**
6
- * Lightens a CSS color value (in hex, RGB or RGBA format) by a given percentage.
2
+ * Converts a hex color string in the format `#RRGGBB`, `#RRGGBBAA` (or even `RRGGBB` and `RGB`) to a tuple.
3
+ * @returns Returns a tuple array where R, G and B are an integer from 0-255 and alpha is a float from 0 to 1, or undefined if no alpha channel exists.
4
+ */
5
+ export declare function hexToRgb(hex: string): [red: number, green: number, blue: number, alpha?: number];
6
+ /** Converts RGB or RGBA number values to a hex color string in the format `#RRGGBB` or `#RRGGBBAA` */
7
+ export declare function rgbToHex(red: number, green: number, blue: number, alpha?: number, withHash?: boolean, upperCase?: boolean): string;
8
+ /**
9
+ * Lightens a CSS color value (in #HEX, rgb() or rgba() format) by a given percentage.
7
10
  * Will not exceed the maximum range (00-FF or 0-255).
8
11
  * @returns Returns the new color value in the same format as the input
9
12
  * @throws Throws if the color format is invalid or not supported
10
13
  */
11
- export declare function lightenColor(color: string, percent: number): string;
14
+ export declare function lightenColor(color: string, percent: number, upperCase?: boolean): string;
12
15
  /**
13
- * Darkens a CSS color value (in hex, RGB or RGBA format) by a given percentage.
16
+ * Darkens a CSS color value (in #HEX, rgb() or rgba() format) by a given percentage.
14
17
  * Will not exceed the maximum range (00-FF or 0-255).
15
18
  * @returns Returns the new color value in the same format as the input
16
19
  * @throws Throws if the color format is invalid or not supported
17
20
  */
18
- export declare function darkenColor(color: string, percent: number): string;
21
+ export declare function darkenColor(color: string, percent: number, upperCase?: boolean): string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sv443-network/userutils",
3
3
  "libName": "UserUtils",
4
- "version": "7.3.0",
4
+ "version": "8.0.0",
5
5
  "description": "Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and more",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",