| 35 | namespace { |
| 36 | |
| 37 | QIcon generateIcon(const WorkingSetIconParameters& params) |
| 38 | { |
| 39 | QImage pixmap(16, 16, QImage::Format_ARGB32); |
| 40 | // fill the background with a transparent color |
| 41 | pixmap.fill(QColor::fromRgba(qRgba(0, 0, 0, 0))); |
| 42 | const uint coloredCount = params.coloredCount; |
| 43 | // coordinates of the rectangles to draw, for 16x16 icons specifically |
| 44 | QList<QRect> rects{ |
| 45 | {1, 1, 5, 5}, |
| 46 | {1, 9, 5, 5}, |
| 47 | {9, 1, 5, 5}, |
| 48 | {9, 9, 5, 5}, |
| 49 | }; |
| 50 | if (params.swapDiagonal) { |
| 51 | rects.swapItemsAt(1, 2); |
| 52 | } |
| 53 | |
| 54 | QPainter painter(&pixmap); |
| 55 | // color for non-colored squares, paint them brighter if the working set is the active one |
| 56 | const int inact = 40; |
| 57 | QColor darkColor = QColor::fromRgb(inact, inact, inact); |
| 58 | // color for colored squares |
| 59 | // this code is not fragile, you can just tune the magic formulas at random and see what looks good. |
| 60 | // just make sure to keep it within the 0-360 / 0-255 / 0-255 space of the HSV model |
| 61 | QColor brightColor = QColor::fromHsv(params.hue, qMin<uint>(255, 215 + (params.setId*5) % 150), |
| 62 | 205 + (params.setId*11) % 50); |
| 63 | // Y'UV "Y" value, the approximate "lightness" of the color |
| 64 | // If it is above 0.6, then making the color darker a bit is okay, |
| 65 | // if it is below 0.35, then the color should be a bit brighter. |
| 66 | float brightY = 0.299 * brightColor.redF() + 0.587 * brightColor.greenF() + 0.114 * brightColor.blueF(); |
| 67 | if ( brightY > 0.6 ) { |
| 68 | if ( params.setId % 7 < 2 ) { |
| 69 | // 2/7 chance to make the color significantly darker |
| 70 | brightColor = brightColor.darker(120 + (params.setId*7) % 35); |
| 71 | } |
| 72 | else if ( params.setId % 5 == 0 ) { |
| 73 | // 1/5 chance to make it a bit darker |
| 74 | brightColor = brightColor.darker(110 + (params.setId*3) % 10); |
| 75 | } |
| 76 | } |
| 77 | if ( brightY < 0.35 ) { |
| 78 | // always make the color brighter to avoid very dark colors (like rgb(0, 0, 255)) |
| 79 | brightColor = brightColor.lighter(120 + (params.setId*13) % 55); |
| 80 | } |
| 81 | int at = 0; |
| 82 | for (const QRect& rect : std::as_const(rects)) { |
| 83 | QColor currentColor; |
| 84 | // pick the colored squares; you can get different patterns by re-ordering the "rects" list |
| 85 | if ( (at + params.setId*7) % 4 < coloredCount ) { |
| 86 | currentColor = brightColor; |
| 87 | } |
| 88 | else { |
| 89 | currentColor = darkColor; |
| 90 | } |
| 91 | // draw the filling of the square |
| 92 | painter.setPen(QColor(currentColor)); |
| 93 | painter.setBrush(QBrush(currentColor)); |
| 94 | painter.drawRect(rect); |
no test coverage detected