Fix white crest contrast on Silver surfaces (by Codex)

This commit is contained in:
Codex committed 2026-10-28 17:54:41 +01:00
1 parent d615f03e15
commit 4b6c18f654
7 files changed
+176 -2

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-3.0-only
add_library(bmh_modern_ui_core STATIC catalog.cpp engine_dto.cpp model.cpp demo_career.cpp demo_national_ui.cpp gui_assets.cpp desktop.cpp demo_sports.cpp demo_calendar.cpp demo_reports.cpp demo_almanac.cpp demo_finance.cpp demo_environment.cpp demo_advertising.cpp demo_toto.cpp demo_lineup.cpp demo_live.cpp demo_post_match.cpp demo_transfer.cpp demo_career_reports.cpp demo_rankings.cpp demo_stadium.cpp demo_statistics.cpp demo_news.cpp news_catalog.cpp news_journal.cpp demo_settings.cpp ui_preferences.cpp demo_widgets.cpp demo_files.cpp demo_opening.cpp)
add_library(bmh_modern_ui_core STATIC catalog.cpp crest_contrast.cpp engine_dto.cpp model.cpp demo_career.cpp demo_national_ui.cpp gui_assets.cpp desktop.cpp demo_sports.cpp demo_calendar.cpp demo_reports.cpp demo_almanac.cpp demo_finance.cpp demo_environment.cpp demo_advertising.cpp demo_toto.cpp demo_lineup.cpp demo_live.cpp demo_post_match.cpp demo_transfer.cpp demo_career_reports.cpp demo_rankings.cpp demo_stadium.cpp demo_statistics.cpp demo_news.cpp news_catalog.cpp news_journal.cpp demo_settings.cpp ui_preferences.cpp demo_widgets.cpp demo_files.cpp demo_opening.cpp)
target_include_directories(bmh_modern_ui_core PUBLIC ../..)
target_link_libraries(bmh_modern_ui_core PUBLIC bmh_asset_io)
add_library(bmh_modern_ui_mods STATIC mod_library.cpp)
+44
View File
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "modern/ui/crest_contrast.hpp"
#include <algorithm>
#include <array>
namespace bmh::modern_ui {
std::optional<editor::images::Raster>
crest_for_light_surface(const editor::images::Raster &source) {
if (source.width <= 0 || source.height <= 0 || source.pixels.size() % 4 != 0)
return std::nullopt;
const auto count = uint64_t(source.width) * uint64_t(source.height);
if (count != source.pixels.size() / 4)
return std::nullopt;
uint64_t transparent = 0, coverage = 0, brightness = 0, dark = 0;
for (size_t i = 0; i < source.pixels.size(); i += 4) {
const int r = source.pixels[i], g = source.pixels[i + 1],
b = source.pixels[i + 2], a = source.pixels[i + 3];
if (a == 0) ++transparent;
if (a >= 16 && std::max({r, g, b}) - std::min({r, g, b}) > 12)
return std::nullopt; // Retain even small coloured details and brand colours.
const int luma = (54 * r + 183 * g + 19 * b + 128) / 256;
if (a >= 64 && luma < 64)
return std::nullopt; // Preserve even a small visible black logo detail.
coverage += a;
brightness += uint64_t(luma) * a;
if (luma < 64) dark += a;
}
// Alpha-weighting excludes invisible RGB and tolerates antialiasing and
// embossed grey shadows. Black/white logos and white rectangular backdrops
// do not qualify. This is only called for crests, never for photos or GUI art.
if (!coverage || transparent * 20 < count || brightness < coverage * 224 ||
dark * 100 > coverage)
return std::nullopt;
auto result = source;
constexpr std::array<unsigned, 3> ink{0x23, 0x34, 0x43}; // Silver --text.
for (size_t i = 0; i < result.pixels.size(); i += 4)
if (result.pixels[i + 3])
for (size_t c = 0; c < ink.size(); ++c)
result.pixels[i + c] = uint8_t((unsigned(result.pixels[i + c]) * ink[c] + 127) / 255);
return result;
}
} // namespace bmh::modern_ui
+13
View File
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include "editor/images.hpp"
#include <optional>
namespace bmh::modern_ui {
// A display-only variant for transparent, predominantly white monochrome crests.
// Coloured, dark, opaque and empty images keep their original appearance.
// The source, dimensions, shading and alpha coverage are preserved.
std::optional<editor::images::Raster>
crest_for_light_surface(const editor::images::Raster &source);
} // namespace bmh::modern_ui
+21 -1
View File
@@ -5,6 +5,7 @@
#include "launch/rml_path.hpp"
#include "launch/rml_video.hpp"
#include "modern/ui/controls.hpp"
#include "modern/ui/crest_contrast.hpp"
#include "modern/ui/engine_bridge.hpp"
#include "modern/ui/live_match_info.hpp"
#include "modern/ui/script_locator.hpp"
@@ -301,6 +302,25 @@ public:
const auto &r = entries_.at(found->second).raster;
return {found->second, r.width, r.height};
}
Picture crest(const std::string &key, bool light_surface) {
const auto source = image(key, true);
if (!light_surface || source.token.empty()) return source;
const auto identity = "light-crest:" + source.token;
auto found = keys_.find(identity);
if (found == keys_.end()) {
auto variant = crest_for_light_surface(entries_.at(source.token).raster);
auto token = source.token;
if (variant) {
token = "modern-image-" + std::to_string(serial_++) + ".png";
entries_[token] = {key, true, 512, std::move(*variant)};
}
// Also cache the decision to keep a coloured/original texture. Theme
// changes reuse both variants; world() invalidates them together.
found = keys_.emplace(identity, token).first;
}
const auto &raster = entries_.at(found->second).raster;
return {found->second, raster.width, raster.height};
}
Rml::TextureHandle LoadTexture(Rml::Vector2i &dimensions,
const Rml::String &source) override {
const auto token = editor::utf8(editor::path(source).filename());
@@ -528,7 +548,7 @@ class App final : public Rml::EventListener {
"dp;\"/></div>";
}
std::string crest(const Json *c, double size) {
return c ? img(c->value("crest", ""), size, size, true) : "";
return c ? picture(renderer_.crest(c->value("crest", ""), theme_ == "silver"), size, size) : "";
}
std::string glyph(const std::string &name, double size) {
return img("icon." + name + (theme_ == "silver" ? "-ink" : ""), size, size,