1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <XGUI_ColorDialog.h>
23 #include <ModelAPI_Tools.h>
25 #include <QtxColorButton.h>
28 #include <QButtonGroup>
29 #include <QGridLayout>
30 #include <QRadioButton>
31 #include <QDialogButtonBox>
33 XGUI_ColorDialog::XGUI_ColorDialog(QWidget* theParent)
36 setWindowTitle(tr("Color"));
37 QGridLayout* aLay = new QGridLayout(this);
39 QRadioButton* aRandomChoiceBtn = new QRadioButton(this);
40 QRadioButton* aColorChoiceBtn = new QRadioButton(this);
41 aColorChoiceBtn->setChecked(true);
42 myButtonGroup = new QButtonGroup(this);
43 myButtonGroup->setExclusive(true);
44 myButtonGroup->addButton(aColorChoiceBtn, 0);
45 myButtonGroup->addButton(aRandomChoiceBtn, 1);
47 aLay->addWidget(aColorChoiceBtn, 0, 0);
48 aLay->addWidget(aRandomChoiceBtn, 1, 0);
50 myColorButton = new QtxColorButton(this);
51 myColorButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
52 aLay->addWidget(myColorButton, 0, 1);
54 QLabel* aRandomLabel = new QLabel(tr("Random"), this);
55 aLay->addWidget(aRandomLabel, 1, 1);
57 QDialogButtonBox* aButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
58 Qt::Horizontal, this);
59 connect(aButtons, SIGNAL(accepted()), this, SLOT(accept()));
60 connect(aButtons, SIGNAL(rejected()), this, SLOT(reject()));
61 aLay->addWidget(aButtons, 2, 0, 1, 2);
64 bool XGUI_ColorDialog::isRandomColor() const
66 int anId = myButtonGroup->checkedId();
68 return myButtonGroup->checkedId() == 1;
71 void XGUI_ColorDialog::setColor(const std::vector<int>& theValue)
73 if (theValue.size() != 3)
76 myColorButton->setColor(QColor(theValue[0], theValue[1], theValue[2]));
79 std::vector<int> XGUI_ColorDialog::getColor() const
81 QColor aColorResult = myColorButton->color();
83 std::vector<int> aValues;
84 aValues.push_back(aColorResult.red());
85 aValues.push_back(aColorResult.green());
86 aValues.push_back(aColorResult.blue());
90 // contains global cash for integer index of the color -> RGB of this color
91 static std::map<int, std::vector<int> > myColorMap;
93 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
95 theRGB.push_back(theRed);
96 theRGB.push_back(theGreen);
97 theRGB.push_back(theBlue);
100 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
102 std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(),
103 aLast = theColorMap.end();
104 bool isFound = false;
105 for (; anIt != aLast && !isFound; anIt++) {
106 std::vector<int> aValues = anIt->second;
107 isFound = aValues[0] == theValues[0] &&
108 aValues[1] == theValues[1] &&
109 aValues[2] == theValues[2];
114 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
116 std::vector<int> aRGB;
117 if (theH < 0 || theH > 360 ||
118 theS < 0 || theS > 100 ||
119 theV < 0 || theV > 100)
122 int aHi = (int)theH/60;
125 double aVmin = (100 - theS)*theV/100;
127 double anA = (theV - aVmin)* (theH % 60) / 60;
129 double aVinc = aVmin + anA;
130 double aVdec = theV - anA;
132 double aPercentToValue = 255./100;
133 int aV_int = (int)(aV*aPercentToValue);
134 int aVinc_int = (int)(aVinc*aPercentToValue);
135 int aVmin_int = (int)(aVmin*aPercentToValue);
136 int aVdec_int = (int)(aVdec*aPercentToValue);
139 case 0: appendValues(aRGB, aV_int, aVinc_int, aVmin_int); break;
140 case 1: appendValues(aRGB, aVdec_int, aV_int, aVmin_int); break;
141 case 2: appendValues(aRGB, aVmin_int, aV_int, aVinc_int); break;
142 case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
143 case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
144 case 5: appendValues(aRGB, aV_int, aVmin_int, aVdec_int); break;
153 if (!myColorMap.empty())
157 for (int s = 100; s > 0; s = s - 50)
159 for (int v = 100; v >= 40; v = v - 20)
161 for (int h = 0; h < 359 ; h = h + 60)
163 std::vector<int> aColor = HSVtoRGB(h, s, v);
164 if (containsValues(myColorMap, aColor))
166 myColorMap[i] = aColor;
173 void findRandomColor(std::vector<int>& theValues)
176 if (myColorMap.empty()) {
180 size_t aSize = myColorMap.size();
181 int anIndex = rand() % aSize;
182 if (myColorMap.find(anIndex) != myColorMap.end()) {
183 theValues = myColorMap.at(anIndex);
187 std::vector<int> XGUI_ColorDialog::getRandomColor() const
189 std::vector<int> aValues;
190 if (isRandomColor()) {
191 findRandomColor(aValues);