daisyui 3.9.4 → 4.0.0-alpha.10

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.
@@ -1,15 +1,7 @@
1
1
  const colorNames = require("./colorNames")
2
2
  const themeDefaults = require("./themeDefaults")
3
3
 
4
- const { colord, getFormat, extend } = require("colord")
5
- const mixPlugin = require("colord/plugins/mix")
6
- const namesPlugin = require("colord/plugins/names")
7
- const lchPlugin = require("colord/plugins/lch")
8
- const hwbPlugin = require("colord/plugins/hwb")
9
- const labPlugin = require("colord/plugins/lab")
10
- const xyzPlugin = require("colord/plugins/xyz")
11
-
12
- extend([mixPlugin, namesPlugin, lchPlugin, hwbPlugin, labPlugin, xyzPlugin])
4
+ const { toGamut, interpolate, wcagContrast } = require("culori")
13
5
 
14
6
  module.exports = {
15
7
  changeColorValuesToObject: function (input) {
@@ -17,24 +9,44 @@ module.exports = {
17
9
  return { h, s, l, a: 1 }
18
10
  },
19
11
 
20
- turnColorValuesToString: function (input) {
21
- const [h, s, l] = input.match(/\d+(\.\d+)?%|\d+(\.\d+)?/g).map(parseFloat)
22
- return `${h} ${s}% ${l}%`
12
+ isDark: (color) => {
13
+ if (wcagContrast(color, "black") < wcagContrast(color, "white")) {
14
+ return true
15
+ }
16
+ return false
23
17
  },
24
18
 
25
- generateForegroundColorFrom: function (input, percentage = 0.8) {
26
- const str = colord(input)
27
- .mix(colord(input).isDark() ? "white" : "black", percentage)
28
- .toHslString()
29
- return this.turnColorValuesToString(str)
19
+ colorObjToString: function (input, colorSpace) {
20
+ const cut = (number) => {
21
+ if (!number) {
22
+ return 0
23
+ }
24
+ return +number.toFixed(4)
25
+ }
26
+ if (colorSpace === "oklch") {
27
+ const { l, c, h } = input
28
+ return `${cut(l)} ${cut(c)} ${cut(h)}`
29
+ }
30
+ if (colorSpace === "hsl") {
31
+ const { h, s, l } = input
32
+ return `${cut(h)} ${cut(s)}% ${cut(l)}%`
33
+ }
34
+ },
35
+
36
+ generateForegroundColorFrom: function (input, percentage = 0.8, colorSpace) {
37
+ const result = interpolate(
38
+ [input, this.isDark(input) ? "white" : "black"],
39
+ colorSpace
40
+ )(percentage)
41
+ return this.colorObjToString(result, colorSpace)
30
42
  },
31
43
 
32
- generateDarkenColorFrom: function (input, percentage = 0.07) {
33
- const str = colord(input).darken(percentage).toHslString()
34
- return this.turnColorValuesToString(str)
44
+ generateDarkenColorFrom: function (input, percentage = 0.07, colorSpace) {
45
+ const result = interpolate([input, "black"], colorSpace)(percentage)
46
+ return this.colorObjToString(result, colorSpace)
35
47
  },
36
48
 
37
- convertColorFormat: function (input, colorFunction = "hsl") {
49
+ convertColorFormat: function (input, colorSpace) {
38
50
  if (typeof input !== "object" || input === null) {
39
51
  return input
40
52
  }
@@ -42,119 +54,85 @@ module.exports = {
42
54
  const resultObj = {}
43
55
 
44
56
  Object.entries(input).forEach(([rule, value]) => {
45
- // if (!Object.hasOwn(colorNames, rule)) {
46
- // resultObj[rule] = value
47
- // } else {
48
- // let arr
49
- // if (getFormat(value) === "lch") {
50
- // arr = this.changeColorValuesToObject(value)
51
- // } else {
52
- // arr = colord(value).toLch()
53
- // }
54
- // resultObj[colorNames[rule]] = arr.l + " " + arr.c + " " + arr.h
55
- // }
56
-
57
57
  if (colorNames.hasOwnProperty(rule)) {
58
- const hslArray = colord(value).toHsl()
59
- resultObj[colorNames[rule]] = `${hslArray.h} ${hslArray.s}% ${hslArray.l}%`
58
+ const colorObj = toGamut(colorSpace)(value)
59
+ resultObj[colorNames[rule]] = this.colorObjToString(colorObj, colorSpace)
60
60
  } else {
61
61
  resultObj[rule] = value
62
62
  }
63
63
 
64
- // if (getFormat("lch(" + value + ")") === "lch") {
65
- // resultObj[rule] = value
66
- // if (colorFunction === "hsl") {
67
- // let arr = colord("lch(" + value + ")").toHsl()
68
- // resultObj[rule] = arr.h + " " + arr.s + "% " + arr.l + "%"
69
- // }
70
- // }
71
-
72
- // auto generate focus colors
73
- if (!Object.hasOwn(input, "primary-focus")) {
74
- resultObj["--pf"] = this.generateDarkenColorFrom(input["primary"])
75
- }
76
- if (!Object.hasOwn(input, "secondary-focus")) {
77
- resultObj["--sf"] = this.generateDarkenColorFrom(input["secondary"])
78
- }
79
- if (!Object.hasOwn(input, "accent-focus")) {
80
- resultObj["--af"] = this.generateDarkenColorFrom(input["accent"])
81
- }
82
- if (!Object.hasOwn(input, "neutral-focus")) {
83
- resultObj["--nf"] = this.generateDarkenColorFrom(input["neutral"])
84
- }
85
-
86
64
  // auto generate base colors
87
65
  if (!Object.hasOwn(input, "base-100")) {
88
66
  resultObj["--b1"] = "100 0 0"
89
67
  }
90
68
  if (!Object.hasOwn(input, "base-200")) {
91
- resultObj["--b2"] = this.generateDarkenColorFrom(input["base-100"])
69
+ resultObj["--b2"] = this.generateDarkenColorFrom(input["base-100"], 0.07, colorSpace)
92
70
  }
93
71
  if (!Object.hasOwn(input, "base-300")) {
94
72
  if (Object.hasOwn(input, "base-200")) {
95
- resultObj["--b3"] = this.generateDarkenColorFrom(input["base-200"])
73
+ resultObj["--b3"] = this.generateDarkenColorFrom(input["base-200"], 0.07, colorSpace)
96
74
  } else {
97
- resultObj["--b3"] = this.generateDarkenColorFrom(input["base-100"], 0.14)
75
+ resultObj["--b3"] = this.generateDarkenColorFrom(input["base-100"], 0.14, colorSpace)
98
76
  }
99
77
  }
100
78
 
101
79
  // auto generate state colors
102
80
 
103
81
  if (!Object.hasOwn(input, "info")) {
104
- resultObj["--in"] = 198 + " " + 93 + "%" + " " + 60 + "%"
82
+ resultObj["--in"] = "0.7206 0.191 231.6"
105
83
  }
106
84
  if (!Object.hasOwn(input, "success")) {
107
- resultObj["--su"] = 158 + " " + 64 + "%" + " " + 52 + "%"
85
+ resultObj["--su"] = "0.7441 0.213 164.75"
108
86
  }
109
87
  if (!Object.hasOwn(input, "warning")) {
110
- resultObj["--wa"] = 43 + " " + 96 + "%" + " " + 56 + "%"
88
+ resultObj["--wa"] = "0.8471 0.199 83.87"
111
89
  }
112
90
  if (!Object.hasOwn(input, "error")) {
113
- resultObj["--er"] = 0 + " " + 91 + "%" + " " + 71 + "%"
91
+ resultObj["--er"] = "0.7176 0.221 22.18"
114
92
  }
115
93
 
116
94
  // auto generate content colors
117
95
  if (!Object.hasOwn(input, "base-content")) {
118
- resultObj["--bc"] = this.generateForegroundColorFrom(input["base-100"])
96
+ resultObj["--bc"] = this.generateForegroundColorFrom(input["base-100"], 0.8, colorSpace)
119
97
  }
120
98
  if (!Object.hasOwn(input, "primary-content")) {
121
- resultObj["--pc"] = this.generateForegroundColorFrom(input["primary"])
99
+ resultObj["--pc"] = this.generateForegroundColorFrom(input["primary"], 0.8, colorSpace)
122
100
  }
123
101
  if (!Object.hasOwn(input, "secondary-content")) {
124
- resultObj["--sc"] = this.generateForegroundColorFrom(input["secondary"])
102
+ resultObj["--sc"] = this.generateForegroundColorFrom(input["secondary"], 0.8, colorSpace)
125
103
  }
126
104
  if (!Object.hasOwn(input, "accent-content")) {
127
- resultObj["--ac"] = this.generateForegroundColorFrom(input["accent"])
105
+ resultObj["--ac"] = this.generateForegroundColorFrom(input["accent"], 0.8, colorSpace)
128
106
  }
129
107
  if (!Object.hasOwn(input, "neutral-content")) {
130
- resultObj["--nc"] = this.generateForegroundColorFrom(input["neutral"])
108
+ resultObj["--nc"] = this.generateForegroundColorFrom(input["neutral"], 0.8, colorSpace)
131
109
  }
132
110
  if (!Object.hasOwn(input, "info-content")) {
133
111
  if (Object.hasOwn(input, "info")) {
134
- resultObj["--inc"] = this.generateForegroundColorFrom(input["info"])
112
+ resultObj["--inc"] = this.generateForegroundColorFrom(input["info"], 0.8, colorSpace)
135
113
  } else {
136
- resultObj["--inc"] = 198 + " " + 100 + "%" + " " + 12 + "%"
114
+ resultObj["--inc"] = "0 0 0"
137
115
  }
138
116
  }
139
117
  if (!Object.hasOwn(input, "success-content")) {
140
118
  if (Object.hasOwn(input, "success")) {
141
- resultObj["--suc"] = this.generateForegroundColorFrom(input["success"])
119
+ resultObj["--suc"] = this.generateForegroundColorFrom(input["success"], 0.8, colorSpace)
142
120
  } else {
143
- resultObj["--suc"] = 158 + " " + 100 + "%" + " " + 10 + "%"
121
+ resultObj["--suc"] = "0 0 0"
144
122
  }
145
123
  }
146
124
  if (!Object.hasOwn(input, "warning-content")) {
147
125
  if (Object.hasOwn(input, "warning")) {
148
- resultObj["--wac"] = this.generateForegroundColorFrom(input["warning"])
126
+ resultObj["--wac"] = this.generateForegroundColorFrom(input["warning"], 0.8, colorSpace)
149
127
  } else {
150
- resultObj["--wac"] = 43 + " " + 100 + "%" + " " + 11 + "%"
128
+ resultObj["--wac"] = "0 0 0"
151
129
  }
152
130
  }
153
131
  if (!Object.hasOwn(input, "error-content")) {
154
132
  if (Object.hasOwn(input, "error")) {
155
- resultObj["--erc"] = this.generateForegroundColorFrom(input["error"])
133
+ resultObj["--erc"] = this.generateForegroundColorFrom(input["error"], 0.8, colorSpace)
156
134
  } else {
157
- resultObj["--erc"] = 0 + " " + 100 + "%" + " " + 14 + "%"
135
+ resultObj["--erc"] = "0 0 0"
158
136
  }
159
137
  }
160
138
 
@@ -175,17 +153,15 @@ module.exports = {
175
153
  return resultObj
176
154
  },
177
155
 
178
- injectThemes: function (addBase, config, themes, colorFunction) {
156
+ injectThemes: function (addBase, config, themes) {
157
+ let colorSpace = "oklch"
158
+ // if (["oklch", "hsl"].includes(config("daisyui.colorSpace"))) {
159
+ // colorSpace = config("daisyui.colorSpace")
160
+ // }
179
161
  const includedThemesObj = {}
180
- // includedThemesObj["@supports (not(color: lch(0 0 0)))"] = {}
181
162
  // add default themes
182
163
  Object.entries(themes).forEach(([theme, value]) => {
183
- if (colorFunction === "lch") {
184
- includedThemesObj[theme] = this.convertColorFormat(value)
185
- }
186
- if (colorFunction === "hsl") {
187
- includedThemesObj[theme] = this.convertColorFormat(value, "hsl")
188
- }
164
+ includedThemesObj[theme] = this.convertColorFormat(value, colorSpace)
189
165
  })
190
166
 
191
167
  // add custom themes
@@ -193,8 +169,10 @@ module.exports = {
193
169
  config("daisyui.themes").forEach((item) => {
194
170
  if (typeof item === "object" && item !== null) {
195
171
  Object.entries(item).forEach(([customThemeName, customThemevalue]) => {
196
- includedThemesObj["[data-theme=" + customThemeName + "]"] =
197
- this.convertColorFormat(customThemevalue)
172
+ includedThemesObj["[data-theme=" + customThemeName + "]"] = this.convertColorFormat(
173
+ customThemevalue,
174
+ colorSpace
175
+ )
198
176
  })
199
177
  }
200
178
  })
@@ -254,12 +232,8 @@ module.exports = {
254
232
  includedThemesObj["[data-theme=" + themeName + "]"]
255
233
  }
256
234
  })
257
- if (colorFunction === "lch") {
258
- addBase({ "@supports (color: lch(0 0 0))": themesToInject })
259
- }
260
- if (colorFunction === "hsl") {
261
- addBase(themesToInject)
262
- }
235
+
236
+ addBase(themesToInject)
263
237
 
264
238
  return {
265
239
  includedThemesObj,
@@ -1,39 +1,37 @@
1
- let colorObject = {
1
+ const colorNames = require("./colorNames")
2
+
3
+ const colorObject = {
2
4
  "transparent": "transparent",
3
5
  "current": "currentColor",
4
6
 
5
- "primary": "hsl(var(--p) / <alpha-value>)",
6
- "primary-focus": "hsl(var(--pf) / <alpha-value>)",
7
- "primary-content": "hsl(var(--pc) / <alpha-value>)",
7
+ "primary": "oklch(var(--p) / <alpha-value>)",
8
+ "primary-content": "oklch(var(--pc) / <alpha-value>)",
8
9
 
9
- "secondary": "hsl(var(--s) / <alpha-value>)",
10
- "secondary-focus": "hsl(var(--sf) / <alpha-value>)",
11
- "secondary-content": "hsl(var(--sc) / <alpha-value>)",
10
+ "secondary": "oklch(var(--s) / <alpha-value>)",
11
+ "secondary-content": "oklch(var(--sc) / <alpha-value>)",
12
12
 
13
- "accent": "hsl(var(--a) / <alpha-value>)",
14
- "accent-focus": "hsl(var(--af) / <alpha-value>)",
15
- "accent-content": "hsl(var(--ac) / <alpha-value>)",
13
+ "accent": "oklch(var(--a) / <alpha-value>)",
14
+ "accent-content": "oklch(var(--ac) / <alpha-value>)",
16
15
 
17
- "neutral": "hsl(var(--n) / <alpha-value>)",
18
- "neutral-focus": "hsl(var(--nf) / <alpha-value>)",
19
- "neutral-content": "hsl(var(--nc) / <alpha-value>)",
16
+ "neutral": "oklch(var(--n) / <alpha-value>)",
17
+ "neutral-content": "oklch(var(--nc) / <alpha-value>)",
20
18
 
21
- "base-100": "hsl(var(--b1) / <alpha-value>)",
22
- "base-200": "hsl(var(--b2) / <alpha-value>)",
23
- "base-300": "hsl(var(--b3) / <alpha-value>)",
24
- "base-content": "hsl(var(--bc) / <alpha-value>)",
19
+ "base-100": "oklch(var(--b1) / <alpha-value>)",
20
+ "base-200": "oklch(var(--b2) / <alpha-value>)",
21
+ "base-300": "oklch(var(--b3) / <alpha-value>)",
22
+ "base-content": "oklch(var(--bc) / <alpha-value>)",
25
23
 
26
- "info": "hsl(var(--in) / <alpha-value>)",
27
- "info-content": "hsl(var(--inc) / <alpha-value>)",
24
+ "info": "oklch(var(--in) / <alpha-value>)",
25
+ "info-content": "oklch(var(--inc) / <alpha-value>)",
28
26
 
29
- "success": "hsl(var(--su) / <alpha-value>)",
30
- "success-content": "hsl(var(--suc) / <alpha-value>)",
27
+ "success": "oklch(var(--su) / <alpha-value>)",
28
+ "success-content": "oklch(var(--suc) / <alpha-value>)",
31
29
 
32
- "warning": "hsl(var(--wa) / <alpha-value>)",
33
- "warning-content": "hsl(var(--wac) / <alpha-value>)",
30
+ "warning": "oklch(var(--wa) / <alpha-value>)",
31
+ "warning-content": "oklch(var(--wac) / <alpha-value>)",
34
32
 
35
- "error": "hsl(var(--er) / <alpha-value>)",
36
- "error-content": "hsl(var(--erc) / <alpha-value>)",
33
+ "error": "oklch(var(--er) / <alpha-value>)",
34
+ "error-content": "oklch(var(--erc) / <alpha-value>)",
37
35
  }
38
36
 
39
37
  module.exports = colorObject
@@ -21,7 +21,6 @@ module.exports = {
21
21
  "base-200": "#0D0D0D",
22
22
  "base-300": "#1A1919",
23
23
  "neutral": "#272626",
24
- "neutral-focus": "#343232",
25
24
  "info": "#0000ff",
26
25
  "success": "#008000",
27
26
  "warning": "#ffff00",
@@ -104,14 +103,10 @@ module.exports = {
104
103
  },
105
104
  "[data-theme=dark]": {
106
105
  "color-scheme": "dark",
107
- "primary": "#661AE6",
108
- "primary-content": "#ffffff",
109
- "secondary": "#D926AA",
110
- "secondary-content": "#ffffff",
111
- "accent": "#1FB2A5",
112
- "accent-content": "#ffffff",
106
+ "primary": "oklch(49.12% 0.3096 275.75)",
107
+ "secondary": "oklch(69.71% 0.329 342.55)",
108
+ "accent": "oklch(76.76% 0.184 183.61)",
113
109
  "neutral": "#2a323c",
114
- "neutral-focus": "#242b33",
115
110
  "neutral-content": "#A6ADBB",
116
111
  "base-100": "#1d232a",
117
112
  "base-200": "#191e24",
@@ -192,12 +187,9 @@ module.exports = {
192
187
  },
193
188
  "[data-theme=light]": {
194
189
  "color-scheme": "light",
195
- "primary": "#570df8",
196
- "primary-content": "#E0D2FE",
197
- "secondary": "#f000b8",
198
- "secondary-content": "#FFD1F4",
199
- "accent": "#1ECEBC",
200
- "accent-content": "#07312D",
190
+ "primary": "oklch(49.12% 0.3096 275.75)",
191
+ "secondary": "oklch(69.71% 0.329 342.55)",
192
+ "accent": "oklch(76.76% 0.184 183.61)",
201
193
  "neutral": "#2B3440",
202
194
  "neutral-content": "#D7DDE4",
203
195
  "base-100": "#ffffff",
@@ -396,7 +388,6 @@ module.exports = {
396
388
  "secondary": "#818CF8",
397
389
  "accent": "#F471B5",
398
390
  "neutral": "#1E293B",
399
- "neutral-focus": "#273449",
400
391
  "base-100": "#0F172A",
401
392
  "info": "#0CA5E9",
402
393
  "info-content": "#000000",