@etsoo/shared 1.1.14 → 1.1.15

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/README.md CHANGED
@@ -18,6 +18,17 @@ $ yarn add @etsoo/shared
18
18
  ## storage
19
19
  Storage interface and browser storage implementation
20
20
 
21
+ ## EColor
22
+ Etsoo implmented Color
23
+
24
+ |Name|Description|
25
+ |---:|---|
26
+ |static getColors|Get HEX or RGB colors|
27
+ |static getEColors|Get EColors|
28
+ |static parse|Parse HTML color to EColor|
29
+ |toHEXColor|To HEX color string|
30
+ |toRGBColor|To RGB color string|
31
+
21
32
  ## Keyboard
22
33
  Keyboard keys and codes
23
34
 
@@ -0,0 +1,17 @@
1
+ import { EColor } from '../src/types/EColor';
2
+
3
+ test('Tests for parse', () => {
4
+ // Arrange & act
5
+ const colorShort = EColor.parse('#000');
6
+ const color = EColor.parse('#e21821');
7
+ const colorRgb = EColor.parse('RGB(226, 24, 33)');
8
+
9
+ // Assert
10
+ expect(colorShort?.toRGBColor()).toBe('RGB(0, 0, 0)');
11
+ expect(color?.toRGBColor()).toBe('RGB(226, 24, 33)');
12
+ expect(colorRgb?.toHEXColor()).toBe('#e21821');
13
+ });
14
+
15
+ test('Tests for getColors', () => {
16
+ expect(EColor.getColors(undefined, 128).length).toBe(8);
17
+ });
@@ -1,5 +1,6 @@
1
- export * from './types/FormData';
2
1
  export * from './types/DelayedExecutorType';
2
+ export * from './types/EColor';
3
+ export * from './types/FormData';
3
4
  export * from './storage/IStorage';
4
5
  export * from './storage/WindowStorage';
5
6
  export * from './DataTypes';
package/lib/cjs/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -10,8 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
15
  };
12
16
  Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./types/FormData"), exports);
14
17
  __exportStar(require("./types/DelayedExecutorType"), exports);
18
+ __exportStar(require("./types/EColor"), exports);
19
+ __exportStar(require("./types/FormData"), exports);
15
20
  __exportStar(require("./storage/IStorage"), exports);
16
21
  __exportStar(require("./storage/WindowStorage"), exports);
