Mercurial > hg > svgui
comparison layer/ColourDatabase.cpp @ 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 | f5566f7271fe |
children | 57a4ee52ad69 |
comparison
equal
deleted
inserted
replaced
1444:8e6a24110996 | 1445:ad86aa712d11 |
---|---|
16 #include "ColourDatabase.h" | 16 #include "ColourDatabase.h" |
17 #include "base/XmlExportable.h" | 17 #include "base/XmlExportable.h" |
18 | 18 |
19 #include <QPainter> | 19 #include <QPainter> |
20 | 20 |
21 //#define DEBUG_COLOUR_DATABASE 1 | |
22 | |
21 ColourDatabase | 23 ColourDatabase |
22 ColourDatabase::m_instance; | 24 ColourDatabase::m_instance; |
23 | 25 |
24 ColourDatabase * | 26 ColourDatabase * |
25 ColourDatabase::getInstance() | 27 ColourDatabase::getInstance() |
83 } | 85 } |
84 | 86 |
85 return -1; | 87 return -1; |
86 } | 88 } |
87 | 89 |
90 int | |
91 ColourDatabase::getNearbyColourIndex(QColor col) const | |
92 { | |
93 int index = 0; | |
94 int closestIndex = -1; | |
95 int closestDistance = 0; | |
96 | |
97 for (auto &c: m_colours) { | |
98 int distance = | |
99 std::abs(col.red() - c.colour.red()) + | |
100 std::abs(col.green() - c.colour.green()) + | |
101 std::abs(col.blue() - c.colour.blue()); | |
102 #ifdef DEBUG_COLOUR_DATABASE | |
103 SVDEBUG << "getNearbyColourIndex: comparing " << c.colour.name() | |
104 << " to " << col.name() << ": distance = " << distance << endl; | |
105 #endif | |
106 if (closestIndex < 0 || distance < closestDistance) { | |
107 closestIndex = index; | |
108 closestDistance = distance; | |
109 #ifdef DEBUG_COLOUR_DATABASE | |
110 SVDEBUG << "(this is the best so far)" << endl; | |
111 #endif | |
112 } | |
113 ++index; | |
114 } | |
115 | |
116 #ifdef DEBUG_COLOUR_DATABASE | |
117 SVDEBUG << "returning " << closestIndex << endl; | |
118 #endif | |
119 return closestIndex; | |
120 } | |
121 | |
88 QColor | 122 QColor |
89 ColourDatabase::getContrastingColour(int c) const | 123 ColourDatabase::getContrastingColour(int c) const |
90 { | 124 { |
91 QColor col = getColour(c); | 125 QColor col = getColour(c); |
92 if (col.red() > col.blue()) { | 126 QColor contrasting = Qt::red; |
93 if (col.green() > col.blue()) { | 127 bool dark = (col.red() < 240 && col.green() < 240 && col.blue() < 240); |
94 return Qt::blue; | 128 if (dark) { |
129 if (col.red() > col.blue()) { | |
130 if (col.green() > col.blue()) { | |
131 contrasting = Qt::blue; | |
132 } else { | |
133 contrasting = Qt::yellow; | |
134 } | |
95 } else { | 135 } else { |
96 return Qt::yellow; | 136 if (col.green() > col.blue()) { |
137 contrasting = Qt::yellow; | |
138 } else { | |
139 contrasting = Qt::red; | |
140 } | |
97 } | 141 } |
98 } else { | 142 } else { |
99 if (col.green() > col.blue()) { | 143 if (col.red() > 230 && col.green() > 230 && col.blue() > 230) { |
100 return Qt::yellow; | 144 contrasting = QColor(30, 150, 255); |
101 } else { | 145 } else { |
102 return Qt::red; | 146 contrasting = QColor(255, 188, 80); |
103 } | 147 } |
104 } | 148 } |
105 return Qt::red; | 149 #ifdef DEBUG_COLOUR_DATABASE |
150 SVDEBUG << "getContrastingColour(" << col.name() << "): dark = " << dark | |
151 << ", returning " << contrasting.name() << endl; | |
152 #endif | |
153 return contrasting; | |
106 } | 154 } |
107 | 155 |
108 bool | 156 bool |
109 ColourDatabase::useDarkBackground(int c) const | 157 ColourDatabase::useDarkBackground(int c) const |
110 { | 158 { |