ismx-nexo-node-app 0.4.17 → 0.4.19
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/dist/js/api/utils/ColorUtils.js +47 -3
- package/dist/js/repository/RepositoryDatabasePostgres.js +9 -5
- package/dist/types/api/utils/ColorUtils.d.ts +9 -1
- package/dist/types/repository/RepositoryDatabasePostgres.d.ts +1 -3
- package/package.json +1 -1
- package/src/main/node/api/utils/ColorUtils.ts +58 -3
- package/src/main/node/repository/RepositoryDatabasePostgres.ts +10 -6
|
@@ -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 (
|
|
6
|
-
|
|
7
|
-
|
|
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;
|
|
@@ -152,11 +152,15 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
152
152
|
throw new Error(`table ${tableName} does not exist`);
|
|
153
153
|
let table = this.tables[tableName];
|
|
154
154
|
let schema = table[0].schema;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
155
|
+
const columns = PostgresUtils_1.default.columns(table).filter(column => {
|
|
156
|
+
const camelKey = PostgresUtils_1.default.stringToCamel(column);
|
|
157
|
+
return Object.prototype.hasOwnProperty.call(object, camelKey);
|
|
158
|
+
});
|
|
159
|
+
if (columns.length === 0)
|
|
160
|
+
throw new Error(`No valid columns provided to update for table ${tableName}`);
|
|
161
|
+
const params = columns.map((_, index) => `\$${index + 2}`).join(",");
|
|
162
|
+
const values = [id, ...columns.map((column) => object[PostgresUtils_1.default.stringToCamel(column)])];
|
|
163
|
+
const query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 RETURNING *`;
|
|
160
164
|
return this.query(query, values).then((result) => result[0]);
|
|
161
165
|
});
|
|
162
166
|
}
|
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
export default abstract class ColorUtils {
|
|
2
|
-
static
|
|
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
|
}
|
|
@@ -36,9 +36,7 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase {
|
|
|
36
36
|
addAll<T>(tableName: string, objects: ({
|
|
37
37
|
[key: string]: Primitive;
|
|
38
38
|
} | any)[]): Promise<T[]>;
|
|
39
|
-
update<T>(tableName: string, id: string, object:
|
|
40
|
-
[key: string]: Primitive;
|
|
41
|
-
} | any): Promise<T>;
|
|
39
|
+
update<T>(tableName: string, id: string, object: Partial<T>): Promise<T>;
|
|
42
40
|
page<T>(tableName: string, sortKey: string, maxResults?: number, pageNumber?: number, filters?: {
|
|
43
41
|
[key: string]: Primitive | Array<Primitive>;
|
|
44
42
|
}): Promise<Pagination<T>>;
|
package/package.json
CHANGED
|
@@ -1,8 +1,63 @@
|
|
|
1
1
|
export default abstract class ColorUtils {
|
|
2
2
|
|
|
3
|
-
static
|
|
4
|
-
|
|
5
|
-
|
|
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
|
}
|
|
@@ -120,18 +120,22 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
120
120
|
return this.query(query, values);
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
async update<T>(tableName: string, id: string, object:
|
|
123
|
+
async update<T>(tableName: string, id: string, object: Partial<T>): Promise<T>
|
|
124
124
|
{
|
|
125
125
|
if (!id) throw new Error(`field 'id' is mandatory when updating ${tableName}`);
|
|
126
126
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
127
127
|
let table = this.tables[tableName];
|
|
128
128
|
let schema = table[0].schema;
|
|
129
|
-
|
|
129
|
+
const columns = PostgresUtils.columns(table).filter(column => {
|
|
130
|
+
const camelKey = PostgresUtils.stringToCamel(column);
|
|
131
|
+
return Object.prototype.hasOwnProperty.call(object, camelKey);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
if (columns.length === 0) throw new Error(`No valid columns provided to update for table ${tableName}`);
|
|
130
135
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
query += `RETURNING *`;
|
|
136
|
+
const params = columns.map((_, index) => `\$${index + 2}`).join(",");
|
|
137
|
+
const values = [ id, ...columns.map((column) => object[PostgresUtils.stringToCamel(column) as keyof T]) ];
|
|
138
|
+
const query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 RETURNING *`;
|
|
135
139
|
|
|
136
140
|
return this.query<T>(query, values).then((result) => result[0]);
|
|
137
141
|
}
|