«color picker» by LFSaw

on 05 Sep'13 10:16 in guicolortoolpickercolourgraphics

A simple color picker. Posts picked color to the postwindow. Based on (non-functional) code found at http://swiki.hfbk-hamburg.de:8888/MusicTechnology/868 (scsolar 2007)

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
(
var res = 20, scrsize = 200, val, set, win, scr, currentColor = Color.gray, setColor;
win = Window.new("color picker",Rect(400, 400, 2*scrsize+20, scrsize),false).front;

//win.view.background_(Color.white);

scr = UserView.new(win,Rect(scrsize+20, 0, scrsize, scrsize));
scr.background_(Color.white);

setColor = {|x, y, val, postVal=false|
	//x = x/scrsize;
	//y = y/scrsize;

	currentColor = Color.hsv(min(0.999, x), min(0.999, 1-y), val, 1);
	postVal.if{
//		("Color.hsv("++(x)++", "++(1-y)++", "++val.value++", 1)").postln; 
		"Color.new255(%, %, %)".format(*(currentColor.asArray*255)).postln; 
	};

	scr.background_(
		Color.hsv(min(0.999, x), min(0.999, 1-y), val.value, 1)
	);
};




val = Slider(win, Rect(scrsize, 0, 20, scrsize)).value_(1).action_({|view|
	setColor.(m.x.postln, m.y, view.value);
	
	win.refresh
});

m = Slider2D(win, Rect(0, 0, scrsize, scrsize))
	.mouseDownAction_({|view| 
		setColor.(view.x, view.y, val.value);
		win.refresh
	})
	.mouseMoveAction_({|view|
		setColor.(view.x, view.y, val.value);
		win.refresh
	}).mouseUpAction_({|view| 
		setColor.(view.x, view.y, val.value, true);
		win.refresh
	});

win.front;
)
raw 1199 chars (focus & ctrl+a+c to copy)
reception
comments