diff --git a/helpers/qr.helper.js b/helpers/qr.helper.js index 7473b6e..9152978 100644 --- a/helpers/qr.helper.js +++ b/helpers/qr.helper.js @@ -4,6 +4,29 @@ const moment = require("moment"); const config = require("../config"); +const luma = (color) => { // color can be a hx string or an array of RGB values 0-255 + const rgb = (typeof color === 'string') ? hexToRGBArray(color) : color; + return (0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]); // SMPTE C, Rec. 709 weightings +} + +const hexToRGBArray = (color) => { + if (color.length === 3) + color = color.charAt(0) + color.charAt(0) + color.charAt(1) + color.charAt(1) + color.charAt(2) + color.charAt(2); + else if (color.length !== 6) + console.debug('Invalid hex color: ' + color); + + const rgb = []; + for (var i = 0; i <= 2; i++) + rgb[i] = parseInt(color.substr(i * 2, 2), 16); + return rgb; +} + +const calculateContrastingColor = (color) => { + const _color = (typeof color === 'string') ? color.replace('#', '') : color; + return (luma(_color) >= 165) ? '#000' : '#fff'; +} + + module.exports.getInscriptionQRCodeUrl = function (inscriptionId) { return encodeURI(`${config.server.public_url}/inscriptions/${inscriptionId}/qrimage`); }; @@ -34,7 +57,7 @@ module.exports.getInscriptionQRCode = function (params) { options = _.assign(options, { color: { light: params.color ? colourNameToHex(params.color) : "#000000", - dark: "#ffffff", + dark: calculateContrastingColor(params.color), //"#ffffff", }, }); } diff --git a/modules/events/marketing_list.service.js b/modules/events/marketing_list.service.js index 7867240..0cd8d4f 100644 --- a/modules/events/marketing_list.service.js +++ b/modules/events/marketing_list.service.js @@ -42,7 +42,7 @@ function _generateMarketingDTO(dataInscription) { inscriptionDTO.validated = inscription.validated; inscriptionDTO.reservation_code = reservation ? reservation.reservation_code : null; - inscriptionDTO.color = reservation ? reservation.color : 'green'; + inscriptionDTO.color = reservation ? reservation.color : "darkgreen"; inscriptionDTO.description = (reservation ? reservation.description : "Entrada").toUpperCase(); inscriptionDTO.qrConfig = null; inscriptionDTO.qrCode = null;