1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: XGUI_ColorDialog.cpp
4 // Created: 27 Apr 2015
5 // Author: Natalia ERMOLAEVA
7 #include <XGUI_ColorDialog.h>
9 #include <ModelAPI_Tools.h>
11 #include <QtxColorButton.h>
14 #include <QButtonGroup>
15 #include <QGridLayout>
16 #include <QRadioButton>
17 #include <QDialogButtonBox>
19 XGUI_ColorDialog::XGUI_ColorDialog(QWidget* theParent)
22 setWindowTitle(tr("Color"));
23 QGridLayout* aLay = new QGridLayout(this);
25 QRadioButton* aRandomChoiceBtn = new QRadioButton(this);
26 QRadioButton* aColorChoiceBtn = new QRadioButton(this);
27 aColorChoiceBtn->setChecked(true);
28 myButtonGroup = new QButtonGroup(this);
29 myButtonGroup->setExclusive(true);
30 myButtonGroup->addButton(aColorChoiceBtn, 0);
31 myButtonGroup->addButton(aRandomChoiceBtn, 1);
33 aLay->addWidget(aColorChoiceBtn, 0, 0);
34 aLay->addWidget(aRandomChoiceBtn, 1, 0);
36 myColorButton = new QtxColorButton(this);
37 myColorButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
38 aLay->addWidget(myColorButton, 0, 1);
40 QLabel* aRandomLabel = new QLabel(tr("Random"), this);
41 aLay->addWidget(aRandomLabel, 1, 1);
43 QDialogButtonBox* aButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
44 Qt::Horizontal, this);
45 connect(aButtons, SIGNAL(accepted()), this, SLOT(accept()));
46 connect(aButtons, SIGNAL(rejected()), this, SLOT(reject()));
47 aLay->addWidget(aButtons, 2, 0, 1, 2);
50 bool XGUI_ColorDialog::isRandomColor() const
52 int anId = myButtonGroup->checkedId();
54 return myButtonGroup->checkedId() == 1;
57 void XGUI_ColorDialog::setColor(const std::vector<int>& theValue)
59 if (theValue.size() != 3)
62 myColorButton->setColor(QColor(theValue[0], theValue[1], theValue[2]));
65 std::vector<int> XGUI_ColorDialog::getColor() const
67 QColor aColorResult = myColorButton->color();
69 std::vector<int> aValues;
70 aValues.push_back(aColorResult.red());
71 aValues.push_back(aColorResult.green());
72 aValues.push_back(aColorResult.blue());
76 // contains global cash for integer index of the color -> RGB of this color
77 static std::map<int, std::vector<int> > myColorMap;
79 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
81 theRGB.push_back(theRed);
82 theRGB.push_back(theGreen);
83 theRGB.push_back(theBlue);
86 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
88 std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(),
89 aLast = theColorMap.end();
91 for (; anIt != aLast && !isFound; anIt++) {
92 std::vector<int> aValues = anIt->second;
93 isFound = aValues[0] == theValues[0] &&
94 aValues[1] == theValues[1] &&
95 aValues[2] == theValues[2];
100 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
102 std::vector<int> aRGB;
103 if (theH < 0 || theH > 360 ||
104 theS < 0 || theS > 100 ||
105 theV < 0 || theV > 100)
108 int aHi = (int)theH/60;
111 double aVmin = (100 - theS)*theV/100;
113 double anA = (theV - aVmin)* (theH % 60) / 60;
115 double aVinc = aVmin + anA;
116 double aVdec = theV - anA;
118 double aPercentToValue = 255./100;
119 int aV_int = (int)(aV*aPercentToValue);
120 int aVinc_int = (int)(aVinc*aPercentToValue);
121 int aVmin_int = (int)(aVmin*aPercentToValue);
122 int aVdec_int = (int)(aVdec*aPercentToValue);
125 case 0: appendValues(aRGB, aV_int, aVinc_int, aVmin_int); break;
126 case 1: appendValues(aRGB, aVdec_int, aV_int, aVmin_int); break;
127 case 2: appendValues(aRGB, aVmin_int, aV_int, aVinc_int); break;
128 case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
129 case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
130 case 5: appendValues(aRGB, aV_int, aVmin_int, aVdec_int); break;
139 if (!myColorMap.empty())
143 for (int s = 100; s > 0; s = s - 50)
145 for (int v = 100; v >= 40; v = v - 20)
147 for (int h = 0; h < 359 ; h = h + 60)
149 std::vector<int> aColor = HSVtoRGB(h, s, v);
150 if (containsValues(myColorMap, aColor))
152 myColorMap[i] = aColor;
159 void findRandomColor(std::vector<int>& theValues)
162 if (myColorMap.empty()) {
166 size_t aSize = myColorMap.size();
167 int anIndex = rand() % aSize;
168 if (myColorMap.find(anIndex) != myColorMap.end()) {
169 theValues = myColorMap.at(anIndex);
173 std::vector<int> XGUI_ColorDialog::getRandomColor() const
175 std::vector<int> aValues;
176 if (isRandomColor()) {
177 findRandomColor(aValues);