-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColorChooser.pde
139 lines (114 loc) · 4.17 KB
/
ColorChooser.pde
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
import java.awt.Color;
class ColorToggleView implements ControllerView<Toggle> {
public void display(PApplet p, Toggle t) {
p.pushMatrix();
p.fill(0);
if( t.getState() ) {
p.ellipse( t.getWidth() * 0.25 - t.getHeight() * 0.5, 0, t.getHeight(), t.getHeight());
} else {
p.rect( t.getWidth() * 0.75 - t.getHeight() * 0.5, 0, t.getHeight(), t.getHeight());
}
p.fill( t.getColor().getForeground() );
p.ellipse( t.getWidth() * 0.25 - t.getHeight() * 0.5 + 5, 5, t.getHeight()- 10, t.getHeight()-10);
p.fill( t.getColor().getBackground() );
p.rect( t.getWidth() * 0.75 - t.getHeight() * 0.5 + 5, 5, t.getHeight() - 10, t.getHeight()-10);
p.popMatrix();
}
}
/*
* Draws the hue, saturation and brightness sliders for the custom ColorChooserController
*/
class ColorChooserView implements ControllerView<ColorChooserController> {
public void display(PApplet p, ColorChooserController colorChooser) {
float barHeight = colorChooser.getHeight() / 4.0;
float h = colorChooser.getHue();
float s = colorChooser.getSaturation();
float b = colorChooser.getBrightness();
float h_pos = map( h, 0, 255, 0, colorChooser.getWidth() );
float s_pos = map(s, 0, 255, 0, colorChooser.getWidth() );
float b_pos = map( b, 0, 255, 0, colorChooser.getWidth() );
p.pushMatrix();
p.colorMode(HSB);
for(float i = 0; i < colorChooser.getWidth(); i++) {
p.stroke( (i / colorChooser.getWidth() ) * 255, 255, 255 );
p.line( i, 0, i, barHeight);
p.stroke( h, (i / colorChooser.getWidth() ) * 255, b );
p.line( i, barHeight * 1.5, i, barHeight * 2.5);
p.stroke( h, s, (i / colorChooser.getWidth() ) * 255 );
p.line( i, barHeight * 3, i, barHeight * 4);
}
p.colorMode(RGB);
p.stroke(0);
p.strokeWeight(2);
p.noFill();
float radius = barHeight / 4.0;
float offset = radius / 2.0;
float center = barHeight / 2;
//Hue point
p.line( h_pos, -5.0, h_pos, center - offset);
p.ellipse( h_pos - offset, center - offset, radius, radius );
p.line( h_pos, center + offset, h_pos, barHeight + 5.0);
//Saturation point
p.translate(0, barHeight * 1.5);
p.line( s_pos, -5.0, s_pos, center - offset);
p.ellipse( s_pos - offset, center - offset, radius, radius );
p.line( s_pos, center + offset, s_pos, barHeight + 5.0);
//Saturation point
p.translate(0, barHeight * 1.5);
p.line( b_pos, -5.0, b_pos, center - offset);
p.ellipse( b_pos - offset, center - offset, radius, radius );
p.line( b_pos, center + offset, b_pos, barHeight + 5.0);
p.popMatrix();
p.noStroke();
}
}
/*
* Creates a custom controller for selecting a color
*/
class ColorChooserController extends Controller<ColorChooserController> {
int index;
ColorChooserController(ControlP5 cp5, String theName) {
super(cp5, theName);
setBroadcast(true);
float[] c = {0.0, 255.0, 255.0};
setArrayValue( c );
setView( new ColorChooserView() );
}
void onClick() {
}
void onPress() {
Pointer p1 = getPointer();
float [] valueArray = getArrayValue();
if( p1.y() < getHeight() * 0.333333 ){
index = 0;
} else if ( p1.y() < getHeight() * 0.666666 ) {
index = 1;
} else {
index = 2;
}
valueArray[index] = constrain( map( p1.x(), 0, getWidth(), 0, 255), 0, 255);
}
void onDrag() {
float [] valueArray = getArrayValue();
Pointer p1 = getPointer();
valueArray[index] = constrain( map( p1.x(), 0, getWidth(), 0, 255), 0, 255);
}
ColorChooserController setColorValue( int col ) {
float[] valueArray = {hue(col), saturation(col), brightness(col)};
setArrayValue( valueArray );
return this;
}
int getColorValue() {
float [] valueArray = getArrayValue();
return Color.HSBtoRGB(valueArray[0] / 255.0, valueArray[1] / 255.0, valueArray[2] / 255.0);
}
float getHue() {
return getArrayValue()[0];
}
float getSaturation() {
return getArrayValue()[1];
}
float getBrightness() {
return getArrayValue()[2];
}
}