Skip to content

Commit 16ad451

Browse files
committed
DwColorPicker - ControlP5 integration
1 parent 2b14aae commit 16ad451

File tree

3 files changed

+240
-11
lines changed

3 files changed

+240
-11
lines changed

examples/Miscellaneous/ColorPicker/ColorPicker.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,33 @@ public class ColorPicker extends PApplet {
3030
public DwColorPicker cp1;
3131
public DwColorPicker cp2;
3232
public DwColorPicker cp3;
33+
public DwColorPicker cp4;
34+
public DwColorPicker cp5;
35+
3336
PFont font;
3437

3538
public void settings(){
36-
size(1200, 900, P2D);
39+
size(1000, 700, P2D);
3740
}
3841

3942
public void setup(){
4043
surface.setLocation(210, 0);
4144

4245
cp1 = new DwColorPicker(this, 10, 10, width-20, 150);
43-
cp2 = new DwColorPicker(this, 10, 220, 180, 600, 51);
44-
cp3 = new DwColorPicker(this, width/2, height/2+10, 180, 80, 20);
45-
46-
cp3.createPallette(8);
46+
cp2 = new DwColorPicker(this, 10, 220, 180, 400);
47+
cp3 = new DwColorPicker(this, 240, 240, 600, 80, 20);
48+
cp4 = new DwColorPicker(this, 240, 400, 240, 240, 20);
49+
cp5 = new DwColorPicker(this, 540, 500, 400, 20, 20);
4750

4851
cp1.createPallette(361, 5);
52+
cp2.createPallette(13, 200);
53+
cp3.createPallette(8);
54+
cp4.createPallette(361,240);
4955

50-
cp1.selectColorByGrid(cp1.getNumColorsX()/2, cp1.getNumColorsY()/2);
56+
cp1.selectColorByGrid(cp1.getNX()/2, cp1.getNY()/2);
5157
cp2.selectColorByRGB(255, 255, 125);
5258
cp3.selectColorByNormalizedCoords(0.5f, 0.5f);
59+
cp4.selectColorByRGB(128,128,128);
5360

5461
font = createFont("../data/SourceCodePro-Regular.ttf", 12);
5562
textFont(font);
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
/**
2+
*
3+
* PixelFlow | Copyright (C) 2016 Thomas Diewald - http://thomasdiewald.com
4+
*
5+
* A Processing/Java library for high performance GPU-Computing (GLSL).
6+
* MIT License: https://opensource.org/licenses/MIT
7+
*
8+
*/
9+
10+
11+
package Miscellaneous.ColorPicker_ControlP5;
12+
13+
14+
15+
import com.thomasdiewald.pixelflow.java.utils.DwColorPicker;
16+
17+
import controlP5.Accordion;
18+
import controlP5.ControlP5;
19+
import controlP5.Controller;
20+
import controlP5.ControllerView;
21+
import controlP5.Group;
22+
import controlP5.Pointer;
23+
24+
import processing.core.*;
25+
26+
27+
28+
public class ColorPicker_ControlP5 extends PApplet {
29+
30+
//
31+
// DwColorPicker as a ControlP5 controller
32+
//
33+
34+
PFont font;
35+
36+
int[] rgb0 = {255,125,0};
37+
int[] rgb1 = {0,125,255};
38+
39+
int circle0_radius = 150;
40+
int circle1_radius = 200;
41+
42+
public void settings(){
43+
// size(600, 600, JAVA2D);
44+
size(600, 600, P2D);
45+
smooth(8);
46+
}
47+
48+
public void setup(){
49+
frameRate(1000);
50+
51+
createGUI();
52+
}
53+
54+
public void draw(){
55+
background(64);
56+
57+
strokeWeight(10);
58+
59+
60+
pushMatrix();
61+
translate(width/2, height/2);
62+
stroke(0);
63+
fill(rgb0[0], rgb0[1], rgb0[2]);
64+
ellipse(0, -100, circle0_radius, circle0_radius);
65+
66+
stroke(255);
67+
fill(rgb1[0], rgb1[1], rgb1[2]);
68+
ellipse(0, +100, circle1_radius, circle1_radius);
69+
popMatrix();
70+
}
71+
72+
73+
int gui_w = 160;
74+
int hui_h = 350;
75+
ControlP5 cp5;
76+
77+
public void createGUI(){
78+
79+
cp5 = new ControlP5(this);
80+
81+
int col_group = color(16,220);
82+
83+
// GROUP
84+
Group group = cp5.addGroup("ColorPicker");
85+
group.setHeight(20).setSize(gui_w, hui_h).setBackgroundColor(col_group).setColorBackground(col_group);
86+
group.getCaptionLabel().align(CENTER, CENTER);
87+
88+
int sx = 100;
89+
int sy = 14;
90+
int px = 10;
91+
int py = 25;
92+
int dy = 2;
93+
94+
cp5.addSlider("radius 0").setGroup(group).setSize(sx, sy).setPosition(px, py)
95+
.setRange(50, 500).setValue(circle0_radius).plugTo(this, "circle0_radius");
96+
py += sy + dy;
97+
98+
cp5.addSlider("radius 1").setGroup(group).setSize(sx, sy).setPosition(px, py)
99+
.setRange(50, 500).setValue(circle1_radius).plugTo(this, "circle1_radius");
100+
py += sy + 30;
101+
102+
sx = gui_w - 20;
103+
sy = 60;
104+
ColorPicker cp1 = new ColorPicker(cp5, "colorpicker1", sx, sy, 100, rgb0).setGroup(group).setPosition(px, py);
105+
py += sy + 30;
106+
107+
sy = sx;
108+
ColorPicker cp2 = new ColorPicker(cp5, "colorpicker2", sx, sy, 5, rgb1).setGroup(group).setPosition(px, py);
109+
py += sy + dy;
110+
111+
112+
cp2.createPallette(20);
113+
114+
// ACCORDION
115+
cp5.addAccordion("acc").setPosition(20, 20).setSize(gui_w, hui_h)
116+
.setCollapseMode(Accordion.MULTI)
117+
.addItem(group)
118+
.open(0, 1);
119+
120+
}
121+
122+
123+
124+
125+
/**
126+
* Creating a new cp5-controller for PixelFlows colorpicker.
127+
*/
128+
static class ColorPicker extends Controller<ColorPicker> {
129+
ControlP5 cp5;
130+
DwColorPicker colorpicker;
131+
Pointer ptr = getPointer();
132+
int[] rgb;
133+
134+
int hud_sy = 16;
135+
136+
ColorPicker(ControlP5 cp5, String theName, int dim_x, int dim_y, int ny, int[] rgb) {
137+
super(cp5, theName);
138+
139+
setSize(dim_x, dim_y);
140+
this.cp5 = cp5;
141+
this.rgb = rgb;
142+
this.colorpicker = new DwColorPicker(cp5.papplet, 0, 0, dim_x, dim_y-hud_sy);
143+
this.colorpicker.setAutoDraw(false);
144+
this.colorpicker.setAutoMouse(false);
145+
createPallette(ny);
146+
147+
setView(new ControllerView<ColorPicker>() {
148+
public void display(PGraphics pg, ColorPicker cp) {
149+
150+
int dim_x = getWidth();
151+
int dim_y = getHeight();
152+
153+
int cp_col = colorpicker.getSelectedColor();
154+
String cp_rgb = colorpicker.getSelectedRGBasString();
155+
// String cp_hsb = colorpicker.getSelectedHSBasString();
156+
157+
int sy = hud_sy;
158+
int px = 0;
159+
int py = colorpicker.h()+1;
160+
161+
pg.noStroke();
162+
pg.fill(200, 50);
163+
pg.rect(px-1, py, dim_x+2, sy+1);
164+
pg.fill(cp_col);
165+
pg.rect(px, py, sy, sy);
166+
167+
pg.fill(255);
168+
pg.text(cp_rgb, px + sy * 2, py+8);
169+
// pg.text(cp_hsb, px + sy * 2, py+8);
170+
171+
colorpicker.display();
172+
}
173+
});
174+
}
175+
176+
public ColorPicker createPallette(int shadesY){
177+
colorpicker.createPallette(shadesY);
178+
colorpicker.selectColorByRGB(rgb[0], rgb[1], rgb[2]);
179+
return this;
180+
}
181+
182+
public ColorPicker createPallette(int shadesX, int shadesY){
183+
colorpicker.createPallette(shadesX, shadesY);
184+
colorpicker.selectColorByRGB(rgb[0], rgb[1], rgb[2]);
185+
return this;
186+
}
187+
188+
189+
boolean STATE_SELECT = false;
190+
public void selectColor(){
191+
if(STATE_SELECT){
192+
colorpicker.selectColorByCoords(ptr.x(), ptr.y());
193+
int[] selected = colorpicker.getSelectedRGBColor();
194+
rgb[0] = selected[0];
195+
rgb[1] = selected[1];
196+
rgb[2] = selected[2];
197+
}
198+
}
199+
200+
protected void onPress() {
201+
STATE_SELECT = colorpicker.inside(ptr.x(), ptr.y());
202+
selectColor();
203+
}
204+
protected void onDrag() {
205+
selectColor();
206+
}
207+
protected void onRelease( ) {
208+
selectColor();
209+
STATE_SELECT = false;
210+
}
211+
212+
}
213+
214+
215+
216+
217+
218+
public static void main(String args[]) {
219+
PApplet.main(new String[] { ColorPicker_ControlP5.class.getName() });
220+
}
221+
222+
223+
}

src/com/thomasdiewald/pixelflow/java/utils/DwColorPicker.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import processing.core.PGraphics;
1919
import processing.core.PImage;
2020
import processing.event.MouseEvent;
21-
import processing.opengl.PGraphics2D;
2221

2322
/**
2423
* @author Thomas Diewald
@@ -34,7 +33,7 @@ public class DwColorPicker {
3433
public static DwColorPicker LAST_USED;
3534

3635
protected PApplet papplet;
37-
protected PGraphics2D canvas;
36+
protected PGraphics canvas;
3837
protected PImage canvas_img;
3938

4039
protected int cp_x, cp_y;
@@ -160,7 +159,7 @@ public void createPallette(int shades_x, int shades_y){
160159
if(canvas == null){
161160
papplet.g.removeCache(canvas);
162161
}
163-
canvas = (PGraphics2D) papplet.createGraphics(cp_w, cp_h, PConstants.P2D);
162+
canvas = papplet.createGraphics(cp_w, cp_h);
164163
canvas.smooth(0);
165164
}
166165
canvas.beginDraw();
@@ -406,8 +405,8 @@ private int assureIndex(int idx){
406405
public int w(){ return cp_w; }
407406
public int h(){ return cp_h; }
408407

409-
public int getNumColorsX(){ return num_x; }
410-
public int getNumColorsY(){ return num_y; }
408+
public int getNX(){ return num_x; }
409+
public int getNY(){ return num_y; }
411410

412411
public String getSelectedRGBasString(){
413412
int[] rgb = getSelectedRGBColor();

0 commit comments

Comments
 (0)