17
22
  __exportStar(require("./DataTypes"), exports);
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Etsoo implmented Color
3
+ */
4
+ export declare class EColor {
5
+ readonly r: number;
6
+ readonly g: number;
7
+ readonly b: number;
8
+ readonly alpha?: number | undefined;
9
+ /**
10
+ * Adjust value
11
+ * @param value Current value
12
+ * @param adjust Adjust value
13
+ * @returns Adjusted value
14
+ */
15
+ static adjust(value: number, adjust?: number): number;
16
+ /**
17
+ * Get HEX or RGB colors
18
+ * @param init Initial color
19
+ * @param factor Increase factor
20
+ * @param hex to HEX or not
21
+ * @returns Result
22
+ */
23
+ static getColors(init?: string, factor?: number, hex?: boolean): string[];
24
+ /**
25
+ * Get EColors
26
+ * @param init Initial color
27
+ * @param factor Increase factor
28
+ * @returns Result
29
+ */
30
+ static getEColors(init?: string, factor?: number): EColor[];
31
+ /**
32
+ * HEX string to integer value
33
+ * @param hex HEX string
34
+ * @returns Integer value
35
+ */
36
+ static hexTo(hex: string): number;
37
+ /**
38
+ * Format value to 16 radix string
39
+ * @param num Int value
40
+ * @returns Result
41
+ */
42
+ static toHex(num: number): string;
43
+ /**
44
+ * Parse HTML color to EColor
45
+ * @param htmlColor HTML color
46
+ * @returns EColor
47
+ */
48
+ static parse(htmlColor?: string | null): EColor | undefined;
49
+ /**
50
+ * Constructor
51
+ * @param r Reg
52
+ * @param g Green
53
+ * @param b Blue
54
+ * @param alpha Alpha
55
+ */
56
+ constructor(r: number, g: number, b: number, alpha?: number | undefined);
57
+ /**
58
+ * Clone color with adjusts
59
+ * @param adjustR Adjust R value
60
+ * @param adjustG Adjust G value
61
+ * @param adjustB Adjust B value
62
+ * @param alpha New alpha value
63
+ */
64
+ clone(adjustR?: number, adjustG?: number, adjustB?: number, alpha?: number): EColor | undefined;
65
+ /**
66
+ * To RGB color string
67
+ * @param includeAlpha Include alpha or not
68
+ * @returns RGB color string
69
+ */
70
+ toRGBColor(includeAlpha?: boolean): string;
71
+ /**
72
+ * To HEX color string
73
+ * @returns HEX color string
74
+ */
75
+ toHEXColor(): string;
76
+ }
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EColor = void 0;
4
+ /**
5
+ * Etsoo implmented Color
6
+ */
7
+ class EColor {
8
+ /**
9
+ * Constructor
10
+ * @param r Reg
11
+ * @param g Green
12
+ * @param b Blue
13
+ * @param alpha Alpha
14
+ */
15
+ constructor(r, g, b, alpha) {
16
+ this.r = r;
17
+ this.g = g;
18
+ this.b = b;
19
+ this.alpha = alpha;
20
+ }
21
+ /**
22
+ * Adjust value
23
+ * @param value Current value
24
+ * @param adjust Adjust value
25
+ * @returns Adjusted value
26
+ */
27
+ static adjust(value, adjust) {
28
+ if (adjust == null)
29
+ return value;
30
+ value = Math.abs(value + adjust);
31
+ if (value > 255)
32
+ return value % 255;
33
+ return value;
34
+ }
35
+ /**
36
+ * Get HEX or RGB colors
37
+ * @param init Initial color
38
+ * @param factor Increase factor
39
+ * @param hex to HEX or not
40
+ * @returns Result
41
+ */
42
+ static getColors(init = '#000', factor = 51, hex = true) {
43
+ return EColor.getEColors(init, factor).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
44
+ }
45
+ /**
46
+ * Get EColors
47
+ * @param init Initial color
48
+ * @param factor Increase factor
49
+ * @returns Result
50
+ */
51
+ static getEColors(init = '#000', factor = 51) {
52
+ var _a;
53
+ // Init color
54
+ const initColor = (_a = EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor(0, 0, 0);
55
+ // Factors elements
56
+ // 51 = '00', '33', '66', '99', 'cc', 'ff'
57
+ const factors = [];
58
+ let f = 0;
59
+ while (f <= 255) {
60
+ factors.push(f);
61
+ f += factor;
62
+ }
63
+ // RGB loop
64
+ const colors = [initColor];
65
+ for (const r of factors) {
66
+ for (const g of factors) {
67
+ for (const b of factors) {
68
+ const newColor = initColor.clone(r, g, b);
69
+ if (newColor)
70
+ colors.push(newColor);
71
+ }
72
+ }
73
+ }
74
+ return colors;
75
+ }
76
+ /**
77
+ * HEX string to integer value
78
+ * @param hex HEX string
79
+ * @returns Integer value
80
+ */
81
+ static hexTo(hex) {
82
+ return parseInt(hex, 16);
83
+ }
84
+ /**
85
+ * Format value to 16 radix string
86
+ * @param num Int value
87
+ * @returns Result
88
+ */
89
+ static toHex(num) {
90
+ return num.toString(16).padStart(2, '0');
91
+ }
92
+ /**
93
+ * Parse HTML color to EColor
94
+ * @param htmlColor HTML color
95
+ * @returns EColor
96
+ */
97
+ static parse(htmlColor) {
98
+ // Null
99
+ if (htmlColor == null)
100
+ return undefined;
101
+ htmlColor = htmlColor.toUpperCase();
102
+ // HEX color
103
+ if (htmlColor.startsWith('#')) {
104
+ htmlColor = htmlColor.substring(1);
105
+ if (htmlColor.length === 3)
106
+ htmlColor = Array.from(htmlColor)
107
+ .map((c) => c + c)
108
+ .join('');
109
+ if (htmlColor.length === 6) {
110
+ return new EColor(EColor.hexTo(htmlColor.substring(0, 2)), EColor.hexTo(htmlColor.substring(2, 4)), EColor.hexTo(htmlColor.substring(4, 6)));
111
+ }
112
+ return undefined;
113
+ }
114
+ // For RGB and RGBA
115
+ const reg = /^RGBA?\(([0-9,\s\.]+)\)$/;
116
+ const result = htmlColor.match(reg);
117
+ if (result != null && result.length == 2) {
118
+ const parts = result[1].split(/\s*,\s*/);
119
+ if (parts.length === 3 || parts.length === 4) {
120
+ const alpha = parts[3];
121
+ return new EColor(parseInt(parts[0]), parseInt(parts[1]), parseInt(parts[2]), alpha == null ? undefined : parseFloat(alpha));
122
+ }
123
+ }
124
+ return undefined;
125
+ }
126
+ /**
127
+ * Clone color with adjusts
128
+ * @param adjustR Adjust R value
129
+ * @param adjustG Adjust G value
130
+ * @param adjustB Adjust B value
131
+ * @param alpha New alpha value
132
+ */
133
+ clone(adjustR, adjustG, adjustB, alpha) {
134
+ const r = EColor.adjust(this.r, adjustR);
135
+ const g = EColor.adjust(this.g, adjustG);
136
+ const b = EColor.adjust(this.b, adjustB);
137
+ if (r === this.r &&
138
+ g === this.g &&
139
+ b === this.b &&
140
+ (alpha == null || alpha === this.alpha))
141
+ return undefined;
142
+ return new EColor(r, g, b, alpha);
143
+ }
144
+ /**
145
+ * To RGB color string
146
+ * @param includeAlpha Include alpha or not
147
+ * @returns RGB color string
148
+ */
149
+ toRGBColor(includeAlpha) {
150
+ var _a;
151
+ // Default case
152
+ includeAlpha !== null && includeAlpha !== void 0 ? includeAlpha : (includeAlpha = this.alpha != null);
153
+ if (includeAlpha)
154
+ return `RGBA(${this.r}, ${this.g}, ${this.b}, ${(_a = this.alpha) !== null && _a !== void 0 ? _a : 1})`;
155
+ return `RGB(${this.r}, ${this.g}, ${this.b})`;
156
+ }
157
+ /**
158
+ * To HEX color string
159
+ * @returns HEX color string
160
+ */
161
+ toHEXColor() {
162
+ return `#${EColor.toHex(this.r)}${EColor.toHex(this.g)}${EColor.toHex(this.b)}`;
163
+ }
164
+ }
165
+ exports.EColor = EColor;
@@ -1,5 +1,6 @@
1
- export * from './types/FormData';
2
1
  export * from './types/DelayedExecutorType';
2
+ export * from './types/EColor';
3
+ export * from './types/FormData';
3
4
  export * from './storage/IStorage';
4
5
  export * from './storage/WindowStorage';
5
6
  export * from './DataTypes';
package/lib/mjs/index.js CHANGED
@@ -1,5 +1,6 @@
1
- export * from './types/FormData';
2
1
  export * from './types/DelayedExecutorType';
2
+ export * from './types/EColor';
3
+ export * from './types/FormData';
3
4
  export * from './storage/IStorage';
4
5
  export * from './storage/WindowStorage';
5
6
  export * from './DataTypes';
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Etsoo implmented Color
3
+ */
4
+ export declare class EColor {
5
+ readonly r: number;
6
+ readonly g: number;
7
+ readonly b: number;
8
+ readonly alpha?: number | undefined;
9
+ /**
10
+ * Adjust value
11
+ * @param value Current value
12
+ * @param adjust Adjust value
13
+ * @returns Adjusted value
14
+ */
15
+ static adjust(value: number, adjust?: number): number;
16
+ /**
17
+ * Get HEX or RGB colors
18
+ * @param init Initial color
19
+ * @param factor Increase factor
20
+ * @param hex to HEX or not
21
+ * @returns Result
22
+ */
23
+ static getColors(init?: string, factor?: number, hex?: boolean): string[];
24
+ /**
25
+ * Get EColors
26
+ * @param init Initial color
27
+ * @param factor Increase factor
28
+ * @returns Result
29
+ */
30
+ static getEColors(init?: string, factor?: number): EColor[];
31
+ /**
32
+ * HEX string to integer value
33
+ * @param hex HEX string
34
+ * @returns Integer value
35
+ */
36
+ static hexTo(hex: string): number;
37
+ /**
38
+ * Format value to 16 radix string
39
+ * @param num Int value
40
+ * @returns Result
41
+ */
42
+ static toHex(num: number): string;
43
+ /**
44
+ * Parse HTML color to EColor
45
+ * @param htmlColor HTML color
46
+ * @returns EColor
47
+ */
48
+ static parse(htmlColor?: string | null): EColor | undefined;
49
+ /**
50
+ * Constructor
51
+ * @param r Reg
52
+ * @param g Green
53
+ * @param b Blue
54
+ * @param alpha Alpha
55
+ */
56
+ constructor(r: number, g: number, b: number, alpha?: number | undefined);
57
+ /**
58
+ * Clone color with adjusts
59
+ * @param adjustR Adjust R value
60
+ * @param adjustG Adjust G value
61
+ * @param adjustB Adjust B value
62
+ * @param alpha New alpha value
63
+ */
64
+ clone(adjustR?: number, adjustG?: number, adjustB?: number, alpha?: number): EColor | undefined;
65
+ /**
66
+ * To RGB color string
67
+ * @param includeAlpha Include alpha or not
68
+ * @returns RGB color string
69
+ */
70
+ toRGBColor(includeAlpha?: boolean): string;
71
+ /**
72
+ * To HEX color string
73
+ * @returns HEX color string
74
+ */
75
+ toHEXColor(): string;
76
+ }
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Etsoo implmented Color
3
+ */
4
+ export class EColor {
5
+ /**
6
+ * Constructor
7
+ * @param r Reg
8
+ * @param g Green
9
+ * @param b Blue
10
+ * @param alpha Alpha
11
+ */
12
+ constructor(r, g, b, alpha) {
13
+ this.r = r;
14
+ this.g = g;
15
+ this.b = b;
16
+ this.alpha = alpha;
17
+ }
18
+ /**
19
+ * Adjust value
20
+ * @param value Current value
21
+ * @param adjust Adjust value
22
+ * @returns Adjusted value
23
+ */
24
+ static adjust(value, adjust) {
25
+ if (adjust == null)
26
+ return value;
27
+ value = Math.abs(value + adjust);
28
+ if (value > 255)
29
+ return value % 255;
30
+ return value;
31
+ }
32
+ /**
33
+ * Get HEX or RGB colors
34
+ * @param init Initial color
35
+ * @param factor Increase factor
36
+ * @param hex to HEX or not
37
+ * @returns Result
38
+ */
39
+ static getColors(init = '#000', factor = 51, hex = true) {
40
+ return EColor.getEColors(init, factor).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
41
+ }
42
+ /**
43
+ * Get EColors
44
+ * @param init Initial color
45
+ * @param factor Increase factor
46
+ * @returns Result
47
+ */
48
+ static getEColors(init = '#000', factor = 51) {
49
+ var _a;
50
+ // Init color
51
+ const initColor = (_a = EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor(0, 0, 0);
52
+ // Factors elements
53
+ // 51 = '00', '33', '66', '99', 'cc', 'ff'
54
+ const factors = [];
55
+ let f = 0;
56
+ while (f <= 255) {
57
+ factors.push(f);
58
+ f += factor;
59
+ }
60
+ // RGB loop
61
+ const colors = [initColor];
62
+ for (const r of factors) {
63
+ for (const g of factors) {
64
+ for (const b of factors) {
65
+ const newColor = initColor.clone(r, g, b);
66
+ if (newColor)
67
+ colors.push(newColor);
68
+ }
69
+ }
70
+ }
71
+ return colors;
72
+ }
73
+ /**
74
+ * HEX string to integer value
75
+ * @param hex HEX string
76
+ * @returns Integer value
77
+ */
78
+ static hexTo(hex) {
79
+ return parseInt(hex, 16);
80
+ }
81
+ /**
82
+ * Format value to 16 radix string
83
+ * @param num Int value
84
+ * @returns Result
85
+ */
86
+ static toHex(num) {
87
+ return num.toString(16).padStart(2, '0');
88
+ }
89
+ /**
90
+ * Parse HTML color to EColor
91
+ * @param htmlColor HTML color
92
+ * @returns EColor
93
+ */
94
+ static parse(htmlColor) {
95
+ // Null
96
+ if (htmlColor == null)
97
+ return undefined;
98
+ htmlColor = htmlColor.toUpperCase();
99
+ // HEX color
100
+ if (htmlColor.startsWith('#')) {
101
+ htmlColor = htmlColor.substring(1);
102
+ if (htmlColor.length === 3)
103
+ htmlColor = Array.from(htmlColor)
104
+ .map((c) => c + c)
105
+ .join('');
106
+ if (htmlColor.length === 6) {
107
+ return new EColor(EColor.hexTo(htmlColor.substring(0, 2)), EColor.hexTo(htmlColor.substring(2, 4)), EColor.hexTo(htmlColor.substring(4, 6)));
108
+ }
109
+ return undefined;
110
+ }
111
+ // For RGB and RGBA
112
+ const reg = /^RGBA?\(([0-9,\s\.]+)\)$/;
113
+ const result = htmlColor.match(reg);
114
+ if (result != null && result.length == 2) {
115
+ const parts = result[1].split(/\s*,\s*/);
116
+ if (parts.length === 3 || parts.length === 4) {
117
+ const alpha = parts[3];
118
+ return new EColor(parseInt(parts[0]), parseInt(parts[1]), parseInt(parts[2]), alpha == null ? undefined : parseFloat(alpha));
119
+ }
120
+ }
121
+ return undefined;
122
+ }
123
+ /**
124
+ * Clone color with adjusts
125
+ * @param adjustR Adjust R value
126
+ * @param adjustG Adjust G value
127
+ * @param adjustB Adjust B value
128
+ * @param alpha New alpha value
129
+ */
130
+ clone(adjustR, adjustG, adjustB, alpha) {
131
+ const r = EColor.adjust(this.r, adjustR);
132
+ const g = EColor.adjust(this.g, adjustG);
133
+ const b = EColor.adjust(this.b, adjustB);
134
+ if (r === this.r &&
135
+ g === this.g &&
136
+ b === this.b &&
137
+ (alpha == null || alpha === this.alpha))
138
+ return undefined;
139
+ return new EColor(r, g, b, alpha);
140
+ }
141
+ /**
142
+ * To RGB color string
143
+ * @param includeAlpha Include alpha or not
144
+ * @returns RGB color string
145
+ */
146
+ toRGBColor(includeAlpha) {
147
+ var _a;
148
+ // Default case
149
+ includeAlpha !== null && includeAlpha !== void 0 ? includeAlpha : (includeAlpha = this.alpha != null);
150
+ if (includeAlpha)
151
+ return `RGBA(${this.r}, ${this.g}, ${this.b}, ${(_a = this.alpha) !== null && _a !== void 0 ? _a : 1})`;
152
+ return `RGB(${this.r}, ${this.g}, ${this.b})`;
153
+ }
154
+ /**
155
+ * To HEX color string
156
+ * @returns HEX color string
157
+ */
158
+ toHEXColor() {
159
+ return `#${EColor.toHex(this.r)}${EColor.toHex(this.g)}${EColor.toHex(this.b)}`;
160
+ }
161
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.14",
3
+ "version": "1.1.15",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -55,13 +55,13 @@
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
57
  "@types/jest": "^27.4.1",
58
- "@typescript-eslint/eslint-plugin": "^5.12.1",
59
- "@typescript-eslint/parser": "^5.12.1",
58
+ "@typescript-eslint/eslint-plugin": "^5.13.0",
59
+ "@typescript-eslint/parser": "^5.13.0",
60
60
  "eslint": "^8.10.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.25.4",
63
63
  "jest": "^27.5.1",
64
64
  "ts-jest": "^27.1.3",
65
- "typescript": "^4.5.5"
65
+ "typescript": "^4.6.2"
66
66
  }
67
67
  }
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
- export * from './types/FormData';
2
1
  export * from './types/DelayedExecutorType';
2
+ export * from './types/EColor';
3
+ export * from './types/FormData';
3
4
 
4
5
  export * from './storage/IStorage';
5
6
  export * from './storage/WindowStorage';
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Etsoo implmented Color
3
+ */
4
+ export class EColor {
5
+ /**
6
+ * Adjust value
7
+ * @param value Current value
8
+ * @param adjust Adjust value
9
+ * @returns Adjusted value
10
+ */
11
+ static adjust(value: number, adjust?: number) {
12
+ if (adjust == null) return value;
13
+ value = Math.abs(value + adjust);
14
+ if (value > 255) return value % 255;
15
+ return value;
16
+ }
17
+
18
+ /**
19
+ * Get HEX or RGB colors
20
+ * @param init Initial color
21
+ * @param factor Increase factor
22
+ * @param hex to HEX or not
23
+ * @returns Result
24
+ */
25
+ static getColors(init = '#000', factor: number = 51, hex: boolean = true) {
26
+ return EColor.getEColors(init, factor).map((c) =>
27
+ hex ? c.toHEXColor() : c.toRGBColor()
28
+ );
29
+ }
30
+
31
+ /**
32
+ * Get EColors
33
+ * @param init Initial color
34
+ * @param factor Increase factor
35
+ * @returns Result
36
+ */
37
+ static getEColors(init = '#000', factor: number = 51): EColor[] {
38
+ // Init color
39
+ const initColor = EColor.parse(init) ?? new EColor(0, 0, 0);
40
+
41
+ // Factors elements
42
+ // 51 = '00', '33', '66', '99', 'cc', 'ff'
43
+ const factors: number[] = [];
44
+ let f = 0;
45
+ while (f <= 255) {
46
+ factors.push(f);
47
+ f += factor;
48
+ }
49
+
50
+ // RGB loop
51
+ const colors: EColor[] = [initColor];
52
+
53
+ for (const r of factors) {
54
+ for (const g of factors) {
55
+ for (const b of factors) {
56
+ const newColor = initColor.clone(r, g, b);
57
+ if (newColor) colors.push(newColor);
58
+ }
59
+ }
60
+ }
61
+
62
+ return colors;
63
+ }
64
+
65
+ /**
66
+ * HEX string to integer value
67
+ * @param hex HEX string
68
+ * @returns Integer value
69
+ */
70
+ static hexTo(hex: string) {
71
+ return parseInt(hex, 16);
72
+ }
73
+
74
+ /**
75
+ * Format value to 16 radix string
76
+ * @param num Int value
77
+ * @returns Result
78
+ */
79
+ static toHex(num: number) {
80
+ return num.toString(16).padStart(2, '0');
81
+ }
82
+
83
+ /**
84
+ * Parse HTML color to EColor
85
+ * @param htmlColor HTML color
86
+ * @returns EColor
87
+ */
88
+ static parse(htmlColor?: string | null): EColor | undefined {
89
+ // Null
90
+ if (htmlColor == null) return undefined;
91
+ htmlColor = htmlColor.toUpperCase();
92
+
93
+ // HEX color
94
+ if (htmlColor.startsWith('#')) {
95
+ htmlColor = htmlColor.substring(1);
96
+ if (htmlColor.length === 3)
97
+ htmlColor = Array.from(htmlColor)
98
+ .map((c) => c + c)
99
+ .join('');
100
+
101
+ if (htmlColor.length === 6) {
102
+ return new EColor(
103
+ EColor.hexTo(htmlColor.substring(0, 2)),
104
+ EColor.hexTo(htmlColor.substring(2, 4)),
105
+ EColor.hexTo(htmlColor.substring(4, 6))
106
+ );
107
+ }
108
+
109
+ return undefined;
110
+ }
111
+
112
+ // For RGB and RGBA
113
+ const reg = /^RGBA?\(([0-9,\s\.]+)\)$/;
114
+ const result = htmlColor.match(reg);
115
+ if (result != null && result.length == 2) {
116
+ const parts = result[1].split(/\s*,\s*/);
117
+ if (parts.length === 3 || parts.length === 4) {
118
+ const alpha = parts[3];
119
+ return new EColor(
120
+ parseInt(parts[0]),
121
+ parseInt(parts[1]),
122
+ parseInt(parts[2]),
123
+ alpha == null ? undefined : parseFloat(alpha)
124
+ );
125
+ }
126
+ }
127
+
128
+ return undefined;
129
+ }
130
+
131
+ /**
132
+ * Constructor
133
+ * @param r Reg
134
+ * @param g Green
135
+ * @param b Blue
136
+ * @param alpha Alpha
137
+ */
138
+ constructor(
139
+ public readonly r: number,
140
+ public readonly g: number,
141
+ public readonly b: number,
142
+ public readonly alpha?: number
143
+ ) {}
144
+
145
+ /**
146
+ * Clone color with adjusts
147
+ * @param adjustR Adjust R value
148
+ * @param adjustG Adjust G value
149
+ * @param adjustB Adjust B value
150
+ * @param alpha New alpha value
151
+ */
152
+ clone(
153
+ adjustR?: number,
154
+ adjustG?: number,
155
+ adjustB?: number,
156
+ alpha?: number
157
+ ) {
158
+ const r = EColor.adjust(this.r, adjustR);
159
+ const g = EColor.adjust(this.g, adjustG);
160
+ const b = EColor.adjust(this.b, adjustB);
161
+ if (
162
+ r === this.r &&
163
+ g === this.g &&
164
+ b === this.b &&
165
+ (alpha == null || alpha === this.alpha)
166
+ )
167
+ return undefined;
168
+ return new EColor(r, g, b, alpha);
169
+ }
170
+
171
+ /**
172
+ * To RGB color string
173
+ * @param includeAlpha Include alpha or not
174
+ * @returns RGB color string
175
+ */
176
+ toRGBColor(includeAlpha?: boolean) {
177
+ // Default case
178
+ includeAlpha ??= this.alpha != null;
179
+
180
+ if (includeAlpha)
181
+ return `RGBA(${this.r}, ${this.g}, ${this.b}, ${this.alpha ?? 1})`;
182
+
183
+ return `RGB(${this.r}, ${this.g}, ${this.b})`;
184
+ }
185
+
186
+ /**
187
+ * To HEX color string
188
+ * @returns HEX color string
189
+ */
190
+ toHEXColor() {
191
+ return `#${EColor.toHex(this.r)}${EColor.toHex(this.g)}${EColor.toHex(
192
+ this.b
193
+ )}`;
194
+ }
195
+ }