-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathofxDatGuiColorPicker.h
More file actions
252 lines (218 loc) · 9.98 KB
/
ofxDatGuiColorPicker.h
File metadata and controls
252 lines (218 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright (C) 2015 Stephen Braitsch [http://braitsch.io]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include "ofxDatGuiTextInput.h"
class ofxDatGuiColorPicker : public ofxDatGuiTextInput {
public:
ofxDatGuiColorPicker(string label, ofColor color=ofColor::black) : ofxDatGuiTextInput(label, "XXXXXX")
{
mColor = color;
mShowPicker = false;
mType = ofxDatGuiType::COLOR_PICKER;
setTheme(ofxDatGuiComponent::theme.get());
// center the text input field //
mInput.setTextInputFieldType(ofxDatGuiInputType::COLORPICKER);
setTextFieldInputColor();
// setup the vbo that draws the main gradient //
gPoints.push_back(ofVec2f(0, 0));
gPoints.push_back(ofVec2f(0, 0));
gPoints.push_back(ofVec2f(0, 0));
gPoints.push_back(ofVec2f(0, 0));
gPoints.push_back(ofVec2f(0, 0));
gPoints.push_back(ofVec2f(0, 0));
// center point of the gradient is 1/2 way between mColor & black //
ofColor center = ofColor(mColor.r/2, mColor.g/2, mColor.b/2);
// draw main gradient as a six point triangle fan //
gColors.push_back(center); // center
gColors.push_back(ofColor::white); // top-left
gColors.push_back(mColor); // top-right
gColors.push_back(ofColor::black); // btm-right
gColors.push_back(ofColor::black); // btm-left
gColors.push_back(ofColor::white); // top-left
vbo.setColorData(&gColors[0], 6, GL_DYNAMIC_DRAW );
}
void setTheme(const ofxDatGuiTheme* theme)
{
ofxDatGuiTextInput::setTheme(theme);
mStyle.stripe.color = theme->stripe.colorPicker;
pickerRect = ofRectangle(0, 0, mInput.getWidth(), (mStyle.height + mStyle.padding) * 3);
rainbow.image = theme->icon.rainbow;
rainbow.rect = ofRectangle(0, 0, theme->layout.colorPicker.rainbowWidth, pickerRect.height - (mStyle.padding * 2));
gradientRect = ofRectangle(0, 0, pickerRect.width - rainbow.rect.width - (mStyle.padding * 3), rainbow.rect.height);
pickerBorder = theme->color.colorPicker.border;
setTextFieldInputColor();
}
void setColor(ofColor color)
{
mColor = color;
setTextFieldInputColor();
gColors[2] = mColor;
gColors[0] = ofColor(mColor.r/2, mColor.g/2, mColor.b/2);
vbo.setColorData(&gColors[0], 6, GL_DYNAMIC_DRAW );
}
void setColor(int hex)
{
mColor = ofColor::fromHex(hex);
setColor(mColor);
}
void setColor(int r, int g, int b, int a = 255)
{
mColor = ofColor(r, g, b, a);
setColor(mColor);
}
ofColor getColor()
{
return mColor;
}
void draw()
{
if (!mVisible) return;
ofPushStyle();
ofxDatGuiTextInput::draw();
if (mShowPicker) {
pickerRect.x = this->x + mLabel.width;
pickerRect.y = this->y + mStyle.padding + mInput.getHeight();
pickerRect.width = mInput.getWidth();
rainbow.rect.x = pickerRect.x + pickerRect.width - rainbow.rect.width - mStyle.padding;
rainbow.rect.y = pickerRect.y + mStyle.padding;
gradientRect.x = pickerRect.x + mStyle.padding;
gradientRect.y = pickerRect.y + mStyle.padding;
gradientRect.width = pickerRect.width - rainbow.rect.width - (mStyle.padding * 3);
gPoints[0] = ofVec2f(gradientRect.x+ gradientRect.width/2, gradientRect.y + gradientRect.height/2);
gPoints[1] = ofVec2f(gradientRect.x, gradientRect.y);
gPoints[2] = ofVec2f(gradientRect.x+ gradientRect.width, gradientRect.y);
gPoints[3] = ofVec2f(gradientRect.x+ gradientRect.width, gradientRect.y + gradientRect.height);
gPoints[4] = ofVec2f(gradientRect.x, gradientRect.y+gradientRect.height);
gPoints[5] = ofVec2f(gradientRect.x, gradientRect.y);
vbo.setVertexData(&gPoints[0], 6, GL_DYNAMIC_DRAW );
ofSetColor(pickerBorder);
ofDrawRectangle(pickerRect);
ofSetColor(ofColor::white);
rainbow.image->draw(rainbow.rect);
vbo.draw( GL_TRIANGLE_FAN, 0, 6 );
}
ofPopStyle();
}
void drawColorPicker()
{
if (mVisible && mShowPicker){
ofPushStyle();
ofSetColor(pickerBorder);
ofDrawRectangle(pickerRect);
ofSetColor(ofColor::white);
rainbow.image->draw(rainbow.rect);
vbo.draw( GL_TRIANGLE_FAN, 0, 6 );
ofPopStyle();
}
}
bool hitTest(ofPoint m)
{
if (mInput.hitTest(m)){
return true;
} else if (mShowPicker && pickerRect.inside(m)){
unsigned char p[3];
int y = (ofGetMouseY()-ofGetHeight())*-1;
glReadPixels(ofGetMouseX(), y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &p);
gColor.r = int(p[0]);
gColor.g = int(p[1]);
gColor.b = int(p[2]);
if (rainbow.rect.inside(m) && mMouseDown){
gColors[2] = gColor;
gColors[0] = ofColor(gColor.r/2, gColor.g/2, gColor.b/2);
vbo.setColorData(&gColors[0], 6, GL_DYNAMIC_DRAW );
} else if (gradientRect.inside(m) && mMouseDown){
mColor = gColor;
// dispatch event out to main application //
if (colorPickerEventCallback != nullptr) {
ofxDatGuiColorPickerEvent e(this, mColor);
colorPickerEventCallback(e);
} else{
ofxDatGuiLog::write(ofxDatGuiMsg::EVENT_HANDLER_NULL);
}
setTextFieldInputColor();
}
return true;
} else{
return false;
}
}
static ofxDatGuiColorPicker* getInstance() { return new ofxDatGuiColorPicker("X"); }
protected:
void onMouseEnter(ofPoint mouse)
{
mShowPicker = true;
ofxDatGuiComponent::onFocus();
ofxDatGuiComponent::onMouseEnter(mouse);
}
void onMouseLeave(ofPoint mouse)
{
mShowPicker = false;
ofxDatGuiTextInput::onMouseLeave(mouse);
if (!mInput.hasFocus()) ofxDatGuiComponent::onFocusLost();
}
void onMousePress(ofPoint mouse)
{
ofxDatGuiComponent::onMousePress(mouse);
if (mInput.hitTest(mouse)) mInput.onFocus();
}
void onInputChanged(ofxDatGuiInternalEvent e)
{
mColor = ofColor::fromHex(ofHexToInt(mInput.getText()));
// set the input field text & background colors //
mInput.setBackgroundColor(mColor);
mInput.setTextInactiveColor(mColor.getBrightness() < BRIGHTNESS_THRESHOLD ? ofColor::white : ofColor::black);
// update the gradient picker //
gColors[2] = mColor;
gColors[0] = ofColor(mColor.r/2, mColor.g/2, mColor.b/2);
vbo.setColorData(&gColors[0], 6, GL_DYNAMIC_DRAW );
// dispatch event out to main application //
if (colorPickerEventCallback != nullptr) {
ofxDatGuiColorPickerEvent evt(this, mColor);
colorPickerEventCallback(evt);
} else{
ofxDatGuiLog::write(ofxDatGuiMsg::EVENT_HANDLER_NULL);
}
}
inline void setTextFieldInputColor()
{
// convert color value to a six character hex string //
std::stringstream ss;
ss<< std::hex << mColor.getHex();
std::string res ( ss.str() );
while(res.size() < 6) res+="0";
mInput.setText(ofToUpper(res));
mInput.setBackgroundColor(mColor);
mInput.setTextInactiveColor(mColor.getBrightness() < BRIGHTNESS_THRESHOLD ? ofColor::white : ofColor::black);
}
private:
ofColor mColor;
ofColor gColor;
struct {
shared_ptr<ofImage> image;
ofRectangle rect;
} rainbow;
bool mShowPicker;
ofColor pickerBorder;
ofRectangle pickerRect;
ofRectangle gradientRect;
ofVbo vbo;
vector<ofVec2f> gPoints;
vector<ofFloatColor> gColors;
static const int BRIGHTNESS_THRESHOLD = 185;
};