File: ColourDatabase.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (320 lines) | stat: -rw-r--r-- 8,264 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2007 QMUL.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "ColourDatabase.h"
#include "base/XmlExportable.h"

#include <QPainter>

#include <memory>

//#define DEBUG_COLOUR_DATABASE 1

namespace sv {

ColourDatabase *
ColourDatabase::getInstance()
{
    static std::unique_ptr<ColourDatabase> instance;
    static std::once_flag f;
    std::call_once(f, [&]() {
        instance = std::unique_ptr<ColourDatabase>(new ColourDatabase());
    });
    return instance.get();
}

ColourDatabase::ColourDatabase()
{
}

int
ColourDatabase::getColourCount() const
{
    return int(m_colours.size());
}

QString
ColourDatabase::getColourName(int c) const
{
    if (!in_range_for(m_colours, c)) return "";
    return m_colours[c].name;
}

QColor
ColourDatabase::getColour(int c) const
{
    if (!in_range_for(m_colours, c)) return Qt::black;
    return m_colours[c].colour;
}

QColor
ColourDatabase::getColour(QString name) const
{
    for (auto &c: m_colours) {
        if (c.name == name) return c.colour;
    }

    return Qt::black;
}

int
ColourDatabase::getColourIndex(QString name) const
{
    int index = 0;

#ifdef DEBUG_COLOUR_DATABASE
    SVDEBUG << "ColourDatabase::getColourIndex(" << name << "): know " << m_colours.size() << " colours" << endl;
#endif

    for (auto &c: m_colours) {
#ifdef DEBUG_COLOUR_DATABASE
        SVDEBUG << c.name << endl;
#endif
        if (c.name == name) return index;
        ++index;
    }

    return -1;
}

int
ColourDatabase::getColourIndex(QColor col) const
{
    int index = 0;
    for (auto &c: m_colours) {
        if (c.colour == col) return index;
        ++index;
    }

    return -1;
}

int
ColourDatabase::getNearbyColourIndex(QColor col, WithBackgroundMode mode) const
{
    int index = -1;
    int closestIndex = -1;
    double closestDistance = 0;

    for (auto &c: m_colours) {

        ++index;

        if (mode == WithDarkBackground && !c.darkbg) {
#ifdef DEBUG_COLOUR_DATABASE
            SVDEBUG << "getNearbyColourIndex: dark background requested, skipping " << c.colour.name() << endl;
#endif
            continue;
        }
        if (mode == WithLightBackground && c.darkbg) {
#ifdef DEBUG_COLOUR_DATABASE
            SVDEBUG << "getNearbyColourIndex: light background requested, skipping " << c.colour.name() << endl;
#endif
            continue;
        }

        // This distance formula is "one of the better low-cost
        // approximations" according to
        // https://en.wikipedia.org/w/index.php?title=Color_difference&oldid=936888327
        
        double r1 = col.red(), r2 = c.colour.red();
        double g1 = col.green(), g2 = c.colour.green();
        double b1 = col.blue(), b2 = c.colour.blue();

        double rav = (r1 + r2) / 2.0;
        double rterm = (2.0 + rav / 256.0) * (r1 - r2) * (r1 - r2);
        double gterm = 4.0 * (g1 - g2) * (g1 - g2);
        double bterm = (2.0 + (255 - rav) / 256.0) * (b1 - b2) * (b1 - b2);
        
        double distance = sqrt(rterm + gterm + bterm);

#ifdef DEBUG_COLOUR_DATABASE
        SVDEBUG << "getNearbyColourIndex: comparing " << c.colour.name()
                << " to " << col.name() << ": distance = " << distance << endl;
#endif
        if (closestIndex < 0 || distance < closestDistance) {
            closestIndex = index;
            closestDistance = distance;
#ifdef DEBUG_COLOUR_DATABASE
            SVDEBUG << "(this is the best so far)" << endl;
#endif
        }
    }

#ifdef DEBUG_COLOUR_DATABASE
    SVDEBUG << "returning " << closestIndex << endl;
#endif
    return closestIndex;
}

QColor
ColourDatabase::getContrastingColour(int c) const
{
    QColor col = getColour(c);
    QColor contrasting = Qt::red;
    bool dark = (col.red() < 240 && col.green() < 240 && col.blue() < 240);
    if (dark) {
        if (col.red() > col.blue()) {
            if (col.green() > col.blue()) {
                contrasting = Qt::blue;
            } else {
                contrasting = Qt::yellow;
            }
        } else {
            if (col.green() > col.blue()) {
                contrasting = Qt::yellow;
            } else {
                contrasting = Qt::red;
            }
        }
    } else {
        if (col.red() > 230 && col.green() > 230 && col.blue() > 230) {
            contrasting = QColor(30, 150, 255);
        } else {
            contrasting = QColor(255, 188, 80);
        }
    }
#ifdef DEBUG_COLOUR_DATABASE
    SVDEBUG << "getContrastingColour(" << col.name() << "): dark = " << dark
            << ", returning " << contrasting.name() << endl;
#endif
    return contrasting;
}

bool
ColourDatabase::useDarkBackground(int c) const
{
    if (!in_range_for(m_colours, c)) return false;
    return m_colours[c].darkbg;
}

void
ColourDatabase::setUseDarkBackground(int c, bool dark)
{
    if (!in_range_for(m_colours, c)) return;
    if (m_colours[c].darkbg != dark) {
        m_colours[c].darkbg = dark;
        emit colourDatabaseChanged();
    }
}

int
ColourDatabase::addColour(QColor c, QString name)
{
    int index = 0;

#ifdef DEBUG_COLOUR_DATABASE
    SVDEBUG << "ColourDatabase::addColour(" << c.name() << ", " << name << ")"
            << endl;
#endif
    
    for (ColourList::iterator i = m_colours.begin();
         i != m_colours.end(); ++i) {
        if (i->name == name) {
            i->colour = c;
            return index;
        }
        ++index;
    }

    ColourRec rec;
    rec.colour = c;
    rec.name = name;
    rec.darkbg = false;
    m_colours.push_back(rec);
    emit colourDatabaseChanged();
    return index;
}

void
ColourDatabase::removeColour(QString name)
{
    for (ColourList::iterator i = m_colours.begin();
         i != m_colours.end(); ++i) {
        if (i->name == name) {
            m_colours.erase(i);
            return;
        }
    }
}

void
ColourDatabase::getStringValues(int index,
                                QString &colourName,
                                QString &colourSpec,
                                QString &darkbg) const
{
    colourName = "";
    colourSpec = "";
    if (!in_range_for(m_colours, index)) return;

    colourName = getColourName(index);
    QColor c = getColour(index);
    colourSpec = XmlExportable::encodeColour(c.red(), c.green(), c.blue());
    darkbg = useDarkBackground(index) ? "true" : "false";
}

int
ColourDatabase::putStringValues(QString colourName,
                                QString colourSpec,
                                QString darkbg)
{
    int index = -1;
    if (colourSpec != "") {
        QColor colour(colourSpec);
        index = getColourIndex(colour);
        if (index < 0) {
            index = addColour(colour,
                              colourName == "" ? colourSpec : colourName);
        }
    } else if (colourName != "") {
        index = getColourIndex(colourName);
    }
    if (index >= 0) {
        setUseDarkBackground(index, darkbg == "true");
    }
    return index;
}

void
ColourDatabase::getColourPropertyRange(int *min, int *max) const
{
    ColourDatabase *db = getInstance();
    if (min) *min = 0;
    if (max) {
        *max = 0;
        if (db->getColourCount() > 0) *max = db->getColourCount()-1;
    }
}

QPixmap
ColourDatabase::getExamplePixmap(int index, QSize size) const
{
    QPixmap pmap(size);
    pmap.fill(useDarkBackground(index) ? Qt::black : Qt::white);
    QPainter paint(&pmap);
    QColor colour(getColour(index));
    paint.setPen(colour);
    paint.setBrush(colour);
    int margin = 2;
    if (size.width() < 4 || size.height() < 4) margin = 0;
    else if (size.width() < 8 || size.height() < 8) margin = 1;
    paint.drawRect(margin, margin,
                   size.width() - margin*2 - 1, size.height() - margin*2 - 1);
    return pmap;
}

} // end namespace sv