@etsoo/shared 1.1.17 → 1.1.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.
- package/__tests__/{EColor.ts → ColorUtils.ts} +2 -1
- package/lib/cjs/ColorUtils.d.ts +23 -0
- package/lib/cjs/ColorUtils.js +80 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/types/EColor.d.ts +0 -17
- package/lib/cjs/types/EColor.js +1 -69
- package/lib/mjs/ColorUtils.d.ts +23 -0
- package/lib/mjs/ColorUtils.js +77 -0
- package/lib/mjs/index.d.ts +1 -0
- package/lib/mjs/index.js +1 -0
- package/lib/mjs/types/EColor.d.ts +0 -17
- package/lib/mjs/types/EColor.js +1 -69
- package/package.json +6 -6
- package/src/ColorUtils.ts +99 -0
- package/src/index.ts +1 -0
- package/src/types/EColor.ts +1 -94
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EColor } from '../src/types/EColor';
|
|
2
|
+
import { ColorUtils } from '../src/ColorUtils';
|
|
2
3
|
|
|
3
4
|
test('Tests for parse', () => {
|
|
4
5
|
// Arrange & act
|
|
@@ -13,7 +14,7 @@ test('Tests for parse', () => {
|
|
|
13
14
|
});
|
|
14
15
|
|
|
15
16
|
test('Tests for getColors', () => {
|
|
16
|
-
const colors =
|
|
17
|
+
const colors = ColorUtils.getColors(undefined, 128);
|
|
17
18
|
expect(colors.length).toBe(8);
|
|
18
19
|
});
|
|
19
20
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { EColor } from './types/EColor';
|
|
2
|
+
/**
|
|
3
|
+
* Color utils
|
|
4
|
+
*/
|
|
5
|
+
export declare namespace ColorUtils {
|
|
6
|
+
/**
|
|
7
|
+
* Get HEX or RGB colors
|
|
8
|
+
* @param init Initial color
|
|
9
|
+
* @param factor Increase factor
|
|
10
|
+
* @param adjustOrder Adjust order to increase difference
|
|
11
|
+
* @param hex to HEX or not
|
|
12
|
+
* @returns Result
|
|
13
|
+
*/
|
|
14
|
+
function getColors(init?: string, factor?: number, adjustOrder?: boolean, hex?: boolean): string[];
|
|
15
|
+
/**
|
|
16
|
+
* Get EColors
|
|
17
|
+
* @param init Initial color
|
|
18
|
+
* @param factor Increase factor
|
|
19
|
+
* @param adjustOrder Adjust order to increase difference
|
|
20
|
+
* @returns Result
|
|
21
|
+
*/
|
|
22
|
+
function getEColors(init?: string, factor?: number, adjustOrder?: boolean): EColor[];
|
|
23
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ColorUtils = void 0;
|
|
4
|
+
const EColor_1 = require("./types/EColor");
|
|
5
|
+
/**
|
|
6
|
+
* Color utils
|
|
7
|
+
*/
|
|
8
|
+
var ColorUtils;
|
|
9
|
+
(function (ColorUtils) {
|
|
10
|
+
/**
|
|
11
|
+
* Get HEX or RGB colors
|
|
12
|
+
* @param init Initial color
|
|
13
|
+
* @param factor Increase factor
|
|
14
|
+
* @param adjustOrder Adjust order to increase difference
|
|
15
|
+
* @param hex to HEX or not
|
|
16
|
+
* @returns Result
|
|
17
|
+
*/
|
|
18
|
+
function getColors(init = '#000', factor = 51, adjustOrder = true, hex = true) {
|
|
19
|
+
return getEColors(init, factor, adjustOrder).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
|
|
20
|
+
}
|
|
21
|
+
ColorUtils.getColors = getColors;
|
|
22
|
+
/**
|
|
23
|
+
* Get EColors
|
|
24
|
+
* @param init Initial color
|
|
25
|
+
* @param factor Increase factor
|
|
26
|
+
* @param adjustOrder Adjust order to increase difference
|
|
27
|
+
* @returns Result
|
|
28
|
+
*/
|
|
29
|
+
function getEColors(init = '#000', factor = 51, adjustOrder = true) {
|
|
30
|
+
var _a;
|
|
31
|
+
// Init color
|
|
32
|
+
const initColor = (_a = EColor_1.EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor_1.EColor(0, 0, 0);
|
|
33
|
+
// Factors elements
|
|
34
|
+
// 51 = '00', '33', '66', '99', 'cc', 'ff'
|
|
35
|
+
const factors = [];
|
|
36
|
+
let f = 0;
|
|
37
|
+
while (f <= 255) {
|
|
38
|
+
factors.push(f);
|
|
39
|
+
f += factor;
|
|
40
|
+
}
|
|
41
|
+
// RGB loop
|
|
42
|
+
const colors = [initColor];
|
|
43
|
+
for (const r of factors) {
|
|
44
|
+
for (const g of factors) {
|
|
45
|
+
for (const b of factors) {
|
|
46
|
+
colors.push(initColor.clone(r, g, b));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// Non-nullable colors
|
|
51
|
+
const nColors = colors.filter((color) => color != null);
|
|
52
|
+
// Adjust order
|
|
53
|
+
if (adjustOrder) {
|
|
54
|
+
const firstColor = nColors.shift();
|
|
55
|
+
if (firstColor) {
|
|
56
|
+
let color = firstColor;
|
|
57
|
+
const newColors = [color];
|
|
58
|
+
while (nColors.length > 0) {
|
|
59
|
+
const result = nColors.reduce((p, c, index) => {
|
|
60
|
+
const delta = color.getDeltaValue(c);
|
|
61
|
+
if (delta != null && delta > p.delta) {
|
|
62
|
+
p.delta = delta;
|
|
63
|
+
p.color = c;
|
|
64
|
+
p.index = index;
|
|
65
|
+
}
|
|
66
|
+
return p;
|
|
67
|
+
}, { delta: 0, color, index: -1 });
|
|
68
|
+
if (result.delta > 0) {
|
|
69
|
+
color = result.color;
|
|
70
|
+
newColors.push(color);
|
|
71
|
+
nColors.splice(result.index, 1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return newColors;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return nColors;
|
|
78
|
+
}
|
|
79
|
+
ColorUtils.getEColors = getEColors;
|
|
80
|
+
})(ColorUtils = exports.ColorUtils || (exports.ColorUtils = {}));
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export * from './types/FormData';
|
|
|
4
4
|
export * from './storage/IStorage';
|
|
5
5
|
export * from './storage/WindowStorage';
|
|
6
6
|
export * from './DataTypes';
|
|
7
|
+
export * from './ColorUtils';
|
|
7
8
|
export * from './DateUtils';
|
|
8
9
|
export * from './DomUtils';
|
|
9
10
|
export * from './ExtendUtils';
|
package/lib/cjs/index.js
CHANGED
|
@@ -20,6 +20,7 @@ __exportStar(require("./types/FormData"), exports);
|
|
|
20
20
|
__exportStar(require("./storage/IStorage"), exports);
|
|
21
21
|
__exportStar(require("./storage/WindowStorage"), exports);
|
|
22
22
|
__exportStar(require("./DataTypes"), exports);
|
|
23
|
+
__exportStar(require("./ColorUtils"), exports);
|
|
23
24
|
__exportStar(require("./DateUtils"), exports);
|
|
24
25
|
__exportStar(require("./DomUtils"), exports);
|
|
25
26
|
__exportStar(require("./ExtendUtils"), exports);
|
|
@@ -13,23 +13,6 @@ export declare class EColor {
|
|
|
13
13
|
* @returns Adjusted value
|
|
14
14
|
*/
|
|
15
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 adjustOrder Adjust order to increase difference
|
|
21
|
-
* @param hex to HEX or not
|
|
22
|
-
* @returns Result
|
|
23
|
-
*/
|
|
24
|
-
static getColors(init?: string, factor?: number, adjustOrder?: boolean, hex?: boolean): string[];
|
|
25
|
-
/**
|
|
26
|
-
* Get EColors
|
|
27
|
-
* @param init Initial color
|
|
28
|
-
* @param factor Increase factor
|
|
29
|
-
* @param adjustOrder Adjust order to increase difference
|
|
30
|
-
* @returns Result
|
|
31
|
-
*/
|
|
32
|
-
static getEColors(init?: string, factor?: number, adjustOrder?: boolean): EColor[];
|
|
33
16
|
/**
|
|
34
17
|
* HEX string to integer value
|
|
35
18
|
* @param hex HEX string
|
package/lib/cjs/types/EColor.js
CHANGED
|
@@ -32,74 +32,6 @@ class EColor {
|
|
|
32
32
|
return value % 255;
|
|
33
33
|
return value;
|
|
34
34
|
}
|
|
35
|
-
/**
|
|
36
|
-
* Get HEX or RGB colors
|
|
37
|
-
* @param init Initial color
|
|
38
|
-
* @param factor Increase factor
|
|
39
|
-
* @param adjustOrder Adjust order to increase difference
|
|
40
|
-
* @param hex to HEX or not
|
|
41
|
-
* @returns Result
|
|
42
|
-
*/
|
|
43
|
-
static getColors(init = '#000', factor = 51, adjustOrder = true, hex = true) {
|
|
44
|
-
return EColor.getEColors(init, factor, adjustOrder).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Get EColors
|
|
48
|
-
* @param init Initial color
|
|
49
|
-
* @param factor Increase factor
|
|
50
|
-
* @param adjustOrder Adjust order to increase difference
|
|
51
|
-
* @returns Result
|
|
52
|
-
*/
|
|
53
|
-
static getEColors(init = '#000', factor = 51, adjustOrder = true) {
|
|
54
|
-
var _a;
|
|
55
|
-
// Init color
|
|
56
|
-
const initColor = (_a = EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor(0, 0, 0);
|
|
57
|
-
// Factors elements
|
|
58
|
-
// 51 = '00', '33', '66', '99', 'cc', 'ff'
|
|
59
|
-
const factors = [];
|
|
60
|
-
let f = 0;
|
|
61
|
-
while (f <= 255) {
|
|
62
|
-
factors.push(f);
|
|
63
|
-
f += factor;
|
|
64
|
-
}
|
|
65
|
-
// RGB loop
|
|
66
|
-
const colors = [initColor];
|
|
67
|
-
for (const r of factors) {
|
|
68
|
-
for (const g of factors) {
|
|
69
|
-
for (const b of factors) {
|
|
70
|
-
colors.push(initColor.clone(r, g, b));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// Non-nullable colors
|
|
75
|
-
const nColors = colors.filter((color) => color != null);
|
|
76
|
-
// Adjust order
|
|
77
|
-
if (adjustOrder) {
|
|
78
|
-
const firstColor = nColors.shift();
|
|
79
|
-
if (firstColor) {
|
|
80
|
-
let color = firstColor;
|
|
81
|
-
const newColors = [color];
|
|
82
|
-
while (nColors.length > 0) {
|
|
83
|
-
const result = nColors.reduce((p, c, index) => {
|
|
84
|
-
const delta = color.getDeltaValue(c);
|
|
85
|
-
if (delta != null && delta > p.delta) {
|
|
86
|
-
p.delta = delta;
|
|
87
|
-
p.color = c;
|
|
88
|
-
p.index = index;
|
|
89
|
-
}
|
|
90
|
-
return p;
|
|
91
|
-
}, { delta: 0, color, index: -1 });
|
|
92
|
-
if (result.delta > 0) {
|
|
93
|
-
color = result.color;
|
|
94
|
-
newColors.push(color);
|
|
95
|
-
nColors.splice(result.index, 1);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return newColors;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return nColors;
|
|
102
|
-
}
|
|
103
35
|
/**
|
|
104
36
|
* HEX string to integer value
|
|
105
37
|
* @param hex HEX string
|
|
@@ -125,7 +57,7 @@ class EColor {
|
|
|
125
57
|
// Null
|
|
126
58
|
if (htmlColor == null)
|
|
127
59
|
return undefined;
|
|
128
|
-
htmlColor = htmlColor.toUpperCase();
|
|
60
|
+
htmlColor = htmlColor.trim().toUpperCase();
|
|
129
61
|
// HEX color
|
|
130
62
|
if (htmlColor.startsWith('#')) {
|
|
131
63
|
htmlColor = htmlColor.substring(1);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { EColor } from './types/EColor';
|
|
2
|
+
/**
|
|
3
|
+
* Color utils
|
|
4
|
+
*/
|
|
5
|
+
export declare namespace ColorUtils {
|
|
6
|
+
/**
|
|
7
|
+
* Get HEX or RGB colors
|
|
8
|
+
* @param init Initial color
|
|
9
|
+
* @param factor Increase factor
|
|
10
|
+
* @param adjustOrder Adjust order to increase difference
|
|
11
|
+
* @param hex to HEX or not
|
|
12
|
+
* @returns Result
|
|
13
|
+
*/
|
|
14
|
+
function getColors(init?: string, factor?: number, adjustOrder?: boolean, hex?: boolean): string[];
|
|
15
|
+
/**
|
|
16
|
+
* Get EColors
|
|
17
|
+
* @param init Initial color
|
|
18
|
+
* @param factor Increase factor
|
|
19
|
+
* @param adjustOrder Adjust order to increase difference
|
|
20
|
+
* @returns Result
|
|
21
|
+
*/
|
|
22
|
+
function getEColors(init?: string, factor?: number, adjustOrder?: boolean): EColor[];
|
|
23
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { EColor } from './types/EColor';
|
|
2
|
+
/**
|
|
3
|
+
* Color utils
|
|
4
|
+
*/
|
|
5
|
+
export var ColorUtils;
|
|
6
|
+
(function (ColorUtils) {
|
|
7
|
+
/**
|
|
8
|
+
* Get HEX or RGB colors
|
|
9
|
+
* @param init Initial color
|
|
10
|
+
* @param factor Increase factor
|
|
11
|
+
* @param adjustOrder Adjust order to increase difference
|
|
12
|
+
* @param hex to HEX or not
|
|
13
|
+
* @returns Result
|
|
14
|
+
*/
|
|
15
|
+
function getColors(init = '#000', factor = 51, adjustOrder = true, hex = true) {
|
|
16
|
+
return getEColors(init, factor, adjustOrder).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
|
|
17
|
+
}
|
|
18
|
+
ColorUtils.getColors = getColors;
|
|
19
|
+
/**
|
|
20
|
+
* Get EColors
|
|
21
|
+
* @param init Initial color
|
|
22
|
+
* @param factor Increase factor
|
|
23
|
+
* @param adjustOrder Adjust order to increase difference
|
|
24
|
+
* @returns Result
|
|
25
|
+
*/
|
|
26
|
+
function getEColors(init = '#000', factor = 51, adjustOrder = true) {
|
|
27
|
+
var _a;
|
|
28
|
+
// Init color
|
|
29
|
+
const initColor = (_a = EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor(0, 0, 0);
|
|
30
|
+
// Factors elements
|
|
31
|
+
// 51 = '00', '33', '66', '99', 'cc', 'ff'
|
|
32
|
+
const factors = [];
|
|
33
|
+
let f = 0;
|
|
34
|
+
while (f <= 255) {
|
|
35
|
+
factors.push(f);
|
|
36
|
+
f += factor;
|
|
37
|
+
}
|
|
38
|
+
// RGB loop
|
|
39
|
+
const colors = [initColor];
|
|
40
|
+
for (const r of factors) {
|
|
41
|
+
for (const g of factors) {
|
|
42
|
+
for (const b of factors) {
|
|
43
|
+
colors.push(initColor.clone(r, g, b));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// Non-nullable colors
|
|
48
|
+
const nColors = colors.filter((color) => color != null);
|
|
49
|
+
// Adjust order
|
|
50
|
+
if (adjustOrder) {
|
|
51
|
+
const firstColor = nColors.shift();
|
|
52
|
+
if (firstColor) {
|
|
53
|
+
let color = firstColor;
|
|
54
|
+
const newColors = [color];
|
|
55
|
+
while (nColors.length > 0) {
|
|
56
|
+
const result = nColors.reduce((p, c, index) => {
|
|
57
|
+
const delta = color.getDeltaValue(c);
|
|
58
|
+
if (delta != null && delta > p.delta) {
|
|
59
|
+
p.delta = delta;
|
|
60
|
+
p.color = c;
|
|
61
|
+
p.index = index;
|
|
62
|
+
}
|
|
63
|
+
return p;
|
|
64
|
+
}, { delta: 0, color, index: -1 });
|
|
65
|
+
if (result.delta > 0) {
|
|
66
|
+
color = result.color;
|
|
67
|
+
newColors.push(color);
|
|
68
|
+
nColors.splice(result.index, 1);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return newColors;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return nColors;
|
|
75
|
+
}
|
|
76
|
+
ColorUtils.getEColors = getEColors;
|
|
77
|
+
})(ColorUtils || (ColorUtils = {}));
|
package/lib/mjs/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export * from './types/FormData';
|
|
|
4
4
|
export * from './storage/IStorage';
|
|
5
5
|
export * from './storage/WindowStorage';
|
|
6
6
|
export * from './DataTypes';
|
|
7
|
+
export * from './ColorUtils';
|
|
7
8
|
export * from './DateUtils';
|
|
8
9
|
export * from './DomUtils';
|
|
9
10
|
export * from './ExtendUtils';
|
package/lib/mjs/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export * from './types/FormData';
|
|
|
4
4
|
export * from './storage/IStorage';
|
|
5
5
|
export * from './storage/WindowStorage';
|
|
6
6
|
export * from './DataTypes';
|
|
7
|
+
export * from './ColorUtils';
|
|
7
8
|
export * from './DateUtils';
|
|
8
9
|
export * from './DomUtils';
|
|
9
10
|
export * from './ExtendUtils';
|
|
@@ -13,23 +13,6 @@ export declare class EColor {
|
|
|
13
13
|
* @returns Adjusted value
|
|
14
14
|
*/
|
|
15
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 adjustOrder Adjust order to increase difference
|
|
21
|
-
* @param hex to HEX or not
|
|
22
|
-
* @returns Result
|
|
23
|
-
*/
|
|
24
|
-
static getColors(init?: string, factor?: number, adjustOrder?: boolean, hex?: boolean): string[];
|
|
25
|
-
/**
|
|
26
|
-
* Get EColors
|
|
27
|
-
* @param init Initial color
|
|
28
|
-
* @param factor Increase factor
|
|
29
|
-
* @param adjustOrder Adjust order to increase difference
|
|
30
|
-
* @returns Result
|
|
31
|
-
*/
|
|
32
|
-
static getEColors(init?: string, factor?: number, adjustOrder?: boolean): EColor[];
|
|
33
16
|
/**
|
|
34
17
|
* HEX string to integer value
|
|
35
18
|
* @param hex HEX string
|
package/lib/mjs/types/EColor.js
CHANGED
|
@@ -29,74 +29,6 @@ export class EColor {
|
|
|
29
29
|
return value % 255;
|
|
30
30
|
return value;
|
|
31
31
|
}
|
|
32
|
-
/**
|
|
33
|
-
* Get HEX or RGB colors
|
|
34
|
-
* @param init Initial color
|
|
35
|
-
* @param factor Increase factor
|
|
36
|
-
* @param adjustOrder Adjust order to increase difference
|
|
37
|
-
* @param hex to HEX or not
|
|
38
|
-
* @returns Result
|
|
39
|
-
*/
|
|
40
|
-
static getColors(init = '#000', factor = 51, adjustOrder = true, hex = true) {
|
|
41
|
-
return EColor.getEColors(init, factor, adjustOrder).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Get EColors
|
|
45
|
-
* @param init Initial color
|
|
46
|
-
* @param factor Increase factor
|
|
47
|
-
* @param adjustOrder Adjust order to increase difference
|
|
48
|
-
* @returns Result
|
|
49
|
-
*/
|
|
50
|
-
static getEColors(init = '#000', factor = 51, adjustOrder = true) {
|
|
51
|
-
var _a;
|
|
52
|
-
// Init color
|
|
53
|
-
const initColor = (_a = EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor(0, 0, 0);
|
|
54
|
-
// Factors elements
|
|
55
|
-
// 51 = '00', '33', '66', '99', 'cc', 'ff'
|
|
56
|
-
const factors = [];
|
|
57
|
-
let f = 0;
|
|
58
|
-
while (f <= 255) {
|
|
59
|
-
factors.push(f);
|
|
60
|
-
f += factor;
|
|
61
|
-
}
|
|
62
|
-
// RGB loop
|
|
63
|
-
const colors = [initColor];
|
|
64
|
-
for (const r of factors) {
|
|
65
|
-
for (const g of factors) {
|
|
66
|
-
for (const b of factors) {
|
|
67
|
-
colors.push(initColor.clone(r, g, b));
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
// Non-nullable colors
|
|
72
|
-
const nColors = colors.filter((color) => color != null);
|
|
73
|
-
// Adjust order
|
|
74
|
-
if (adjustOrder) {
|
|
75
|
-
const firstColor = nColors.shift();
|
|
76
|
-
if (firstColor) {
|
|
77
|
-
let color = firstColor;
|
|
78
|
-
const newColors = [color];
|
|
79
|
-
while (nColors.length > 0) {
|
|
80
|
-
const result = nColors.reduce((p, c, index) => {
|
|
81
|
-
const delta = color.getDeltaValue(c);
|
|
82
|
-
if (delta != null && delta > p.delta) {
|
|
83
|
-
p.delta = delta;
|
|
84
|
-
p.color = c;
|
|
85
|
-
p.index = index;
|
|
86
|
-
}
|
|
87
|
-
return p;
|
|
88
|
-
}, { delta: 0, color, index: -1 });
|
|
89
|
-
if (result.delta > 0) {
|
|
90
|
-
color = result.color;
|
|
91
|
-
newColors.push(color);
|
|
92
|
-
nColors.splice(result.index, 1);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return newColors;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return nColors;
|
|
99
|
-
}
|
|
100
32
|
/**
|
|
101
33
|
* HEX string to integer value
|
|
102
34
|
* @param hex HEX string
|
|
@@ -122,7 +54,7 @@ export class EColor {
|
|
|
122
54
|
// Null
|
|
123
55
|
if (htmlColor == null)
|
|
124
56
|
return undefined;
|
|
125
|
-
htmlColor = htmlColor.toUpperCase();
|
|
57
|
+
htmlColor = htmlColor.trim().toUpperCase();
|
|
126
58
|
// HEX color
|
|
127
59
|
if (htmlColor.startsWith('#')) {
|
|
128
60
|
htmlColor = htmlColor.substring(1);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.18",
|
|
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.
|
|
59
|
-
"@typescript-eslint/parser": "^5.
|
|
60
|
-
"eslint": "^8.
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.16.0",
|
|
59
|
+
"@typescript-eslint/parser": "^5.16.0",
|
|
60
|
+
"eslint": "^8.11.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
|
-
"ts-jest": "^27.1.
|
|
65
|
-
"typescript": "^4.6.
|
|
64
|
+
"ts-jest": "^27.1.4",
|
|
65
|
+
"typescript": "^4.6.3"
|
|
66
66
|
}
|
|
67
67
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { EColor } from './types/EColor';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Color utils
|
|
5
|
+
*/
|
|
6
|
+
export namespace ColorUtils {
|
|
7
|
+
/**
|
|
8
|
+
* Get HEX or RGB colors
|
|
9
|
+
* @param init Initial color
|
|
10
|
+
* @param factor Increase factor
|
|
11
|
+
* @param adjustOrder Adjust order to increase difference
|
|
12
|
+
* @param hex to HEX or not
|
|
13
|
+
* @returns Result
|
|
14
|
+
*/
|
|
15
|
+
export function getColors(
|
|
16
|
+
init: string = '#000',
|
|
17
|
+
factor: number = 51,
|
|
18
|
+
adjustOrder: boolean = true,
|
|
19
|
+
hex: boolean = true
|
|
20
|
+
) {
|
|
21
|
+
return getEColors(init, factor, adjustOrder).map((c) =>
|
|
22
|
+
hex ? c.toHEXColor() : c.toRGBColor()
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get EColors
|
|
28
|
+
* @param init Initial color
|
|
29
|
+
* @param factor Increase factor
|
|
30
|
+
* @param adjustOrder Adjust order to increase difference
|
|
31
|
+
* @returns Result
|
|
32
|
+
*/
|
|
33
|
+
export function getEColors(
|
|
34
|
+
init: string = '#000',
|
|
35
|
+
factor: number = 51,
|
|
36
|
+
adjustOrder: boolean = true
|
|
37
|
+
): 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 | undefined)[] = [initColor];
|
|
52
|
+
for (const r of factors) {
|
|
53
|
+
for (const g of factors) {
|
|
54
|
+
for (const b of factors) {
|
|
55
|
+
colors.push(initColor.clone(r, g, b));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Non-nullable colors
|
|
61
|
+
const nColors = colors.filter(
|
|
62
|
+
(color): color is EColor => color != null
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// Adjust order
|
|
66
|
+
if (adjustOrder) {
|
|
67
|
+
const firstColor = nColors.shift();
|
|
68
|
+
if (firstColor) {
|
|
69
|
+
let color = firstColor;
|
|
70
|
+
const newColors: EColor[] = [color];
|
|
71
|
+
|
|
72
|
+
while (nColors.length > 0) {
|
|
73
|
+
const result = nColors.reduce(
|
|
74
|
+
(p, c, index) => {
|
|
75
|
+
const delta = color.getDeltaValue(c);
|
|
76
|
+
if (delta != null && delta > p.delta) {
|
|
77
|
+
p.delta = delta;
|
|
78
|
+
p.color = c;
|
|
79
|
+
p.index = index;
|
|
80
|
+
}
|
|
81
|
+
return p;
|
|
82
|
+
},
|
|
83
|
+
{ delta: 0, color, index: -1 }
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
if (result.delta > 0) {
|
|
87
|
+
color = result.color;
|
|
88
|
+
newColors.push(color);
|
|
89
|
+
nColors.splice(result.index, 1);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return newColors;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return nColors;
|
|
98
|
+
}
|
|
99
|
+
}
|
package/src/index.ts
CHANGED
package/src/types/EColor.ts
CHANGED
|
@@ -15,99 +15,6 @@ export class EColor {
|
|
|
15
15
|
return value;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
/**
|
|
19
|
-
* Get HEX or RGB colors
|
|
20
|
-
* @param init Initial color
|
|
21
|
-
* @param factor Increase factor
|
|
22
|
-
* @param adjustOrder Adjust order to increase difference
|
|
23
|
-
* @param hex to HEX or not
|
|
24
|
-
* @returns Result
|
|
25
|
-
*/
|
|
26
|
-
static getColors(
|
|
27
|
-
init: string = '#000',
|
|
28
|
-
factor: number = 51,
|
|
29
|
-
adjustOrder: boolean = true,
|
|
30
|
-
hex: boolean = true
|
|
31
|
-
) {
|
|
32
|
-
return EColor.getEColors(init, factor, adjustOrder).map((c) =>
|
|
33
|
-
hex ? c.toHEXColor() : c.toRGBColor()
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Get EColors
|
|
39
|
-
* @param init Initial color
|
|
40
|
-
* @param factor Increase factor
|
|
41
|
-
* @param adjustOrder Adjust order to increase difference
|
|
42
|
-
* @returns Result
|
|
43
|
-
*/
|
|
44
|
-
static getEColors(
|
|
45
|
-
init: string = '#000',
|
|
46
|
-
factor: number = 51,
|
|
47
|
-
adjustOrder: boolean = true
|
|
48
|
-
): EColor[] {
|
|
49
|
-
// Init color
|
|
50
|
-
const initColor = EColor.parse(init) ?? new EColor(0, 0, 0);
|
|
51
|
-
|
|
52
|
-
// Factors elements
|
|
53
|
-
// 51 = '00', '33', '66', '99', 'cc', 'ff'
|
|
54
|
-
const factors: number[] = [];
|
|
55
|
-
let f = 0;
|
|
56
|
-
while (f <= 255) {
|
|
57
|
-
factors.push(f);
|
|
58
|
-
f += factor;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// RGB loop
|
|
62
|
-
const colors: (EColor | undefined)[] = [initColor];
|
|
63
|
-
for (const r of factors) {
|
|
64
|
-
for (const g of factors) {
|
|
65
|
-
for (const b of factors) {
|
|
66
|
-
colors.push(initColor.clone(r, g, b));
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Non-nullable colors
|
|
72
|
-
const nColors = colors.filter(
|
|
73
|
-
(color): color is EColor => color != null
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
// Adjust order
|
|
77
|
-
if (adjustOrder) {
|
|
78
|
-
const firstColor = nColors.shift();
|
|
79
|
-
if (firstColor) {
|
|
80
|
-
let color = firstColor;
|
|
81
|
-
const newColors: EColor[] = [color];
|
|
82
|
-
|
|
83
|
-
while (nColors.length > 0) {
|
|
84
|
-
const result = nColors.reduce(
|
|
85
|
-
(p, c, index) => {
|
|
86
|
-
const delta = color.getDeltaValue(c);
|
|
87
|
-
if (delta != null && delta > p.delta) {
|
|
88
|
-
p.delta = delta;
|
|
89
|
-
p.color = c;
|
|
90
|
-
p.index = index;
|
|
91
|
-
}
|
|
92
|
-
return p;
|
|
93
|
-
},
|
|
94
|
-
{ delta: 0, color, index: -1 }
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
if (result.delta > 0) {
|
|
98
|
-
color = result.color;
|
|
99
|
-
newColors.push(color);
|
|
100
|
-
nColors.splice(result.index, 1);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return newColors;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return nColors;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
18
|
/**
|
|
112
19
|
* HEX string to integer value
|
|
113
20
|
* @param hex HEX string
|
|
@@ -134,7 +41,7 @@ export class EColor {
|
|
|
134
41
|
static parse(htmlColor?: string | null): EColor | undefined {
|
|
135
42
|
// Null
|
|
136
43
|
if (htmlColor == null) return undefined;
|
|
137
|
-
htmlColor = htmlColor.toUpperCase();
|
|
44
|
+
htmlColor = htmlColor.trim().toUpperCase();
|
|
138
45
|
|
|
139
46
|
// HEX color
|
|
140
47
|
if (htmlColor.startsWith('#')) {
|