-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolourpicker.pde
90 lines (78 loc) · 2.03 KB
/
colourpicker.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
ColorPicker cp;
color col = #ffffff;
public class ColorPicker
{
int x, y, w, h, c;
PImage cpImage;
public ColorPicker ( int x, int y, int w, int h, int c )
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.c = c;
cpImage = new PImage( w, h );
init();
}
private void init ()
{
// draw color.
int cw = w - 60;
for( int i=0; i<cw; i++ )
{
float nColorPercent = i / (float)cw;
float rad = (-360 * nColorPercent) * (PI / 180);
int nR = (int)(cos(rad) * 127 + 128) << 16;
int nG = (int)(cos(rad + 2 * PI / 3) * 127 + 128) << 8;
int nB = (int)(Math.cos(rad + 4 * PI / 3) * 127 + 128);
int nColor = nR | nG | nB;
setGradient( i, 0, h/2, 0xFFFFFF, nColor );
setGradient( i, (h/2), h/2, nColor, 0x000000 );
}
// draw black/white.
drawRect( cw, 0, 30, h/2, 0xFFFFFF );
drawRect( cw, h/2, 30, h/2, 0 );
// draw grey scale.
for( int j=0; j<h; j++ )
{
int g = 255 - (int)(j/(float)(h-1) * 255 );
drawRect( w-30, j, 30, 1, color( g, g, g ) );
}
}
private void setGradient(int x, int y, float h, int c1, int c2 )
{
float deltaR = red(c2) - red(c1);
float deltaG = green(c2) - green(c1);
float deltaB = blue(c2) - blue(c1);
for (int j = y; j<(y+h); j++)
{
int c = color( red(c1)+(j-y)*(deltaR/h), green(c1)+(j-y)*(deltaG/h), blue(c1)+(j-y)*(deltaB/h) );
cpImage.set( x, j, c );
}
}
private void drawRect( int rx, int ry, int rw, int rh, int rc )
{
for(int i=rx; i<rx+rw; i++)
{
for(int j=ry; j<ry+rh; j++)
{
cpImage.set( i, j, rc );
}
}
}
public void render ()
{
image( cpImage, x, y );
if( mousePressed &&
mouseX >= x &&
mouseX < x + w &&
mouseY >= y &&
mouseY < y + h )
{
c = get( mouseX, mouseY );
}
fill( c );
col = color(c);
rect( x, y+h+10, 20, 20 );
}
}