-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilityClass.js
More file actions
381 lines (356 loc) · 10.7 KB
/
utilityClass.js
File metadata and controls
381 lines (356 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// canvas工具类
class CanvasHandler {
constructor (element) {
if (!element) {
throw "Element can't stomach empty"
}
const dom = document.querySelector(element)
if (!dom) {
throw 'Element does not have a DOM'
}
this.canvas = dom
this.ctx = dom.getContext('2d')
}
// 画图片 参数:图片地址 x坐标 y坐标 宽 高 回调函数
// 回调函数:如果传了回调函数会返回图片的dom 第二个参数为next-不执行不会往下走
// next可穿绘制图片的宽度和高度-不传为默认传的宽度和高度,都不传为默认的图片宽高
drawImage (src, x = 0, y = 0, width, height, callback) {
const that = this
const img = new Image()
img.src = src
img.onload = function () {
const imgInfo = this
const imgWidth = this.width
const imgHeight = this.height
if (typeof callback === 'function') {
callback(imgInfo, function (wid, hei) {
that.ctx.drawImage(img, x, y, wid || width || imgWidth, hei || height || imgHeight)
})
} else {
that.ctx.drawImage(img, x, y, width || imgWidth, height || imgHeight)
}
}
}
}
// 文件操作类
class FileHandler {
// 将上传的文件转换为base64 可上传file或者fileList
async fileToBase64 (files) {
let fileArr = []
if (files instanceof File) {
fileArr = [files]
} else if (files instanceof FileList) {
fileArr = files
} else {
throw 'File format error '
}
return new Promise((resolve, reject) => {
let retArr = []
for (let i = 0; i < fileArr.length; i++) {
const file = fileArr[i]
const reads = new FileReader()
reads.readAsDataURL(file)
reads.onload = function () {
retArr.push(this.result)
if (retArr.length >= fileArr.length) {
resolve(retArr)
}
}
}
})
}
}
// 颜色操作类
// 返回{$hex, $hsbm $rgba}
(function (window) {
const rgbaReg = /^(rgba|RGBA)/
const rgbReg = /^(rgb|RGB)/
const hexReg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
const hsbToRgbObj = function (hsb) {
const rgb = {}
let h = Math.round(hsb['h'])
let s = Math.round(hsb['s'] * 255 / 100)
let b = Math.round(hsb['b'] * 255 / 100)
if (s === 0) {
rgb['r'] = rgb['g'] = rgb['b'] = b
} else {
let num1 = b
let num2 = (255 - s) * b / 255
let num3 = (num1 - num2) * (h % 60) / 60
if (h === 360) {
h = 0
}
if (h < 60) {
rgb['r'] = num1
rgb['g'] = num2 + num3
rgb['b'] = num2
} else if (h < 120) {
rgb['r'] = num1 - num3
rgb['g'] = num1
rgb['b'] = num2
} else if (h < 180) {
rgb['r'] = num2
rgb['g'] = num1
rgb['b'] = num2 + num3
} else if (h < 240) {
rgb['r'] = num2
rgb['g'] = num1 - num3
rgb['b'] = num1
} else if (h < 300) {
rgb['r'] = num2 + num3
rgb['g'] = num2
rgb['b'] = num1
} else if (h < 360) {
rgb['r'] = num1
rgb['g'] = num2
rgb['b'] = num1 - num3
} else {
rgb['r'] = 0
rgb['g'] = 0
rgb['b'] = 0
}
}
return {
r: Math.round(rgb['r']),
g: Math.round(rgb['g']),
b: Math.round(rgb['b']),
a: 1
}
}
const hexToRgbObj = function (hex) {
hex = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16)
return {
r: hex >> 16,
g: (hex & 0x00FF00) >> 8,
b: (hex & 0x0000FF),
a: 1
}
}
const rgbObjToHsb = function (rgb) {
const hsb = {
h: 0,
s: 0,
b: 0
}
const min = Math.min(rgb['r'], rgb['g'], rgb['b'])
const max = Math.max(rgb['r'], rgb['g'], rgb['b'])
const cz = max - min
hsb['b'] = max
hsb['s'] = max !== 0 ? 255 * cz / max : 0
if (hsb['s'] !== 0) {
if (rgb['r'] === max) {
hsb['h'] = (rgb['g'] - rgb['b']) / cz
} else if (rgb['g'] === max) {
hsb['h'] = 2 + (rgb['b'] - rgb['r']) / cz
} else {
hsb['h'] = 4 + (rgb['r'] - rgb['g']) / cz
}
} else {
hsb['h'] = -1
}
hsb['h'] *= 60
if (hsb['h'] < 0) {
hsb['h'] += 360
}
hsb['s'] *= 100 / 255
hsb['b'] *= 100 / 255
return hsb;
}
const hexToHsb = function (hex) {
return rgbObjToHsb(hexToRgbObj(hex))
}
const rgbObjToHex = function (rgb) {
var hex = [
rgb['r'].toString(16),
rgb['g'].toString(16),
rgb['b'].toString(16)
]
hex.map(function (str, i) {
if (str.length == 1) {
hex[i] = '0' + str
}
})
return '#' + hex.join('')
}
const rgbStrToHex = function (color) {
var rgb = color.split(',')
var r = parseInt(rgb[0].split('(')[1])
var g = parseInt(rgb[1])
var b = parseInt(rgb[2].split(')')[0])
var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
return hex
}
const rgbaStrToRgba = function (str) {
const strArr = str.toLocaleUpperCase().replace('RGBA(', '').replace(')', '').split(',')
const rgba = {
r: parseInt(strArr[0]),
g: parseInt(strArr[1]),
b: parseInt(strArr[2]),
a: parseFloat(strArr[3]),
}
return rgba
}
const getThemeCluster = function (theme) {
const tintColor = (color, tint) => {
let red = parseInt(color.slice(0, 2), 16)
let green = parseInt(color.slice(2, 4), 16)
let blue = parseInt(color.slice(4, 6), 16)
if (tint === 0) {
return [red, green, blue].join(',')
} else {
red += Math.round(tint * (255 - red))
green += Math.round(tint * (255 - green))
blue += Math.round(tint * (255 - blue))
red = red.toString(16)
green = green.toString(16)
blue = blue.toString(16)
return `#${ red }${ green }${ blue }`
}
};
const shadeColor = (color, shade) => {
let red = parseInt(color.slice(0, 2), 16)
let green = parseInt(color.slice(2, 4), 16)
let blue = parseInt(color.slice(4, 6), 16)
red = Math.round((1 - shade) * red)
green = Math.round((1 - shade) * green)
blue = Math.round((1 - shade) * blue)
red = red.toString(16)
green = green.toString(16)
blue = blue.toString(16)
return `#${ red }${ green }${ blue }`
};
const clusters = [theme]
for (let i = 0; i <= 9; i++) {
clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
}
clusters.push(shadeColor(theme, 0.1))
return clusters
}
class ColorProcessing {
constructor (color) {
if (typeof color === 'string') {
if (rgbaReg.test(color)) {
this.init('rgba', rgbaStrToRgba(color))
} else if (rgbReg.test(color)) {
this.init('hex', rgbStrToHex(color))
} else if (hexReg.test(color)) {
this.init('rgb', hexToRgbObj(color))
} else {
throw new Error('The color type is not recognized. string')
}
} else if (typeof color === 'object' && !Array.isArray(color)) {
if (color['r'] && color['g'] && color['b'] && color['a']) {
const {r, g, b, a} = color
this.init('rgba', {r, g, b, a})
} else if (color['r'] && color['g'] && color['b']) {
this.init('hsb', rgbObjToHsb(color))
} else if (color['h'] && color['s'] && color['b']) {
this.init('rgb', hsbToRgbObj(color))
} else {
throw new Error('The color type is not recognized. object')
}
} else {
throw new Error('The color type is not recognized. null')
}
}
// 创建hex rgba hsb
init (type, color) {
if (type === 'hex') {
this.$hex = color
this.$rgba = hexToRgbObj(color)
this.$hsb = rgbObjToHsb(this.$rgba)
} else if (type === 'rgb') {
this.$rgba = color
this.$hex = rgbObjToHex(color)
this.$hsb = rgbObjToHsb(color)
} else if (type === 'hsb') {
this.$hsb = color
this.$rgba = hsbToRgbObj(color)
this.$hex = rgbObjToHex(this.$rgba)
} else if (type === 'rgba') {
this.$rgba = color
this.$hsb = rgbObjToHsb(color)
this.$hex = rgbObjToHex(this.$rgba)
}
}
// 返回字符串的rgb
rgbToString () {
const {r, g, b} = this.$rgba
return `rgb(${r}, ${g}, ${b})`
}
// 返回字符串的rgba
rgbaToString () {
const {r, g, b, a} = this.$rgba
return `rgb(${r}, ${g}, ${b}, ${a})`
}
// 传入 new 新的颜色会返回创建的颜色和新的颜色所有透明颜色值
getThemeCluster (newColor) {
const newColorProcessing = new ColorProcessing(newColor)
return {
oldColors: getThemeCluster(this.$hex.replace('#', '')),
newColors: getThemeCluster(newColorProcessing.$hex.replace('#', ''))
}
}
// 改变style中的创建颜色改为新的颜色
setThemeCluster (newColor, id) {
const colorObj = this.getThemeCluster(newColor)
const {oldColors, newColors} = colorObj
const styles = document.querySelectorAll('style')
styles.forEach((style) => {
oldColors.forEach((old, index) => {
if (id) {
if (style.innerText.indexOf(id) !== -1) {
style.innerText = style.innerText.replace(new RegExp(old, 'ig'), newColors[index])
}
} else {
style.innerText = style.innerText.replace(new RegExp(old, 'ig'), newColors[index])
}
})
})
}
}
['hex', 'rgba', 'hsb'].forEach((key) => {
ColorProcessing.prototype[key] = function () {
return this['$' + key]
}
});
window.ColorProcessing = ColorProcessing
})(window)
// dom操作类
class DomHandler {
constructor (element) {
if (!element) {
throw "Element can't stomach empty"
}
const dom = document.querySelector(element)
if (!dom) {
throw 'Element does not have a DOM'
}
this.dom = dom
}
// 打印dom中的内容
// 参数1: 是否展示页眉 boolean
// 参数2:参数1为false时页眉的标题
printPart (isHeader = false, title = '') {
const el = this.dom
const iframe = document.createElement('IFRAME')
const style = document.createElement('style')
style.innerHTML = "@page {size: auto; margin: 0;}"
iframe.setAttribute('style', 'position: absolute;width: 0px;height: 0px;left: -500px; top:-500px;')
iframe.onload = function () {
const doc = iframe.contentWindow.document
document.title = title || ''
doc.write('<div>' + el.innerHTML + '</div>')
if (isHeader) {
doc.querySelector('head').appendChild(style)
}
doc.close()
iframe.contentWindow.focus()
iframe.contentWindow.print()
if (navigator.userAgent.indexOf("MSIE") > 0) {
document.body.removeChild(iframe)
}
}
document.body.appendChild(iframe)
}
}