@codeleap/styles 5.5.1 → 5.5.4

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.5.1",
3
+ "version": "5.5.4",
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.5.1",
12
+ "@codeleap/config": "5.5.4",
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.5.1",
3
+ "version": "5.5.4",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -71,7 +71,7 @@ export const createTheme = <T extends Theme>(theme: T, themePersistor: ThemePers
71
71
  },
72
72
 
73
73
  setColorScheme(colorScheme: string) {
74
- const hasScheme = !!themeStore.alternateColorsScheme?.[colorScheme]
74
+ const hasScheme = colorScheme === 'default' ? true : !!themeStore.alternateColorsScheme?.[colorScheme]
75
75
 
76
76
  if (!hasScheme) {
77
77
  console.warn(`Color scheme ${colorScheme} not found in theme`)
@@ -1,10 +1,9 @@
1
1
  import { AppTheme, ColorMap, IAppVariants, ITheme, Theme } from '../types'
2
- import { map, atom, computed } from 'nanostores'
2
+ import { map, computed, atom } from 'nanostores'
3
3
 
4
4
  export type ThemeState = {
5
5
  theme: AppTheme<Theme> | null
6
6
  colorScheme: string | null
7
- variants: IAppVariants
8
7
  }
9
8
 
10
9
  class ThemeStore {
@@ -89,9 +88,7 @@ export const themeStore = new ThemeStore()
89
88
  export const themeStoreComputed = computed([
90
89
  themeStore['themeStore'],
91
90
  themeStore['colorSchemeStore'],
92
- themeStore['variantsStore']
93
- ], (theme, colorScheme, variants) => ({
91
+ ], (theme, colorScheme) => ({
94
92
  theme: theme as unknown as AppTheme<Theme>,
95
93
  colorScheme,
96
- variants
97
94
  }))
@@ -1,3 +1,5 @@
1
+ import { ColorMap } from '../types'
2
+
1
3
  export function hexToHSL(hex: string) {
2
4
  const r = parseInt(hex.slice(1, 3), 16) / 255
3
5
  const g = parseInt(hex.slice(3, 5), 16) / 255
@@ -36,28 +38,54 @@ export function hslToHex(h: number, s: number, l: number): string {
36
38
  return `#${[f(0), f(8), f(4)].map(x => x.toString(16).padStart(2, '0')).join('')}`
37
39
  }
38
40
 
39
- export function generateColorScheme(anchorHex: string, steps: number = 10): string[] {
40
- const { h, s } = hexToHSL(anchorHex)
41
+ export function hexToRGB(hex: string) {
42
+ const r = parseInt(hex.slice(1, 3), 16)
43
+ const g = parseInt(hex.slice(3, 5), 16)
44
+ const b = parseInt(hex.slice(5, 7), 16)
45
+ return { r, g, b }
46
+ }
41
47
 
42
- const lightest = 95
43
- const darkest = 20
44
- const increment = (lightest - darkest) / (steps - 1)
48
+ export function hslToRGB(h: number, s: number, l: number) {
49
+ s /= 100
50
+ l /= 100
45
51
 
46
- const colors: string[] = []
52
+ const k = (n: number) => (n + h / 30) % 12
53
+ 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)))))
47
56
 
48
- for (let i = 0; i < steps; i++) {
49
- const l = lightest - i * increment
50
- colors.push(hslToHex(h, s, l))
57
+ return {
58
+ r: f(0),
59
+ g: f(8),
60
+ b: f(4)
51
61
  }
52
-
53
- return colors
54
62
  }
55
63
 
56
- export function hexToRGB(hex: string) {
57
- const r = parseInt(hex.slice(1, 3), 16)
58
- const g = parseInt(hex.slice(3, 5), 16)
59
- const b = parseInt(hex.slice(5, 7), 16)
60
- return { r, g, b }
64
+ export function generateColorScheme(
65
+ anchorHex: string,
66
+ prefix: string = 'primary'
67
+ ): ColorMap {
68
+ const { h, s } = hexToHSL(anchorHex)
69
+ const baseRGB = hexToRGB(anchorHex)
70
+
71
+ 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
77
+ const rgb = hslToRGB(h, s, lightness)
78
+ scheme[`${prefix}Solid${step}`] = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 1.00)`
79
+ })
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
85
+ scheme[`${prefix}Transparent${step}`] = `rgba(${baseRGB.r}, ${baseRGB.g}, ${baseRGB.b}, ${alpha.toFixed(2)})`
86
+ })
87
+
88
+ return scheme
61
89
  }
62
90
 
63
91
  export function getLuminance({ r, g, b }: { r: number; g: number; b: number }): number {
@@ -75,4 +103,4 @@ export function getTextColor(backgroundHex: string, darkColor = 'black', lightCo
75
103
  const rgb = hexToRGB(backgroundHex)
76
104
  const luminance = getLuminance(rgb)
77
105
  return luminance > 0.5 ? darkColor : lightColor
78
- }
106
+ }