annotate layer/ColourDatabase.h @ 1445:ad86aa712d11 single-point

Add getNearbyColourIndex to ColourDatabase; tweak getContrastingColour for bright colours; add comments
author Chris Cannam
date Tue, 30 Apr 2019 14:02:03 +0100
parents 05d614f6e46d
children 57a4ee52ad69
rev   line source
Chris@376 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@376 2
Chris@376 3 /*
Chris@376 4 Sonic Visualiser
Chris@376 5 An audio file viewer and annotation editor.
Chris@376 6 Centre for Digital Music, Queen Mary, University of London.
Chris@376 7 This file copyright 2007 QMUL.
Chris@376 8
Chris@376 9 This program is free software; you can redistribute it and/or
Chris@376 10 modify it under the terms of the GNU General Public License as
Chris@376 11 published by the Free Software Foundation; either version 2 of the
Chris@376 12 License, or (at your option) any later version. See the file
Chris@376 13 COPYING included with this distribution for more information.
Chris@376 14 */
Chris@376 15
Chris@1407 16 #ifndef SV_COLOUR_DATABASE_H
Chris@1407 17 #define SV_COLOUR_DATABASE_H
Chris@376 18
Chris@376 19 #include <QObject>
Chris@376 20 #include <QString>
Chris@376 21 #include <QColor>
Chris@376 22 #include <QSize>
Chris@376 23 #include <QPixmap>
Chris@376 24 #include <vector>
Chris@376 25
Chris@376 26 class ColourDatabase : public QObject
Chris@376 27 {
Chris@376 28 Q_OBJECT
Chris@376 29
Chris@376 30 public:
Chris@376 31 static ColourDatabase *getInstance();
Chris@376 32
Chris@1445 33 /**
Chris@1445 34 * Return the number of colours in the database.
Chris@1445 35 */
Chris@376 36 int getColourCount() const;
Chris@1445 37
Chris@1445 38 /**
Chris@1445 39 * Return the name of the colour at index c.
Chris@1445 40 */
Chris@376 41 QString getColourName(int c) const;
Chris@1445 42
Chris@1445 43 /**
Chris@1445 44 * Return the colour at index c.
Chris@1445 45 */
Chris@376 46 QColor getColour(int c) const;
Chris@1445 47
Chris@1445 48 /**
Chris@1445 49 * Return the colour with the given name, if found in the
Chris@1445 50 * database. If not found, return Qt::black.
Chris@1445 51 */
Chris@376 52 QColor getColour(QString name) const;
Chris@1445 53
Chris@1445 54 /**
Chris@1445 55 * Return the index of the colour with the given name, if found in
Chris@1445 56 * the database. If not found, return -1.
Chris@1445 57 */
Chris@1445 58 int getColourIndex(QString name) const;
Chris@1445 59
Chris@1445 60 /**
Chris@1445 61 * Return the index of the given colour, if found in the
Chris@1445 62 * database. If not found, return -1. Note that it is possible for
Chris@1445 63 * a colour to appear more than once in the database: names have
Chris@1445 64 * to be unique in the database, but colours don't. This always
Chris@1445 65 * returns the first match.
Chris@1445 66 */
Chris@1445 67 int getColourIndex(QColor c) const;
Chris@1445 68
Chris@1445 69 /**
Chris@1445 70 * Return true if the given colour exists in the database.
Chris@1445 71 */
Chris@376 72 bool haveColour(QColor c) const;
Chris@376 73
Chris@1445 74 /**
Chris@1445 75 * Return the index of the colour in the database that is closest
Chris@1445 76 * to the given one, by some simplistic measure (Manhattan
Chris@1445 77 * distance in RGB space). This always returns some valid index,
Chris@1445 78 * unless the database is empty, in which case it returns -1.
Chris@1445 79 */
Chris@1445 80 int getNearbyColourIndex(QColor c) const;
Chris@1445 81
Chris@1445 82 /**
Chris@1445 83 * Add a colour to the database, with the associated name. Return
Chris@1445 84 * the index of the colour in the database. Names are unique
Chris@1445 85 * within the database: if another colour exists already with the
Chris@1445 86 * given name, its colour value is replaced with the given
Chris@1445 87 * one. Colours may appear more than once under different names.
Chris@1445 88 */
Chris@1445 89 int addColour(QColor c, QString name);
Chris@1445 90
Chris@1445 91 /**
Chris@1445 92 * Remove the colour with the given name from the database.
Chris@1445 93 */
Chris@1445 94 void removeColour(QString);
Chris@1445 95
Chris@1445 96 /**
Chris@1445 97 * Return true if the colour at index c is marked as using a dark
Chris@1445 98 * background. Such colours are presumably "bright" ones, but all
Chris@1445 99 * this reports is whether the colour has been marked with
Chris@1445 100 * setUseDarkBackground, not any intrinsic property of the colour.
Chris@1445 101 */
Chris@376 102 bool useDarkBackground(int c) const;
Chris@1445 103
Chris@1445 104 /**
Chris@1445 105 * Mark the colour at index c as using a dark
Chris@1445 106 * background. Generally this should be called for "bright"
Chris@1445 107 * colours.
Chris@1445 108 */
Chris@376 109 void setUseDarkBackground(int c, bool dark);
Chris@376 110
Chris@1445 111 /**
Chris@1445 112 * Return a colour that contrasts with the one at index c,
Chris@1445 113 * according to some simplistic algorithm. The returned colour is
Chris@1445 114 * not necessarily in the database; pass it to
Chris@1445 115 * getNearbyColourIndex if you need one that is.
Chris@1445 116 */
Chris@376 117 QColor getContrastingColour(int c) const;
Chris@376 118
Chris@376 119 // for use in XML export
Chris@376 120 void getStringValues(int index,
Chris@376 121 QString &colourName,
Chris@376 122 QString &colourSpec,
Chris@376 123 QString &darkbg) const;
Chris@376 124
Chris@376 125 // for use in XML import
Chris@376 126 int putStringValues(QString colourName,
Chris@376 127 QString colourSpec,
Chris@376 128 QString darkbg);
Chris@376 129
Chris@376 130 // for use by PropertyContainer getPropertyRangeAndValue methods
Chris@376 131 void getColourPropertyRange(int *min, int *max) const;
Chris@376 132
Chris@1445 133 /**
Chris@1445 134 * Generate a swatch pixmap illustrating the colour at index c.
Chris@1445 135 */
Chris@1445 136 QPixmap getExamplePixmap(int c, QSize size) const;
Chris@376 137
Chris@376 138 signals:
Chris@376 139 void colourDatabaseChanged();
Chris@376 140
Chris@376 141 protected:
Chris@376 142 ColourDatabase();
Chris@376 143
Chris@376 144 struct ColourRec {
Chris@376 145 QColor colour;
Chris@376 146 QString name;
Chris@376 147 bool darkbg;
Chris@376 148 };
Chris@376 149
Chris@376 150 typedef std::vector<ColourRec> ColourList;
Chris@376 151 ColourList m_colours;
Chris@376 152
Chris@376 153 static ColourDatabase m_instance;
Chris@376 154 };
Chris@376 155
Chris@376 156 #endif