From f19b21e2637d8bc8357059be239898de2c0f7122 Mon Sep 17 00:00:00 2001 From: Ashley Date: Thu, 24 Nov 2022 20:26:41 +0000 Subject: [PATCH] lightOrDark :3 --- src/libpoketube/init/pages-video.js | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/libpoketube/init/pages-video.js b/src/libpoketube/init/pages-video.js index 297ba187..db574fb8 100644 --- a/src/libpoketube/init/pages-video.js +++ b/src/libpoketube/init/pages-video.js @@ -20,6 +20,51 @@ const { const sha384 = modules.hash; +function lightOrDark(color) { + + // Variables for red, green, blue values + var r, g, b, hsp; + + // Check the format of the color, HEX or RGB? + if (color.match(/^rgb/)) { + + // If RGB --> store the red, green, blue values in separate variables + color = color.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/); + + r = color[1]; + g = color[2]; + b = color[3]; + } + else { + + // If hex --> Convert it to RGB: http://gist.github.com/983661 + color = +("0x" + color.slice(1).replace( + color.length < 5 && /./g, '$&$&')); + + r = color >> 16; + g = color >> 8 & 255; + b = color & 255; + } + + // HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html + hsp = Math.sqrt( + 0.299 * (r * r) + + 0.587 * (g * g) + + 0.114 * (b * b) + ); + + // Using the HSP value, determine whether the color is light or dark + if (hsp>127.5) { + + return 'light'; + } + else { + + return 'dark'; + } +} + + module.exports = function (app, config, renderTemplate) { app.get("/encryption", async function (req, res) { var v = req.query.v; @@ -115,6 +160,7 @@ module.exports = function (app, config, renderTemplate) { renderTemplate(res, req, "poketube.ejs", { color: data.color, + color2:data.color2, engagement: engagement, video: json, date: k.Video.uploadDate, @@ -122,6 +168,7 @@ module.exports = function (app, config, renderTemplate) { k: k, process: process, sha384: sha384, + lightOrDark, isMobile: req.useragent.isMobile, tj: data.channel, r: r,