Correctly handle colorizer special cases.
This commit is contained in:
parent
cc5ebf21e7
commit
cf5e41d81e
4 changed files with 77 additions and 22 deletions
|
|
@ -10,6 +10,20 @@
|
||||||
|
|
||||||
namespace style {
|
namespace style {
|
||||||
|
|
||||||
|
struct palette::FinalizeHelper {
|
||||||
|
not_null<const colorizer*> with;
|
||||||
|
base::flat_set<int> ignoreKeys;
|
||||||
|
base::flat_map<
|
||||||
|
int,
|
||||||
|
std::pair<colorizer::Color, colorizer::Color>> keepContrast;
|
||||||
|
};
|
||||||
|
|
||||||
|
palette::palette() = default;
|
||||||
|
|
||||||
|
palette::~palette() {
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
int palette::indexOfColor(style::color c) const {
|
int palette::indexOfColor(style::color c) const {
|
||||||
auto start = data(0);
|
auto start = data(0);
|
||||||
if (c._data >= start && c._data < start + kCount) {
|
if (c._data >= start && c._data < start + kCount) {
|
||||||
|
|
@ -19,8 +33,9 @@ int palette::indexOfColor(style::color c) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
color palette::colorAtIndex(int index) const {
|
color palette::colorAtIndex(int index) const {
|
||||||
Assert(_ready);
|
Expects(index >= 0 && index < kCount);
|
||||||
Assert(index >= 0 && index < kCount);
|
Expects(_ready);
|
||||||
|
|
||||||
return _colors[index];
|
return _colors[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -28,8 +43,9 @@ void palette::finalize(const colorizer &with) {
|
||||||
if (_ready) return;
|
if (_ready) return;
|
||||||
_ready = true;
|
_ready = true;
|
||||||
|
|
||||||
_colorizer = with ? &with : nullptr;
|
_finalizeHelper = PrepareFinalizeHelper(with);
|
||||||
palette_data::finalize(*this);
|
palette_data::finalize(*this);
|
||||||
|
_finalizeHelper = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void palette::finalize() {
|
void palette::finalize() {
|
||||||
|
|
@ -131,18 +147,18 @@ void palette::compute(int index, int fallbackIndex, TempColorData value) {
|
||||||
_status[index] = Status::Loaded;
|
_status[index] = Status::Loaded;
|
||||||
new (data(index)) internal::ColorData(*data(fallbackIndex));
|
new (data(index)) internal::ColorData(*data(fallbackIndex));
|
||||||
} else {
|
} else {
|
||||||
if (_colorizer && *_colorizer) {
|
if (!_finalizeHelper
|
||||||
colorize(
|
|| _finalizeHelper->ignoreKeys.contains(index)) {
|
||||||
//(index
|
|
||||||
// ? style::main_palette::data()[index - 1].name
|
|
||||||
// : qstr("transparent")),
|
|
||||||
value.r,
|
|
||||||
value.g,
|
|
||||||
value.b,
|
|
||||||
*_colorizer);
|
|
||||||
_status[index] = Status::Loaded;
|
|
||||||
} else {
|
|
||||||
_status[index] = Status::Created;
|
_status[index] = Status::Created;
|
||||||
|
} else {
|
||||||
|
const auto &with = *_finalizeHelper->with;
|
||||||
|
const auto i = _finalizeHelper->keepContrast.find(index);
|
||||||
|
if (i == end(_finalizeHelper->keepContrast)) {
|
||||||
|
colorize(value.r, value.g, value.b, with);
|
||||||
|
} else {
|
||||||
|
colorize(i->second, value.r, value.g, value.b, with);
|
||||||
|
}
|
||||||
|
_status[index] = Status::Loaded;
|
||||||
}
|
}
|
||||||
new (data(index)) internal::ColorData(value.r, value.g, value.b, value.a);
|
new (data(index)) internal::ColorData(value.r, value.g, value.b, value.a);
|
||||||
}
|
}
|
||||||
|
|
@ -158,6 +174,28 @@ void palette::setData(int index, const internal::ColorData &value) {
|
||||||
_status[index] = Status::Loaded;
|
_status[index] = Status::Loaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto palette::PrepareFinalizeHelper(const colorizer &with)
|
||||||
|
-> std::unique_ptr<FinalizeHelper> {
|
||||||
|
if (!with) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
auto result = std::make_unique<FinalizeHelper>(FinalizeHelper{
|
||||||
|
.with = &with,
|
||||||
|
});
|
||||||
|
result->ignoreKeys.reserve(with.ignoreKeys.size() + 1);
|
||||||
|
result->ignoreKeys.emplace(0);
|
||||||
|
for (const auto &key : with.ignoreKeys) {
|
||||||
|
if (const auto index = internal::GetPaletteIndex(key); index > 0) {
|
||||||
|
result->ignoreKeys.emplace(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const auto &[key, contrast] : with.keepContrast) {
|
||||||
|
if (const auto index = internal::GetPaletteIndex(key); index > 0) {
|
||||||
|
result->keepContrast.emplace(index, contrast);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
namespace main_palette {
|
namespace main_palette {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,10 @@ struct colorizer;
|
||||||
|
|
||||||
class palette : public palette_data {
|
class palette : public palette_data {
|
||||||
public:
|
public:
|
||||||
palette() = default;
|
palette();
|
||||||
palette(const palette &other) = delete;
|
palette(const palette &other) = delete;
|
||||||
palette &operator=(const palette &other);
|
palette &operator=(const palette &other);
|
||||||
~palette() {
|
~palette();
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray save() const;
|
QByteArray save() const;
|
||||||
bool load(const QByteArray &cache);
|
bool load(const QByteArray &cache);
|
||||||
|
|
@ -43,14 +41,18 @@ public:
|
||||||
color colorAtIndex(int index) const;
|
color colorAtIndex(int index) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct FinalizeHelper;
|
||||||
struct TempColorData { uchar r, g, b, a; };
|
struct TempColorData { uchar r, g, b, a; };
|
||||||
friend class palette_data;
|
friend class palette_data;
|
||||||
|
|
||||||
|
[[nodiscard]] static auto PrepareFinalizeHelper(const colorizer &with)
|
||||||
|
-> std::unique_ptr<FinalizeHelper>;
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
void compute(int index, int fallbackIndex, TempColorData value);
|
void compute(int index, int fallbackIndex, TempColorData value);
|
||||||
void setData(int index, const internal::ColorData &value);
|
void setData(int index, const internal::ColorData &value);
|
||||||
|
|
||||||
const colorizer *_colorizer = nullptr;
|
std::unique_ptr<FinalizeHelper> _finalizeHelper;
|
||||||
bool _ready = false;
|
bool _ready = false;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -101,9 +101,18 @@ void colorize(
|
||||||
const auto i = with.keepContrast.find(name);
|
const auto i = with.keepContrast.find(name);
|
||||||
if (i == end(with.keepContrast)) {
|
if (i == end(with.keepContrast)) {
|
||||||
colorize(r, g, b, with);
|
colorize(r, g, b, with);
|
||||||
return;
|
} else {
|
||||||
|
colorize(i->second, r, g, b, with);
|
||||||
}
|
}
|
||||||
const auto check = i->second.first;
|
}
|
||||||
|
|
||||||
|
void colorize(
|
||||||
|
const std::pair<colorizer::Color, colorizer::Color> &contrast,
|
||||||
|
uchar &r,
|
||||||
|
uchar &g,
|
||||||
|
uchar &b,
|
||||||
|
const colorizer &with) {
|
||||||
|
const auto check = contrast.first;
|
||||||
const auto rgb = QColor(int(r), int(g), int(b));
|
const auto rgb = QColor(int(r), int(g), int(b));
|
||||||
const auto changed = colorize(rgb, with);
|
const auto changed = colorize(rgb, with);
|
||||||
const auto checked = colorize(check, with).value_or(check);
|
const auto checked = colorize(check, with).value_or(check);
|
||||||
|
|
@ -120,7 +129,7 @@ void colorize(
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto replace = i->second.second;
|
const auto replace = contrast.second;
|
||||||
const auto result = colorize(replace, with).value_or(replace);
|
const auto result = colorize(replace, with).value_or(replace);
|
||||||
FillColorizeResult(
|
FillColorizeResult(
|
||||||
r,
|
r,
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,12 @@ void colorize(
|
||||||
uchar &g,
|
uchar &g,
|
||||||
uchar &b,
|
uchar &b,
|
||||||
const colorizer &with);
|
const colorizer &with);
|
||||||
|
void colorize(
|
||||||
|
const std::pair<colorizer::Color, colorizer::Color> &contrast,
|
||||||
|
uchar &r,
|
||||||
|
uchar &g,
|
||||||
|
uchar &b,
|
||||||
|
const colorizer &with);
|
||||||
void colorize(QImage &image, const colorizer &with);
|
void colorize(QImage &image, const colorizer &with);
|
||||||
|
|
||||||
[[nodiscard]] std::optional<QColor> colorize(
|
[[nodiscard]] std::optional<QColor> colorize(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue