Skip to content

Commit

Permalink
Adjust the logo color according to the background color
Browse files Browse the repository at this point in the history
  • Loading branch information
silentsoft committed Feb 13, 2022
1 parent d90b2af commit 8c85384
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
17 changes: 16 additions & 1 deletion src/main/java/org/silentsoft/badge4j/badge/Badge.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,24 @@ protected String encodeLogoToBase64(String logo) {
Icon simpleIcon = SimpleIcons.get(logo);
if (simpleIcon != null && simpleIcon.getSvg() != null && simpleIcon.getSvg().length() > 0) {
String svg = simpleIcon.getSvg();
String adjustedColor = null;
if (!(this instanceof SocialBadge) && Brightness.of(labelColor) <= brightnessThreshold) {
adjustedColor = "whitesmoke";
}
if (simpleIcon.getHex() != null && simpleIcon.getHex().length() > 0) {
svg = svg.replace("<svg", String.format("<svg fill=\"%s\"", (simpleIcon.getHex().startsWith("#") ? simpleIcon.getHex() : "#".concat(simpleIcon.getHex()))));
String logoColor = (simpleIcon.getHex().startsWith("#") ? simpleIcon.getHex() : "#".concat(simpleIcon.getHex()));
if (this instanceof SocialBadge && Brightness.of(logoColor) > brightnessThreshold) {
adjustedColor = "#333";
}

if (adjustedColor == null) {
adjustedColor = logoColor;
}
}
if (adjustedColor != null) {
svg = svg.replace("<svg", String.format("<svg fill=\"%s\"", adjustedColor));
}

return "data:image/svg+xml;base64,".concat(Base64.getEncoder().encodeToString(svg.getBytes(StandardCharsets.UTF_8)));
}
}
Expand Down
53 changes: 39 additions & 14 deletions src/test/java/org/silentsoft/badge4j/BadgeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

public class BadgeTest {

Expand All @@ -34,48 +34,70 @@ public void logoTest() {

@Test
public void simpleTest() {
final double brightnessThreshold = 0.69;
for (Style style : Style.values()) {
String label = "hello".toLowerCase();
String message = "world".toLowerCase();

Function<String, String> buildLogo = (String logo) -> {
BiFunction<String, String, String> buildLogo = (String logo, String labelColor) -> {
if (logo.startsWith("data:")) {
return logo.toLowerCase();
}

labelColor = labelColor.trim();
labelColor = (NamedColor.nameOf(labelColor) != null) ? NamedColor.nameOf(labelColor).getHex() : (NamedColorAlias.nameOf(labelColor) != null) ? NamedColorAlias.nameOf(labelColor).getHex() : labelColor;
if (labelColor.startsWith("#") == false && org.silentsoft.csscolor4j.NamedColor.nameOf(labelColor) == null) {
labelColor = "#".concat(labelColor);
}

Icon simpleIcon = SimpleIcons.get(logo);
if (simpleIcon != null && simpleIcon.getSvg() != null && simpleIcon.getSvg().length() > 0) {
String svg = simpleIcon.getSvg();
String adjustedColor = null;
if (style != Style.Social && Brightness.of(labelColor) <= brightnessThreshold) {
adjustedColor = "whitesmoke";
}
if (simpleIcon.getHex() != null && simpleIcon.getHex().length() > 0) {
svg = svg.replace("<svg", String.format("<svg fill=\"%s\"", (simpleIcon.getHex().startsWith("#") ? simpleIcon.getHex() : "#".concat(simpleIcon.getHex()))));
String logoColor = (simpleIcon.getHex().startsWith("#") ? simpleIcon.getHex() : "#".concat(simpleIcon.getHex()));
if (style == Style.Social && Brightness.of(logoColor) > brightnessThreshold) {
adjustedColor = "#333";
}

if (adjustedColor == null) {
adjustedColor = logoColor;
}
}
if (adjustedColor != null) {
svg = svg.replace("<svg", String.format("<svg fill=\"%s\"", adjustedColor));
}
return "data:image/svg+xml;base64,".concat(Base64.getEncoder().encodeToString(svg.getBytes(StandardCharsets.UTF_8))).toLowerCase();
}

return null;
};

String label = "hello".toLowerCase();
String message = "world".toLowerCase();
String labelColor = "#555";

Consumer<String> test = (logo) -> {
{
String svg = Badge.builder().style(style).label(label).message(message).logo(logo).build().toLowerCase();
String svg = Badge.builder().style(style).label(label).message(message).labelColor(labelColor).logo(logo).build().toLowerCase();
Assertions.assertTrue(svg.contains(label));
Assertions.assertTrue(svg.contains(message));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo)));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo, labelColor)));
}
{
String[] links = new String[]{ "https://silentsoft.org" };
String svg = Badge.builder().style(style).label(label).message(message).logo(logo).links(links).build().toLowerCase();
String svg = Badge.builder().style(style).label(label).message(message).labelColor(labelColor).logo(logo).links(links).build().toLowerCase();
Assertions.assertTrue(svg.contains(label));
Assertions.assertTrue(svg.contains(message));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo)));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo, labelColor)));
Assertions.assertTrue(svg.contains(links[0]));
}
{
String[] links = new String[]{ "https://left.silentsoft.org", "https://right.silentsoft.org" };
String svg = Badge.builder().style(style).label(label).message(message).logo(logo).links(links).build().toLowerCase();
String svg = Badge.builder().style(style).label(label).message(message).labelColor(labelColor).logo(logo).links(links).build().toLowerCase();
Assertions.assertTrue(svg.contains(label));
Assertions.assertTrue(svg.contains(message));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo)));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo, labelColor)));
Assertions.assertTrue(svg.contains(links[0]));
Assertions.assertTrue(svg.contains(links[1]));
}
Expand All @@ -84,21 +106,24 @@ public void simpleTest() {
String svg = Badge.builder().style(style).label(label).message(message).logo(logo).color(namedColor.name()).labelColor(namedColor.name()).build().toLowerCase();
Assertions.assertTrue(svg.contains(label));
Assertions.assertTrue(svg.contains(message));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo)));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo, namedColor.name())));
Assertions.assertTrue(svg.contains(namedColor.getHex()));
}
for (NamedColorAlias namedColorAlias : NamedColorAlias.values()) {
String svg = Badge.builder().style(style).label(label).message(message).logo(logo).color(namedColorAlias.name()).labelColor(namedColorAlias.name()).build().toLowerCase();
Assertions.assertTrue(svg.contains(label));
Assertions.assertTrue(svg.contains(message));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo)));
Assertions.assertTrue(svg.contains(buildLogo.apply(logo, namedColorAlias.name())));
Assertions.assertTrue(svg.contains(namedColorAlias.getHex()));
}
}
};

test.accept("data:image/svg+xml;base64,Dummy123+LOGO456+data789=");
test.accept("simpleicons");
test.accept("apachecordova");
test.accept("github");
test.accept("twitter");
}
}

Expand Down

0 comments on commit 8c85384

Please sign in to comment.