diff --git a/api/index.js b/api/index.js index 22019cd..4625379 100644 --- a/api/index.js +++ b/api/index.js @@ -4,7 +4,16 @@ const fetchStats = require("../src/fetchStats"); const renderStatsCard = require("../src/renderStatsCard"); module.exports = async (req, res) => { - const { username, hide, hide_border, show_icons, line_height } = req.query; + const { + username, + hide, + hide_border, + show_icons, + line_height, + title_color, + icon_color, + text_color, + } = req.query; let stats; res.setHeader("Content-Type", "image/svg+xml"); @@ -20,6 +29,9 @@ module.exports = async (req, res) => { show_icons, hide_border, line_height, + title_color, + icon_color, + text_color, }) ); }; diff --git a/src/renderStatsCard.js b/src/renderStatsCard.js index 98d73b3..0791014 100644 --- a/src/renderStatsCard.js +++ b/src/renderStatsCard.js @@ -1,11 +1,11 @@ -const { kFormatter } = require("../src/utils"); +const { kFormatter, isValidHexColor } = require("../src/utils"); const createTextNode = ({ icon, label, value, lineHeight, id }) => { const classname = icon === "★" && "star-icon"; const kValue = kFormatter(value); return ` - ${icon} ${label}: + ${icon} ${label}: ${kValue} `; }; @@ -24,10 +24,19 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { show_icons = false, hide_border = false, line_height = 25, + title_color, + icon_color, + text_color, } = options; const lheight = parseInt(line_height); + const titleColor = + (isValidHexColor(title_color) && `#${title_color}`) || "#2f80ed"; + const iconColor = + (isValidHexColor(icon_color) && `#${icon_color}`) || "#4c71f2"; + const textColor = (isValidHexColor(text_color) && `#${text_color}`) || "#333"; + const STAT_MAP = { stars: createTextNode({ icon: "★", @@ -76,11 +85,12 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { return ` diff --git a/src/utils.js b/src/utils.js index 2b7168e..6cd2b50 100644 --- a/src/utils.js +++ b/src/utils.js @@ -25,4 +25,10 @@ function kFormatter(num) { : Math.sign(num) * Math.abs(num); } -module.exports = { renderError, kFormatter, encodeHTML }; +function isValidHexColor(hexColor) { + return new RegExp( + /^([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}|[A-Fa-f0-9]{4})$/ + ).test(hexColor); +} + +module.exports = { renderError, kFormatter, encodeHTML, isValidHexColor };