// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/crest_contrast.hpp" #include #include namespace bmh::modern_ui { std::optional 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 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