Skip to content

Commit 8da5e70

Browse files
committed
white shadow for dark bg
1 parent 74a23d8 commit 8da5e70

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

frontend/src/svgBadge.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Get an approximate width of the given string
22
const approxWidth = (str) => {
33
let size = 0;
4-
for (var i = 0; i < str.length; i++) {
4+
for (let i = 0; i < str.length; i++) {
55
let s = str[i];
66
if ('lij|\' '.includes(s)) size += 37;
77
else if ('![]fI.,:;/\\t'.includes(s)) size += 50;
@@ -15,14 +15,27 @@ const approxWidth = (str) => {
1515
return size * 6 / 1000.0;
1616
};
1717

18-
// Generate the shadow color when black is mixed in with the bgColor. This is
19-
// used instead of opacity as multiple texts with the shadowColor are added a
20-
// little below one another to create a solid long shadow
21-
const shadowColor = (bgColor) => {
22-
var a = 0.3;
23-
var r = Math.floor(0x00 * a + Number(`0x${bgColor.substring(0, 2)}`) * (1 - a));
24-
var g = Math.floor(0x00 * a + Number(`0x${bgColor.substring(2, 4)}`) * (1 - a));
25-
var b = Math.floor(0x00 * a + Number(`0x${bgColor.substring(4, 6)}`) * (1 - a));
18+
// Get the lightness percentage of any given color
19+
function lightness(hex) {
20+
let result = /([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
21+
let r = parseInt(result[1], 16) / 255;
22+
let g = parseInt(result[2], 16) / 255;
23+
let b = parseInt(result[3], 16) / 255;
24+
let max = Math.max(r, g, b), min = Math.min(r, g, b);
25+
let l = Math.round((max + min) * 50);
26+
return l;
27+
}
28+
29+
// Generate the shadow color when black or white depending upon relative
30+
// lightness of bg and text color is mixed in with the bgColor. This is used
31+
// instead of opacity as multiple texts with the shadowColor are added a little
32+
// below one another to create a solid long shadow
33+
const shadowColor = (bgColor, textColor) => {
34+
let base = (lightness(bgColor) > lightness(textColor)) ? 0xFF : 0x00;
35+
let a = 0.3;
36+
let r = Math.floor(base * a + Number(`0x${bgColor.substring(0, 2)}`) * (1 - a));
37+
let g = Math.floor(base * a + Number(`0x${bgColor.substring(2, 4)}`) * (1 - a));
38+
let b = Math.floor(base * a + Number(`0x${bgColor.substring(4, 6)}`) * (1 - a));
2639
const finalColor = "#" + ((r << 16) | (g << 8) | b).toString(16);
2740
return finalColor;
2841
};
@@ -52,10 +65,10 @@ function svgBadge(label, shadow, swap, labelBGColor, countBGColor, labelTextColo
5265

5366
// Text shadow template
5467
let shadowTemplate = (shadow === "1") ? `
55-
<text transform="matrix(1 0 0 1 ${visitsWidth + 10.4} 14.8206)" fill="${shadowColor(countBGColor)}" font-family="Arial" font-size="10px">${visits}</text>
56-
<text transform="matrix(1 0 0 1 ${visitsWidth + 10.4} 14.1597)" fill="${shadowColor(countBGColor)}" font-family="Arial" font-size="10px">${visits}</text>
57-
<text transform="matrix(1 0 0 1 7.0189 14.8425)" fill="${shadowColor(labelBGColor)}" font-family="Arial" font-size="10px">${label}</text>
58-
<text transform="matrix(1 0 0 1 7.038 14.1817)" fill="${shadowColor(labelBGColor)}" font-family="Arial" font-size="10px">${label}</text>
68+
<text transform="matrix(1 0 0 1 ${visitsWidth + 10.4} 14.8206)" fill="${shadowColor(countBGColor, countTextColor)}" font-family="Arial" font-size="10px">${visits}</text>
69+
<text transform="matrix(1 0 0 1 ${visitsWidth + 10.4} 14.1597)" fill="${shadowColor(countBGColor, countTextColor)}" font-family="Arial" font-size="10px">${visits}</text>
70+
<text transform="matrix(1 0 0 1 7.0189 14.8425)" fill="${shadowColor(labelBGColor, labelTextColor)}" font-family="Arial" font-size="10px">${label}</text>
71+
<text transform="matrix(1 0 0 1 7.038 14.1817)" fill="${shadowColor(labelBGColor, labelTextColor)}" font-family="Arial" font-size="10px">${label}</text>
5972
`: '';
6073

6174
// Main SVG template

0 commit comments

Comments
 (0)