##// END OF EJS Templates
Fix color generation for pie
Jani Honkonen -
r324:405c1f0ce037
parent child
Show More
@@ -1,7 +1,7
1 1 #include "charttheme_p.h"
2 2 #include "qchart.h"
3 3 #include "qchartaxis.h"
4
4 #include <QTime>
5 5
6 6 //series
7 7 #include "qbarset.h"
@@ -50,6 +50,8 ChartTheme::ChartTheme(QChart::ChartTheme id)
50 50 m_seriesColor.append(QRgb(0xff707070));
51 51 m_gradientStartColor = QColor(QRgb(0xffffffff));
52 52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
53
54 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
53 55 }
54 56
55 57
@@ -188,31 +190,63 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
188 190 QColor c = m_seriesColor[i++];
189 191 i = i % m_seriesColor.count();
190 192
191 // -1 means achromatic color -> cannot manipulate lightness
192 // TODO: find a better way to randomize lightness
193 if (c.toHsv().hue() == -1)
194 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
193 // by default use the "raw" theme color
194 if (!colors.contains(c)) {
195 colors << c;
196 continue;
197 }
198 // ...ok we need to generate something that looks like the same color
199 // but different lightness
200
201 int tryCount = 0;
202 while (tryCount++ < 100) {
195 203
196 // randomize lightness
197 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
198 c = c.lighter(f);
204 // find maximum value we can raise the lightness
205 int lMax = 255;
206 if (lMax > 255 - c.red())
207 lMax = 255 - c.red();
208 if (lMax > 255 - c.green())
209 lMax = 255 - c.green();
210 if (lMax > 255 - c.blue())
211 lMax = 255 - c.blue();
199 212
200 // find duplicates
201 bool isUnique = true;
202 foreach (QColor color, colors) {
203 if (c == color)
204 isUnique = false;
213 // find maximum value we can make it darker
214 int dMax = 255;
215 if (dMax > c.red())
216 dMax = c.red();
217 if (dMax > c.green())
218 dMax = c.green();
219 if (dMax > c.blue())
220 dMax = c.blue();
221
222 int max = dMax + lMax;
223 if (max == 0) {
224 // no room to make color lighter or darker...
225 qDebug() << "cannot generate a color for pie!";
226 break;
227 }
228
229 // generate random color
230 int r = c.red() - dMax;
231 int g = c.green() - dMax;
232 int b = c.blue() - dMax;
233 int d = qrand() % max;
234 c.setRgb(r+d, g+d, b+d);
235
236 // found a unique color?
237 if (!colors.contains(c))
238 break;
205 239 }
206 240
207 // add to array if unique
208 //if (isUnique)
209 colors << c;
241 qDebug() << "generated a color for pie" << c;
242 colors << c;
210 243 }
211 244
212 245 // finally update colors
213 246 foreach (QPieSlice* s, series->slices()) {
214 s->setPen(QPen(Qt::black)); // TODO: get from theme
215 s->setBrush(colors.takeFirst());
247 QColor c = colors.takeFirst();
248 s->setPen(c);
249 s->setBrush(c);
216 250 }
217 251 }
218 252
General Comments 0
You need to be logged in to leave comments. Login now