@@ -55,7 +55,12 @@ import tc from 'tinycolor2'
55
55
import { InputType , GradientType , Modes } from ' @/enums'
56
56
import { cloneDeep } from ' lodash-es'
57
57
import { createGradientStr } from ' @/utils/color'
58
- import type { IColor , ColorPickerProps , GradientProps , IMode } from ' @/interfaces'
58
+ import type {
59
+ IColor ,
60
+ ColorPickerProps ,
61
+ GradientProps ,
62
+ IMode ,
63
+ } from ' @/interfaces'
59
64
60
65
const emits = defineEmits ([' update:value' , ' change' ])
61
66
// const { init, colorState, isGradient } = useColor()
@@ -106,7 +111,6 @@ const colorState = reactive<ColorPickerProps>({
106
111
const gradientType = ref <GradientType >(GradientType .linear )
107
112
const tinycolor = ref <typeof tc | null >(null )
108
113
109
-
110
114
const onChange = (val : IColor ) => {
111
115
emits (' update:value' , val .color )
112
116
emits (' change' , { ... val })
@@ -129,7 +133,11 @@ const setValue = (color?: string) => {
129
133
if (unref (isGradient )) {
130
134
colorState .degreesStr = degreeStr
131
135
colorState .gradientColors = colors
132
- colorState .gradientColor = createGradientStr (colors , unref (gradientType ), colorState )
136
+ colorState .gradientColor = createGradientStr (
137
+ colors ,
138
+ unref (gradientType ),
139
+ colorState ,
140
+ )
133
141
tinycolor .value = tc (
134
142
colorState .gradientColors [colorState .gradientColorsIdx ! ].value ,
135
143
)
@@ -165,126 +173,126 @@ const setValue = (color?: string) => {
165
173
}
166
174
167
175
const updateSelectColor = (value : string ) => {
168
- const colors = colorState .gradientColors || []
169
- const colorValue = colors [colorState .gradientColorsIdx ! ]
170
- if (! colorValue ) return
171
- colorValue .value = value
172
- const newGradStr = createGradientStr (colors , unref (gradientType ), colorState )
173
- setValue (newGradStr )
174
- }
175
-
176
- const handleGradient = (newColor : string , left ? : number ) => {
177
- const colors = colorState .gradientColors || []
178
- const colorValue = colors [colorState .gradientColorsIdx ! ]
179
- if (! colorValue ) return
180
- colorValue .left = left ?? colorValue .left
181
- colorValue .value = newColor
182
- const newGradStr = createGradientStr (colors , unref (gradientType ), colorState )
183
- setValue (newGradStr )
184
- }
176
+ const colors = colorState .gradientColors || []
177
+ const colorValue = colors [colorState .gradientColorsIdx ! ]
178
+ if (! colorValue ) return
179
+ colorValue .value = value
180
+ const newGradStr = createGradientStr (colors , unref (gradientType ), colorState )
181
+ setValue (newGradStr )
182
+ }
185
183
184
+ const handleGradient = (newColor : string , left ? : number ) => {
185
+ const colors = colorState .gradientColors || []
186
+ const colorValue = colors [colorState .gradientColorsIdx ! ]
187
+ if (! colorValue ) return
188
+ colorValue .left = left ?? colorValue .left
189
+ colorValue .value = newColor
190
+ const newGradStr = createGradientStr (colors , unref (gradientType ), colorState )
191
+ setValue (newGradStr )
192
+ }
186
193
187
- const changeColor = (newColor : string ) => {
188
- if (unref (isGradient )) {
189
- handleGradient (newColor )
190
- } else {
191
- setValue (newColor )
192
- }
194
+ const changeColor = (newColor : string ) => {
195
+ if (unref (isGradient )) {
196
+ handleGradient (newColor )
197
+ } else {
198
+ setValue (newColor )
193
199
}
200
+ }
194
201
195
- const setHcH = (h : number ) => {
196
- if (colorState .hc ?.h ) {
197
- colorState .hc .h = h
198
- }
202
+ const setHcH = (h : number ) => {
203
+ if (colorState .hc ?.h ) {
204
+ colorState .hc .h = h
199
205
}
206
+ }
200
207
201
- const setSelectColorIdx = (idx : number ) => {
202
- colorState .gradientColorsIdx = idx
203
- // warning: Here is update hc, but not need to handle onChange
204
- tinycolor .value = tc (
205
- colorState .gradientColors ! [colorState .gradientColorsIdx ! ].value ,
206
- )
207
- const rgba = tinycolor .value .toRgb ()
208
- const hsv = tinycolor .value .toHsv ()
209
- colorState .hc = { ... rgba , ... hsv }
210
- }
208
+ const setSelectColorIdx = (idx : number ) => {
209
+ colorState .gradientColorsIdx = idx
210
+ // warning: Here is update hc, but not need to handle onChange
211
+ tinycolor .value = tc (
212
+ colorState .gradientColors ! [colorState .gradientColorsIdx ! ].value ,
213
+ )
214
+ const rgba = tinycolor .value .toRgb ()
215
+ const hsv = tinycolor .value .toHsv ()
216
+ colorState .hc = { ... rgba , ... hsv }
217
+ }
211
218
212
- // 设置inputs的类型
213
- const setInputType = (type : InputType ) => {
214
- colorState .inputType = type
215
- }
219
+ // 设置inputs的类型
220
+ const setInputType = (type : InputType ) => {
221
+ colorState .inputType = type
222
+ }
216
223
217
- // 设置线性渐变
218
- const setLinear = () => {
219
- const value = colorState .gradientColor ?.split (/ ,(. + )/ )[1 ]
220
- value && setValue (` linear-gradient(90deg, ${value } ` )
221
- gradientType .value = GradientType .linear
222
- }
224
+ // 设置线性渐变
225
+ const setLinear = () => {
226
+ const value = colorState .gradientColor ?.split (/ ,(. + )/ )[1 ]
227
+ value && setValue (` linear-gradient(90deg, ${value } ` )
228
+ gradientType .value = GradientType .linear
229
+ }
223
230
224
- // 设置径向渐变
225
- const setRadial = () => {
226
- const value = colorState .gradientColor ?.split (/ ,(. + )/ )[1 ]
227
- value && setValue (` radial-gradient(circle, ${value } ` )
228
- gradientType .value = GradientType .radial
229
- }
231
+ // 设置径向渐变
232
+ const setRadial = () => {
233
+ const value = colorState .gradientColor ?.split (/ ,(. + )/ )[1 ]
234
+ value && setValue (` radial-gradient(circle, ${value } ` )
235
+ gradientType .value = GradientType .radial
236
+ }
230
237
231
- // 设置角度
232
- const setDegrees = (val : number ) => {
233
- if (gradientType .value !== GradientType .linear )
234
- return console .log (
235
- ' Warning: you are updating degrees when the gradient type is not linear. This will change the gradients type which may be undesired' ,
236
- )
237
-
238
- const remaining = colorState .gradientColor ?.split (/ ,(. + )/ )[1 ]
239
- if (! remaining ) return
240
- colorState .degrees = val
241
- setValue (
242
- ` linear-gradient(${formatInputValues (val , 0 , 360 )}deg, ${remaining } ` ,
238
+ // 设置角度
239
+ const setDegrees = (val : number ) => {
240
+ if (gradientType .value !== GradientType .linear )
241
+ return console .log (
242
+ ' Warning: you are updating degrees when the gradient type is not linear. This will change the gradients type which may be undesired' ,
243
243
)
244
- }
245
244
246
- // 添加渐变点
247
- const addPoint = (left : number ) => {
248
- if (! left && left !== 0 ) {
249
- console .log (
250
- ' You did not pass a stop value (left amount) for the new color point so it defaulted to 50' ,
251
- )
252
- }
253
- const colors = cloneDeep (colorState .gradientColors ! )
254
- const curColorValue = colors [colorState .gradientColorsIdx ! ]
255
-
256
- const newColors = [
257
- ... colors ?.map ((c : GradientProps ) => ({
258
- ... c ,
259
- value: low (c ),
260
- })),
261
- { value: curColorValue .value , left: left },
262
- ]?.sort ((a : any , b : any ) => a .left - b .left )
263
-
264
- colorState .gradientColorsIdx = newColors .findIndex ((i ) => i .left === left )
265
- const color = createGradientStr (newColors , unref (gradientType ), colorState )
266
- setValue (color )
267
- }
245
+ const remaining = colorState .gradientColor ?.split (/ ,(. + )/ )[1 ]
246
+ if (! remaining ) return
247
+ colorState .degrees = val
248
+ setValue (` linear-gradient(${formatInputValues (val , 0 , 360 )}deg, ${remaining } ` )
249
+ }
268
250
269
- // 删除渐变点
270
- const deletePoint = (index ? : number ) => {
271
- const colors = colorState .gradientColors
272
- if (colors && colors ?.length > 2 ) {
273
- const pointToDelete = index ?? colorState .gradientColorsIdx
274
- const remaining = colors ?.filter (
275
- (rc : GradientProps , i : number ) => i !== pointToDelete ,
276
- )
277
- colorState .gradientColors = cloneDeep (remaining )
278
-
279
- const newGradientColor = createGradientStr (remaining , unref (gradientType ), colorState )
280
- setValue (newGradientColor )
281
- } else {
282
- console .log (
283
- ' A gradient must have atleast two colors, disable your delete button when necessary' ,
284
- )
285
- }
251
+ // 添加渐变点
252
+ const addPoint = (left : number ) => {
253
+ if (! left && left !== 0 ) {
254
+ console .log (
255
+ ' You did not pass a stop value (left amount) for the new color point so it defaulted to 50' ,
256
+ )
286
257
}
258
+ const colors = cloneDeep (colorState .gradientColors ! )
259
+ const curColorValue = colors [colorState .gradientColorsIdx ! ]
260
+
261
+ const newColors = [
262
+ ... colors ?.map ((c : GradientProps ) => ({
263
+ ... c ,
264
+ value: low (c ),
265
+ })),
266
+ { value: curColorValue .value , left: left },
267
+ ]?.sort ((a : any , b : any ) => a .left - b .left )
268
+
269
+ colorState .gradientColorsIdx = newColors .findIndex ((i ) => i .left === left )
270
+ const color = createGradientStr (newColors , unref (gradientType ), colorState )
271
+ setValue (color )
272
+ }
273
+
274
+ // 删除渐变点
275
+ const deletePoint = (index ? : number ) => {
276
+ const colors = colorState .gradientColors
277
+ if (colors && colors ?.length > 2 ) {
278
+ const pointToDelete = index ?? colorState .gradientColorsIdx
279
+ const remaining = colors ?.filter (
280
+ (rc : GradientProps , i : number ) => i !== pointToDelete ,
281
+ )
282
+ colorState .gradientColors = cloneDeep (remaining )
287
283
284
+ const newGradientColor = createGradientStr (
285
+ remaining ,
286
+ unref (gradientType ),
287
+ colorState ,
288
+ )
289
+ setValue (newGradientColor )
290
+ } else {
291
+ console .log (
292
+ ' A gradient must have atleast two colors, disable your delete button when necessary' ,
293
+ )
294
+ }
295
+ }
288
296
289
297
const init = () => {
290
298
Object .assign (colorState , props )
@@ -319,7 +327,7 @@ const colorProvider = {
319
327
setDegrees ,
320
328
setSelectColorIdx ,
321
329
addPoint ,
322
- deletePoint
330
+ deletePoint ,
323
331
}
324
332
325
333
init ()
0 commit comments