Freeciv21
Develop your civilization from humble roots to a global empire
colorizer.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2022 Louis Moureaux <m_louis30@yahoo.com>
3  *
4  * SPDX-License-Identifier: GPLv3-or-later
5  */
6 
7 #include "colorizer.h"
8 
9 #include <QBrush>
10 #include <QPainter>
11 
12 namespace freeciv {
13 
25 colorizer::colorizer(const QPixmap &base, int hue_to_replace,
26  QObject *parent)
27  : QObject(parent), m_base(base), m_base_image(base.toImage()),
28  m_hue_to_replace(hue_to_replace)
29 {
30 }
31 
36 const QPixmap *colorizer::pixmap(const QColor &color) const
37 {
38  // Easy cases with nothing to do
39  if (m_hue_to_replace < 0 || !color.isValid()) {
40  return &m_base;
41  }
42 
43  // Draw it if we don't have it yet
44  if (m_cache.count(color.rgba()) == 0) {
45  auto new_hue = color.hslHue();
46  auto image = m_base_image.copy();
47 
48  // Iterate through pixels and replace the hue
49  for (int x = 0; x < image.width(); ++x) {
50  for (int y = 0; y < image.height(); ++y) {
51  auto pixel = image.pixelColor(x, y);
52  if (pixel.hslHue() == m_hue_to_replace) {
53  image.setPixelColor(x, y,
54  QColor::fromHsl(new_hue, pixel.hslSaturation(),
55  pixel.lightness()));
56  }
57  }
58  }
59 
60  m_cache[color.rgba()] = QPixmap::fromImage(image);
61  }
62 
63  return &m_cache[color.rgba()];
64 }
65 
66 } // namespace freeciv
std::map< QRgb, QPixmap > m_cache
Definition: colorizer.h:34
const QPixmap * pixmap(const QColor &color) const
Returns a pixmap with some pixels changed to the target color.
Definition: colorizer.cpp:36
colorizer(const QPixmap &base, int hue_to_replace, QObject *parent=nullptr)
Creates a colorizer that will replace every pixel of the given hue.
Definition: colorizer.cpp:25
QImage m_base_image
Definition: colorizer.h:32
static void base(QVariant data1, QVariant data2)
Action "Build Base" for choice dialog.
Definition: dialogs.cpp:2393
Definition: path.cpp:10