How to generate new QColors that are different from each other

I want to have a large array of QColors with which many classes will share and index.

In the past, I always had this list:

QColor colours[10] = {QColor("cyan"), QColor("magenta"), QColor("red"), QColor("darkRed"), QColor("darkCyan"), QColor("darkMagenta"), QColor("green"), QColor("darkGreen"), QColor("yellow"), QColor("blue")}; 

However, now I want a lot more than 10. How can I create a large list of different QColors?

+4
source share
3 answers

if you want your list to be dynamic, instead I would use some kind of QVector, for example, in the Color Manager:

 class ColorManager { public: ColorManager(size_t iDefaultSize) { m_colorList.reserve(iDefaultSize); } void addColor(const QColor& c) { m_colorList.push_back(c); } QColor& operator[](int iIndex) { return m_colorList.at(iIndex); } private: QVector m_colorList; };
class ColorManager { public: ColorManager(size_t iDefaultSize) { m_colorList.reserve(iDefaultSize); } void addColor(const QColor& c) { m_colorList.push_back(c); } QColor& operator[](int iIndex) { return m_colorList.at(iIndex); } private: QVector m_colorList; }; 

If your colors should be unique, consider using QSet, but you will lose the [] operator, since QSet is an ordered structure and you will have to implement find in ColorManager using QSet :: find (). It will also be slower. If it should be thread safe, you can end up protecting it with QMutex.

Also, I don't know why you need this, but you should take a look at:

QColorGroup and / or QPalette

+1
source

Here's a good article on randomly generating colors from sets so they look good together.

http://devmag.org.za/2012/07/29/how-to-choose-colours-procedurally-algorithms/

+1
source

You can use the QColor constructor, which accepts red, green, and blue parameters to create new colors like:

 QColor colours[10] = {QColor(255,0,0), QColor(0,255,0), QColor(0,0,255), QColor(0,0,0), QColor(255,255,255), QColor(0,128,64)}; 

You can use as many combinations of r, g, b as possible to come up with new colors. You can find the red, green, and blue options for different colors by looking at the Pantone color chart and create a nice set of colors for your application.

0
source

Source: https://habr.com/ru/post/1387144/


All Articles