ismx-nexo-node-app 0.4.17 → 0.4.18

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.
@@ -1,10 +1,54 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class ColorUtils {
4
+ static hexToRgb(hex) {
5
+ hex = hex.replace(/^#/, '');
6
+ return {
7
+ r: parseInt(hex.slice(0, 2), 16),
8
+ g: parseInt(hex.slice(2, 4), 16),
9
+ b: parseInt(hex.slice(4, 6), 16),
10
+ };
11
+ }
12
+ static rgbToHex(r, g, b) {
13
+ return `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)}`;
14
+ }
15
+ static interpolate(color1, color2, ratio) {
16
+ const color1Rgb = ColorUtils.hexToRgb(color1);
17
+ const color2Rgb = ColorUtils.hexToRgb(color2);
18
+ // Interpolate each color component (r, g, b)
19
+ const r = Math.round(color1Rgb.r + (color2Rgb.r - color1Rgb.r) * ratio);
20
+ const g = Math.round(color1Rgb.g + (color2Rgb.g - color1Rgb.g) * ratio);
21
+ const b = Math.round(color1Rgb.b + (color2Rgb.b - color1Rgb.b) * ratio);
22
+ // Convert the resulting RGB back to hex
23
+ return ColorUtils.rgbToHex(r, g, b);
24
+ }
25
+ static contrast(color) {
26
+ // Ensure hex is in the correct format
27
+ color = color.replace(/^#/, '');
28
+ // Convert 3-digit hex to 6-digit
29
+ if (color.length === 3) {
30
+ color = color.split('').map(x => x + x).join('');
31
+ }
32
+ // Convert hex to RGB
33
+ let r = parseInt(color.substring(0, 2), 16) / 255;
34
+ let g = parseInt(color.substring(2, 4), 16) / 255;
35
+ let b = parseInt(color.substring(4, 6), 16) / 255;
36
+ // Apply gamma correction (sRGB)
37
+ r = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);
38
+ g = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4);
39
+ b = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
40
+ // Calculate luminance
41
+ let luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
42
+ // WCAG threshold for contrast readability (4.5:1)
43
+ return luminance > 0.4 ? '#111111' : '#FFFFFF';
44
+ }
4
45
  static transparency(color, transparency) {
5
- if (!color || !transparency)
6
- return undefined;
7
- return color.substring(0, 7) + Math.ceil(transparency * 255).toString(16);
46
+ if (!/^#([0-9A-F]{3}){1,2}$/i.test(color))
47
+ throw new Error('Invalid hex color format');
48
+ if (color.length === 4)
49
+ color = '#' + color.slice(1).split('').map(x => x + x).join('');
50
+ let alphaHex = Math.round(transparency * 255).toString(16).padStart(2, '0');
51
+ return color + alphaHex;
8
52
  }
9
53
  }
10
54
  exports.default = ColorUtils;
@@ -1,3 +1,11 @@
1
1
  export default abstract class ColorUtils {
2
- static transparency(color?: string, transparency?: number): string | undefined;
2
+ static hexToRgb(hex: string): {
3
+ r: number;
4
+ g: number;
5
+ b: number;
6
+ };
7
+ static rgbToHex(r: number, g: number, b: number): string;
8
+ static interpolate(color1: string, color2: string, ratio: number): string;
9
+ static contrast(color: string): string;
10
+ static transparency(color: string, transparency: number): string;
3
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.17",
3
+ "version": "0.4.18",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && npx tsc",
@@ -1,8 +1,63 @@
1
1
  export default abstract class ColorUtils {
2
2
 
3
- static transparency(color?: string, transparency?: number) {
4
- if (!color || !transparency) return undefined;
5
- return color.substring(0, 7) + Math.ceil(transparency*255).toString(16);
3
+ static hexToRgb(hex: string): { r: number; g: number; b: number }
4
+ {
5
+ hex = hex.replace(/^#/, '');
6
+ return {
7
+ r: parseInt(hex.slice(0, 2), 16),
8
+ g: parseInt(hex.slice(2, 4), 16),
9
+ b: parseInt(hex.slice(4, 6), 16),
10
+ };
6
11
  }
7
12
 
13
+ static rgbToHex(r:number, g: number, b: number): string
14
+ {
15
+ return `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)}`;
16
+ }
17
+
18
+ static interpolate(color1: string, color2: string, ratio: number) {
19
+ const color1Rgb = ColorUtils.hexToRgb(color1);
20
+ const color2Rgb = ColorUtils.hexToRgb(color2);
21
+
22
+ // Interpolate each color component (r, g, b)
23
+ const r = Math.round(color1Rgb.r + (color2Rgb.r - color1Rgb.r) * ratio);
24
+ const g = Math.round(color1Rgb.g + (color2Rgb.g - color1Rgb.g) * ratio);
25
+ const b = Math.round(color1Rgb.b + (color2Rgb.b - color1Rgb.b) * ratio);
26
+
27
+ // Convert the resulting RGB back to hex
28
+ return ColorUtils.rgbToHex(r, g, b);
29
+ }
30
+
31
+ static contrast(color: string): string {
32
+ // Ensure hex is in the correct format
33
+ color = color.replace(/^#/, '');
34
+
35
+ // Convert 3-digit hex to 6-digit
36
+ if (color.length === 3) {
37
+ color = color.split('').map(x => x + x).join('');
38
+ }
39
+
40
+ // Convert hex to RGB
41
+ let r = parseInt(color.substring(0, 2), 16) / 255;
42
+ let g = parseInt(color.substring(2, 4), 16) / 255;
43
+ let b = parseInt(color.substring(4, 6), 16) / 255;
44
+
45
+ // Apply gamma correction (sRGB)
46
+ r = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);
47
+ g = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4);
48
+ b = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
49
+
50
+ // Calculate luminance
51
+ let luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
52
+
53
+ // WCAG threshold for contrast readability (4.5:1)
54
+ return luminance > 0.4 ? '#111111' : '#FFFFFF';
55
+ }
56
+
57
+ static transparency(color: string, transparency: number) {
58
+ if (!/^#([0-9A-F]{3}){1,2}$/i.test(color)) throw new Error('Invalid hex color format');
59
+ if (color.length === 4) color = '#' + color.slice(1).split('').map(x => x + x).join('');
60
+ let alphaHex = Math.round(transparency * 255).toString(16).padStart(2, '0');
61
+ return color + alphaHex;
62
+ }
8
63
  }