@codeleap/styles 5.6.3 → 5.7.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/styles",
3
- "version": "5.6.3",
3
+ "version": "5.7.0",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -9,7 +9,7 @@
9
9
  "directory": "packages/styles"
10
10
  },
11
11
  "devDependencies": {
12
- "@codeleap/config": "5.6.3",
12
+ "@codeleap/config": "5.7.0",
13
13
  "ts-node-dev": "^1.1.8"
14
14
  },
15
15
  "scripts": {
package/package.json.bak CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/styles",
3
- "version": "5.6.3",
3
+ "version": "5.7.0",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -22,7 +22,7 @@ export function hexToHSL(hex: string) {
22
22
  return {
23
23
  h: Math.round(h * 360),
24
24
  s: Math.round(s * 100),
25
- l: Math.round(l * 100)
25
+ l: Math.round(l * 100),
26
26
  }
27
27
  }
28
28
 
@@ -32,8 +32,7 @@ export function hslToHex(h: number, s: number, l: number): string {
32
32
 
33
33
  const k = (n: number) => (n + h / 30) % 12
34
34
  const a = s * Math.min(l, 1 - l)
35
- const f = (n: number) =>
36
- Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))))
35
+ const f = (n: number) => Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))))
37
36
 
38
37
  return `#${[f(0), f(8), f(4)].map(x => x.toString(16).padStart(2, '0')).join('')}`
39
38
  }
@@ -51,40 +50,53 @@ export function hslToRGB(h: number, s: number, l: number) {
51
50
 
52
51
  const k = (n: number) => (n + h / 30) % 12
53
52
  const a = s * Math.min(l, 1 - l)
54
- const f = (n: number) =>
55
- Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))))
53
+ const f = (n: number) => Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))))
56
54
 
57
55
  return {
58
56
  r: f(0),
59
57
  g: f(8),
60
- b: f(4)
58
+ b: f(4),
61
59
  }
62
60
  }
63
61
 
62
+ const lightnesses = [95, 85, 75, 60, 45, 30, 27, 21, 16, 10]
63
+
64
+ const defaultLightnessMap = Object.fromEntries(
65
+ lightnesses.map((l, i) => {
66
+ const step = ((i + 1) * 100).toString()
67
+ return [step, l]
68
+ }),
69
+ )
70
+
71
+ const alphas = [0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90]
72
+
73
+ const defaultAlphasMap = Object.fromEntries(
74
+ alphas.map((a, i) => {
75
+ const step = ((i + 1) * 100).toString()
76
+ return [step, a]
77
+ }),
78
+ )
79
+
64
80
  export function generateColorScheme(
65
- anchorHex: string,
66
- prefix: string = 'primary'
81
+ anchorHex: string,
82
+ prefix = 'primary',
83
+ lightnesses:typeof defaultLightnessMap = defaultLightnessMap,
84
+ alphas: typeof defaultAlphasMap = defaultAlphasMap,
67
85
  ): ColorMap {
68
86
  const { h, s } = hexToHSL(anchorHex)
69
87
  const baseRGB = hexToRGB(anchorHex)
70
-
88
+
71
89
  const scheme: ColorMap = {}
72
-
73
- const lightnesses = [95, 85, 75, 60, 45, 30, 27, 21, 16, 10]
74
-
75
- lightnesses.forEach((lightness, index) => {
76
- const step = (index + 1) * 100
90
+
91
+ Object.entries(lightnesses).forEach(([step, lightness]) => {
77
92
  const rgb = hslToRGB(h, s, lightness)
78
93
  scheme[`${prefix}Solid${step}`] = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 1.00)`
79
94
  })
80
-
81
- const alphas = [0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90]
82
-
83
- alphas.forEach((alpha, index) => {
84
- const step = (index + 1) * 100
95
+
96
+ Object.entries(alphas).forEach(([step, alpha]) => {
85
97
  scheme[`${prefix}Transparent${step}`] = `rgba(${baseRGB.r}, ${baseRGB.g}, ${baseRGB.b}, ${alpha.toFixed(2)})`
86
98
  })
87
-
99
+
88
100
  return scheme
89
101
  }
90
102
 
@@ -103,4 +115,4 @@ export function getTextColor(backgroundHex: string, darkColor = 'black', lightCo
103
115
  const rgb = hexToRGB(backgroundHex)
104
116
  const luminance = getLuminance(rgb)
105
117
  return luminance > 0.5 ? darkColor : lightColor
106
- }
118
+ }