e0de4a60776707130c36e15d6e2f6d89693008a139fb3db909c12854ca538fcd.json raw
1 {"ast":null,"code":"function hex2rgba(hex) {\n if (typeof hex === 'number') {\n hex = hex.toString();\n }\n if (typeof hex !== 'string') {\n throw new Error('Color should be defined as hex string');\n }\n let hexCode = hex.slice().replace('#', '').split('');\n if (hexCode.length < 3 || hexCode.length === 5 || hexCode.length > 8) {\n throw new Error('Invalid hex color: ' + hex);\n }\n\n // Convert from short to long form (fff -> ffffff)\n if (hexCode.length === 3 || hexCode.length === 4) {\n hexCode = Array.prototype.concat.apply([], hexCode.map(function (c) {\n return [c, c];\n }));\n }\n\n // Add default alpha value\n if (hexCode.length === 6) hexCode.push('F', 'F');\n const hexValue = parseInt(hexCode.join(''), 16);\n return {\n r: hexValue >> 24 & 255,\n g: hexValue >> 16 & 255,\n b: hexValue >> 8 & 255,\n a: hexValue & 255,\n hex: '#' + hexCode.slice(0, 6).join('')\n };\n}\nexports.getOptions = function getOptions(options) {\n if (!options) options = {};\n if (!options.color) options.color = {};\n const margin = typeof options.margin === 'undefined' || options.margin === null || options.margin < 0 ? 4 : options.margin;\n const width = options.width && options.width >= 21 ? options.width : undefined;\n const scale = options.scale || 4;\n return {\n width: width,\n scale: width ? 4 : scale,\n margin: margin,\n color: {\n dark: hex2rgba(options.color.dark || '#000000ff'),\n light: hex2rgba(options.color.light || '#ffffffff')\n },\n type: options.type,\n rendererOpts: options.rendererOpts || {}\n };\n};\nexports.getScale = function getScale(qrSize, opts) {\n return opts.width && opts.width >= qrSize + opts.margin * 2 ? opts.width / (qrSize + opts.margin * 2) : opts.scale;\n};\nexports.getImageWidth = function getImageWidth(qrSize, opts) {\n const scale = exports.getScale(qrSize, opts);\n return Math.floor((qrSize + opts.margin * 2) * scale);\n};\nexports.qrToImageData = function qrToImageData(imgData, qr, opts) {\n const size = qr.modules.size;\n const data = qr.modules.data;\n const scale = exports.getScale(size, opts);\n const symbolSize = Math.floor((size + opts.margin * 2) * scale);\n const scaledMargin = opts.margin * scale;\n const palette = [opts.color.light, opts.color.dark];\n for (let i = 0; i < symbolSize; i++) {\n for (let j = 0; j < symbolSize; j++) {\n let posDst = (i * symbolSize + j) * 4;\n let pxColor = opts.color.light;\n if (i >= scaledMargin && j >= scaledMargin && i < symbolSize - scaledMargin && j < symbolSize - scaledMargin) {\n const iSrc = Math.floor((i - scaledMargin) / scale);\n const jSrc = Math.floor((j - scaledMargin) / scale);\n pxColor = palette[data[iSrc * size + jSrc] ? 1 : 0];\n }\n imgData[posDst++] = pxColor.r;\n imgData[posDst++] = pxColor.g;\n imgData[posDst++] = pxColor.b;\n imgData[posDst] = pxColor.a;\n }\n }\n};","map":null,"metadata":{},"sourceType":"script","externalDependencies":[]}