It is necessary to generate colors, as we know the color of RGBA in Qt is represented as unsigned int , We need a generator that meets the following requirements:

  • Generating numbers with a large spread from each other;
  • Naturally, without repetition.
  • Do you really need RGB with random transparency? - gil9red
  • @ gil9red is all fixable - OlegUP

1 answer 1

Control the variation and uniqueness will have to manually. I will give an example with uniqueness. You need to be careful with the cycle - all combinations will ever run out, therefore, you will need to either re-create / clear the unique :

 class UniqueRGB { public: UniqueRGB() { qsrand(QTime::currentTime().msec()); } QRgb get() { QRgb rgb; do { int r = rand() % 255; int g = rand() % 255; int b = rand() % 255; rgb = qRgba(r, g, b, 255); } while (unique.contains(rgb)); unique.insert(rgb); return rgb; } private: QSet<QRgb> unique; }; 

Using:

 UniqueRGB uniqueRGB; for (int i = 0; i < 1000; i++) { QRgb rgb = uniqueRGB.get(); qDebug() << rgb << QColor::fromRgb(rgb); } 
  • I would also do such a thing, but it is precisely to keep unique, and even control the spread of the cycles itself, this is not Zen, we need a mathematical function that ensures at least that, but I don’t know about this. - OlegUP
  • @ OlegUP, and how? Combinations of all variants of rgb are of course not so much - gil9red
  • I say, to know such a distribution, according to its function, one could create the necessary generator, but I myself didn’t ter Ter Ver taught. - OlegUP