@descope/web-components-ui 1.0.287 → 1.0.288

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@descope/web-components-ui",
3
- "version": "1.0.287",
3
+ "version": "1.0.288",
4
4
  "description": "",
5
5
  "main": "dist/cjs/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
@@ -1,26 +1,86 @@
1
1
  import Color from 'color';
2
2
 
3
- // TODO: fix generated colors strategy
4
- const genDark = (c, percentage = 0.5) => c.darken(percentage).hex();
5
- const genLight = (c, percentage = 0.5) => c.lighten(percentage).hex();
6
- const genContrast = (c, percentage = 0.9) => {
3
+ const colorGaps = {
4
+ darkLight: 0.4,
5
+ highlight: 0.8,
6
+ contrast: 1,
7
+ edgeColor: {
8
+ darkLight: 0.25,
9
+ highlight: 0.1,
10
+ },
11
+ };
12
+
13
+ const darken = (c, percentage) => c.darken(percentage).hex();
14
+
15
+ const contrast = (c) => {
7
16
  const isDark = c.isDark();
8
17
  return c
9
- .mix(Color(isDark ? 'white' : 'black'), percentage)
18
+ .mix(Color(isDark ? 'white' : 'black'), colorGaps.contrast)
10
19
  .saturate(1)
11
20
  .hex();
12
21
  };
13
22
 
14
- export const genColor = (color) => {
23
+ const lighten = (c, percentage) => {
24
+ const isDark = c.lightness() < 0.5;
25
+
26
+ if (isDark) {
27
+ return c.lightness(percentage * 100).hex();
28
+ }
29
+
30
+ return c.lighten(percentage).hex();
31
+ };
32
+
33
+ const isNearBlack = (color) => color.luminosity() < 0.01;
34
+ const isNearWhite = (color) => color.luminosity() > 0.99;
35
+
36
+ const generateDarkColor = (color, theme) => {
37
+ if (theme === 'dark') {
38
+ return isNearWhite(color)
39
+ ? darken(color, colorGaps.edgeColor.darkLight)
40
+ : lighten(color, colorGaps.darkLight);
41
+ }
42
+
43
+ return isNearBlack(color)
44
+ ? lighten(color, colorGaps.edgeColor.darkLight)
45
+ : darken(color, colorGaps.darkLight);
46
+ };
47
+
48
+ const generateLightColor = (color, theme) => {
49
+ if (theme === 'dark') {
50
+ return isNearBlack(color)
51
+ ? lighten(color, colorGaps.edgeColor.darkLight)
52
+ : darken(color, colorGaps.darkLight);
53
+ }
54
+
55
+ return isNearWhite(color)
56
+ ? darken(color, colorGaps.edgeColor.darkLight)
57
+ : lighten(color, colorGaps.darkLight);
58
+ };
59
+
60
+ const generateHighlightColor = (color, theme) => {
61
+ if (theme === 'dark') {
62
+ return isNearBlack(color)
63
+ ? lighten(color, colorGaps.edgeColor.highlight)
64
+ : darken(color, colorGaps.highlight);
65
+ }
66
+
67
+ return isNearWhite(color)
68
+ ? darken(color, colorGaps.edgeColor.highlight)
69
+ : lighten(color, colorGaps.highlight);
70
+ };
71
+
72
+ export const genColor = (color, theme) => {
15
73
  const mainColor = new Color(color.main || color);
16
74
 
17
- return {
75
+ const res = {
18
76
  main: mainColor.hex(),
19
- dark: color.dark || genDark(mainColor),
20
- light: color.light || genLight(mainColor),
21
- highlight: color.highlight || genLight(mainColor),
22
- contrast: color.contrast || genContrast(mainColor),
77
+ dark: color.dark || generateDarkColor(mainColor, theme),
78
+ light: color.light || generateLightColor(mainColor, theme),
79
+ highlight: color.highlight || generateHighlightColor(mainColor, theme),
80
+ contrast: color.contrast || contrast(mainColor),
23
81
  };
82
+
83
+ return res;
24
84
  };
25
85
 
26
86
  export const genColors = (colors) => {