utils.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. function hex2rgba (hex) {
  2. if (typeof hex === 'number') {
  3. hex = hex.toString()
  4. }
  5. if (typeof hex !== 'string') {
  6. throw new Error('Color should be defined as hex string')
  7. }
  8. var hexCode = hex.slice().replace('#', '').split('')
  9. if (hexCode.length < 3 || hexCode.length === 5 || hexCode.length > 8) {
  10. throw new Error('Invalid hex color: ' + hex)
  11. }
  12. // Convert from short to long form (fff -> ffffff)
  13. if (hexCode.length === 3 || hexCode.length === 4) {
  14. hexCode = Array.prototype.concat.apply([], hexCode.map(function (c) {
  15. return [c, c]
  16. }))
  17. }
  18. // Add default alpha value
  19. if (hexCode.length === 6) hexCode.push('F', 'F')
  20. var hexValue = parseInt(hexCode.join(''), 16)
  21. return {
  22. r: (hexValue >> 24) & 255,
  23. g: (hexValue >> 16) & 255,
  24. b: (hexValue >> 8) & 255,
  25. a: hexValue & 255,
  26. hex: '#' + hexCode.slice(0, 6).join('')
  27. }
  28. }
  29. exports.getOptions = function getOptions (options) {
  30. if (!options) options = {}
  31. if (!options.color) options.color = {}
  32. var margin = typeof options.margin === 'undefined' ||
  33. options.margin === null ||
  34. options.margin < 0 ? 4 : options.margin
  35. var width = options.width && options.width >= 21 ? options.width : undefined
  36. var scale = options.scale || 4
  37. return {
  38. width: width,
  39. scale: width ? 4 : scale,
  40. margin: margin,
  41. color: {
  42. dark: hex2rgba(options.color.dark || '#000000ff'),
  43. light: hex2rgba(options.color.light || '#ffffffff')
  44. },
  45. type: options.type,
  46. rendererOpts: options.rendererOpts || {}
  47. }
  48. }
  49. exports.getScale = function getScale (qrSize, opts) {
  50. return opts.width && opts.width >= qrSize + opts.margin * 2
  51. ? opts.width / (qrSize + opts.margin * 2)
  52. : opts.scale
  53. }
  54. exports.getImageWidth = function getImageWidth (qrSize, opts) {
  55. var scale = exports.getScale(qrSize, opts)
  56. return Math.floor((qrSize + opts.margin * 2) * scale)
  57. }
  58. exports.qrToImageData = function qrToImageData (imgData, qr, opts) {
  59. var size = qr.modules.size
  60. var data = qr.modules.data
  61. var scale = exports.getScale(size, opts)
  62. var symbolSize = Math.floor((size + opts.margin * 2) * scale)
  63. var scaledMargin = opts.margin * scale
  64. var palette = [opts.color.light, opts.color.dark]
  65. for (var i = 0; i < symbolSize; i++) {
  66. for (var j = 0; j < symbolSize; j++) {
  67. var posDst = (i * symbolSize + j) * 4
  68. var pxColor = opts.color.light
  69. if (i >= scaledMargin && j >= scaledMargin &&
  70. i < symbolSize - scaledMargin && j < symbolSize - scaledMargin) {
  71. var iSrc = Math.floor((i - scaledMargin) / scale)
  72. var jSrc = Math.floor((j - scaledMargin) / scale)
  73. pxColor = palette[data[iSrc * size + jSrc] ? 1 : 0]
  74. }
  75. imgData[posDst++] = pxColor.r
  76. imgData[posDst++] = pxColor.g
  77. imgData[posDst++] = pxColor.b
  78. imgData[posDst] = pxColor.a
  79. }
  80. }
  81. }