]> git.sesse.net Git - nageru/blob - vu_common.cpp
Work around some false positives found by Coverity Scan.
[nageru] / vu_common.cpp
1 #include "vu_common.h"
2
3 #include <math.h>
4 #include <algorithm>
5
6 #include <QBrush>
7 #include <QColor>
8 #include <QPainter>
9
10 using namespace std;
11
12 int lufs_to_pos(float level_lu, int height)
13 {       
14         const float min_level = 9.0f;    // y=0 is top of screen, so “min” is the loudest level.
15         const float max_level = -18.0f;
16
17         // Handle -inf.
18         if (level_lu < max_level) {
19                 return height - 1;
20         }
21
22         int y = lrintf(height * (level_lu - min_level) / (max_level - min_level));
23         y = max(y, 0);
24         y = min(y, height - 1);
25         return y;
26 }
27
28 void draw_vu_meter(QPainter &painter, float range_low_lu, float range_high_lu, int width, int height, int margin)
29 {
30         painter.fillRect(margin, 0, width - 2 * margin, height, Qt::black);
31
32         // TODO: QLinearGradient is not gamma-correct; we might want to correct for that.
33         QLinearGradient on(0, 0, 0, height);
34         on.setColorAt(0.0f, QColor(255, 0, 0));
35         on.setColorAt(0.5f, QColor(255, 255, 0));
36         on.setColorAt(1.0f, QColor(0, 255, 0));
37         QColor off(80, 80, 80);
38
39         int min_on_y = lufs_to_pos(range_high_lu, height);
40         int max_on_y = lufs_to_pos(range_low_lu, height);
41
42         // Draw bars colored up until the level, then gray from there.
43         for (int level = -18; level < 9; ++level) {
44                 int min_y = lufs_to_pos(level + 1.0f, height) + 1;
45                 int max_y = lufs_to_pos(level, height) - 1;
46
47                 painter.fillRect(margin, min_y, width - 2 * margin, max_y - min_y, off);
48                 int min_draw_y = max(min_y, min_on_y);
49                 int max_draw_y = min(max_y, max_on_y);
50                 if (min_draw_y < max_draw_y) {
51                         painter.fillRect(margin, min_draw_y, width - 2 * margin, max_draw_y - min_draw_y, on);
52                 }
53         }
54 }