##// END OF EJS Templates
Fix color generation for pie
Jani Honkonen -
r324:405c1f0ce037
parent child
Show More
@@ -1,7 +1,7
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4
4 #include <QTime>
5
5
6 //series
6 //series
7 #include "qbarset.h"
7 #include "qbarset.h"
@@ -50,6 +50,8 ChartTheme::ChartTheme(QChart::ChartTheme id)
50 m_seriesColor.append(QRgb(0xff707070));
50 m_seriesColor.append(QRgb(0xff707070));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
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 QColor c = m_seriesColor[i++];
190 QColor c = m_seriesColor[i++];
189 i = i % m_seriesColor.count();
191 i = i % m_seriesColor.count();
190
192
191 // -1 means achromatic color -> cannot manipulate lightness
193 // by default use the "raw" theme color
192 // TODO: find a better way to randomize lightness
194 if (!colors.contains(c)) {
193 if (c.toHsv().hue() == -1)
195 colors << c;
194 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
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
204 // find maximum value we can raise the lightness
197 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
205 int lMax = 255;
198 c = c.lighter(f);
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
213 // find maximum value we can make it darker
201 bool isUnique = true;
214 int dMax = 255;
202 foreach (QColor color, colors) {
215 if (dMax > c.red())
203 if (c == color)
216 dMax = c.red();
204 isUnique = false;
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
241 qDebug() << "generated a color for pie" << c;
208 //if (isUnique)
242 colors << c;
209 colors << c;
210 }
243 }
211
244
212 // finally update colors
245 // finally update colors
213 foreach (QPieSlice* s, series->slices()) {
246 foreach (QPieSlice* s, series->slices()) {
214 s->setPen(QPen(Qt::black)); // TODO: get from theme
247 QColor c = colors.takeFirst();
215 s->setBrush(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