Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions javascripts/discourse/initializers/tag-icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function iconTagRenderer(tag, params) {
tagElement.prepend(iconElement);
tagElement.classList.add("discourse-tag--tag-icons-style");
tagElement.style.setProperty("--color1", color ?? "");

tagElement.style.setProperty("--color2", color ? contrastColor(color) : "");

return tagElement.outerHTML;
Expand Down
7 changes: 6 additions & 1 deletion javascripts/discourse/lib/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const hexToRgb = (hex) => {
}

if (!/^[0-9a-fA-F]{6}$/.test(value)) {
throw new Error(`Invalid hex color: ${hex}`);
// eslint-disable-next-line no-console
console.warn(`Invalid hex color: ${hex}`);
return false;
}

const num = parseInt(value, 16);
Expand All @@ -32,5 +34,8 @@ const luminance = (rgb) => {

export const contrastColor = (hexColor) => {
const rgb = hexToRgb(hexColor);
if (!rgb) {
return "";
}
return luminance(rgb) >= 0.45 ? "#000d" : "#fffd";
};
12 changes: 12 additions & 0 deletions test/acceptance/tag-icons-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ acceptance("Topic with tags", function (needs) {
const el = queryAll(".discourse-tags a.discourse-tag .tag-icon .d-icon")[0];
assert.ok(el.classList.contains("d-icon-star"), "tag matches correct icon");
});

test("Ensure non-HEX colors do not throw an error", async function (assert) {
settings.tag_icon_list = "Tag2,star,rgba(0,0,0)";

await visit("/t/internationalization-localization/280");

assert.ok(queryAll(".title-wrapper .discourse-tags"), "it has tags");
assert.ok(
queryAll(".discourse-tags a.discourse-tag .tag-icon").length,
"it has tag icon"
);
});
});