Blame view

public/assets/addons/fastflow/plugins/node-qrcode-master/lib/renderer/utils.js 2.83 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
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
  function hex2rgba (hex) {
    if (typeof hex === 'number') {
      hex = hex.toString()
    }
  
    if (typeof hex !== 'string') {
      throw new Error('Color should be defined as hex string')
    }
  
    let hexCode = hex.slice().replace('#', '').split('')
    if (hexCode.length < 3 || hexCode.length === 5 || hexCode.length > 8) {
      throw new Error('Invalid hex color: ' + hex)
    }
  
    // Convert from short to long form (fff -> ffffff)
    if (hexCode.length === 3 || hexCode.length === 4) {
      hexCode = Array.prototype.concat.apply([], hexCode.map(function (c) {
        return [c, c]
      }))
    }
  
    // Add default alpha value
    if (hexCode.length === 6) hexCode.push('F', 'F')
  
    const hexValue = parseInt(hexCode.join(''), 16)
  
    return {
      r: (hexValue >> 24) & 255,
      g: (hexValue >> 16) & 255,
      b: (hexValue >> 8) & 255,
      a: hexValue & 255,
      hex: '#' + hexCode.slice(0, 6).join('')
    }
  }
  
  exports.getOptions = function getOptions (options) {
    if (!options) options = {}
    if (!options.color) options.color = {}
  
    const margin = typeof options.margin === 'undefined' ||
      options.margin === null ||
      options.margin < 0
      ? 4
      : options.margin
  
    const width = options.width && options.width >= 21 ? options.width : undefined
    const scale = options.scale || 4
  
    return {
      width: width,
      scale: width ? 4 : scale,
      margin: margin,
      color: {
        dark: hex2rgba(options.color.dark || '#000000ff'),
        light: hex2rgba(options.color.light || '#ffffffff')
      },
      type: options.type,
      rendererOpts: options.rendererOpts || {}
    }
  }
  
  exports.getScale = function getScale (qrSize, opts) {
    return opts.width && opts.width >= qrSize + opts.margin * 2
      ? opts.width / (qrSize + opts.margin * 2)
      : opts.scale
  }
  
  exports.getImageWidth = function getImageWidth (qrSize, opts) {
    const scale = exports.getScale(qrSize, opts)
    return Math.floor((qrSize + opts.margin * 2) * scale)
  }
  
  exports.qrToImageData = function qrToImageData (imgData, qr, opts) {
    const size = qr.modules.size
    const data = qr.modules.data
    const scale = exports.getScale(size, opts)
    const symbolSize = Math.floor((size + opts.margin * 2) * scale)
    const scaledMargin = opts.margin * scale
    const palette = [opts.color.light, opts.color.dark]
  
    for (let i = 0; i < symbolSize; i++) {
      for (let j = 0; j < symbolSize; j++) {
        let posDst = (i * symbolSize + j) * 4
        let pxColor = opts.color.light
  
        if (i >= scaledMargin && j >= scaledMargin &&
          i < symbolSize - scaledMargin && j < symbolSize - scaledMargin) {
          const iSrc = Math.floor((i - scaledMargin) / scale)
          const jSrc = Math.floor((j - scaledMargin) / scale)
          pxColor = palette[data[iSrc * size + jSrc] ? 1 : 0]
        }
  
        imgData[posDst++] = pxColor.r
        imgData[posDst++] = pxColor.g
        imgData[posDst++] = pxColor.b
        imgData[posDst] = pxColor.a
      }
    }
  }