1 #include "rgb_histogram.h"
11 static constexpr int margin_left = 16;
12 static constexpr int margin_top = 14;
13 static constexpr int margin_right = 14;
14 static constexpr int margin_bottom = 25;
16 RGBHistogram::RGBHistogram(QWidget *parent)
17 : QWidget(parent), red(256, 0.0), green(256, 0.0), blue(256, 0.0)
21 void RGBHistogram::set_histograms(std::vector<double> red, std::vector<double> green, std::vector<double> blue)
23 assert(red.size() == 256);
24 assert(green.size() == 256);
25 assert(blue.size() == 256);
26 this->red = std::move(red);
27 this->green = std::move(green);
28 this->blue = std::move(blue);
32 void RGBHistogram::paintEvent(QPaintEvent *event)
34 QPainter painter(this);
35 painter.fillRect(0, 0, width(), height(), Qt::white);
36 painter.setRenderHints(QPainter::Antialiasing);
39 for (unsigned i = 0; i < 256; ++i) {
40 max = std::max(max, red[i]);
41 max = std::max(max, green[i]);
42 max = std::max(max, blue[i]);
45 // Draw the graphs. (We'd like to get rid of the bottom line, but that's just cosmetic.)
46 painter.setPen(QPen(Qt::red));
47 painter.setBrush(QBrush(QColor(255, 127, 127, 80)));
48 draw(red, max, Qt::GlobalColor::red, &painter);
50 painter.setPen(QPen(Qt::green));
51 painter.setBrush(QBrush(QColor(127, 255, 127, 80)));
52 draw(green, max, Qt::GlobalColor::green, &painter);
54 painter.setPen(QPen(Qt::blue));
55 painter.setBrush(QBrush(QColor(127, 127, 255, 80)));
56 draw(blue, max, Qt::GlobalColor::blue, &painter);
58 // Draw the tick lines.
59 int w = (width() - 1) - margin_left - margin_right;
60 painter.setPen(QPen(Qt::black));
61 painter.setBrush(QBrush());
62 for (int i = 0; i < 256; i += 10) {
63 int th = (i % 50 == 0) ? 5 : 2;
64 double x = (i / 255.0) * w + margin_left;
65 double y = (height() - 1) - margin_top - margin_bottom;
66 painter.drawLine(QPointF(x, y + margin_top), QPointF(x, y + margin_top - th));
69 // And the axis labels.
70 for (int i = 0; i < 256; i += 50) {
72 snprintf(buf, sizeof(buf), "%d", i);
74 double x = (i / 255.0) * w + margin_left;
75 double y = (height() - 1) - margin_top - margin_bottom;
76 painter.drawText(QRectF(x - 20, y + margin_top, 40, margin_bottom), Qt::AlignCenter | Qt::AlignTop, QString(buf));
80 void RGBHistogram::draw(const vector<double> &v, double max, Qt::GlobalColor color, QPainter *painter)
82 int w = (width() - 1) - margin_left - margin_right;
83 int h = (height() - 1) - margin_top - margin_bottom;
86 points[0] = QPointF(margin_left, margin_top + h);
87 for (int i = 0; i < 256; ++i) {
88 double x = (i / 255.0) * w + margin_left;
89 double y = (1.0 - (v[i] / max)) * h + margin_top;
90 points[i + 1] = QPointF(x, y);
92 points[257] = QPointF(margin_left + w, margin_top + h);
93 painter->drawPolygon(points, 258);