«ColorPicker ( HSV to Color ), RAW Version» by Dindoléon

on 12 Aug'21 11:00 in guitoolhsv

Function that returns a color picker view, and allows to connect it to a function outside of the color picker itself. Contains an HSV to Color() algorithm. No embellishment.

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
(

/*

Demonstration of a HSV Color Picker, raw version

Steps :

1) Copy/Paste get_color_picker() alongside a variable which will store the UserView.
( color_picker = get_color_picker.value(); <- color_picker is an UserView )

2) Create a function that the color picker will call when used. As is, only takes a Color() as arg.
( modulate_color_via_picker = { | color | ... }; )

3) Bind the two of them, using : userview_variable.bindFunction( function );
( color_picker.bindFunction( modulate_color_via_picker ) )

Pink ftw, D.

*/

var win = Window.new( bounds:Rect(300,300,300,600) );

var outer_view = UserView();
var modulated_color = Color.red;
var modulate_color_via_picker;

var color_picker;
var get_color_picker;

get_color_picker = {

	var color = Color( 1, 0, 0 );
	var hsv = [ 0, 1, 1 ];

	var color_picker_view = UserView();
	var h_slider = UserView();
	var sv_slider = UserView();
	var helper = UserView();

	var binded_function = nil;

	var hsv_to_color = { | hsv |

		var color = Color( 0, 0, 0 );

		var h = hsv[0];
		var s = hsv[1];
		var v = hsv[2];

		var c = s * v;
		var x = c * ( 1 - ( ( h/60.0 )%2.0 -1 ).abs );
		var m = v - c;

		if( ( h >= 0 ) && ( h < 60 ), {
			color.red = c;
			color.green = x;
			color.blue = 0;
		} );

		if( ( h >= 60 ) && ( h < 120 ), {
			color.red = x;
			color.green = c;
			color.blue = 0;
		} );

		if( ( h >= 120 ) && ( h < 180 ), {
			color.red = 0;
			color.green = c;
			color.blue = x;
		} );

		if( ( h >= 180 ) && ( h < 240 ), {
			color.red = 0;
			color.green = x;
			color.blue = c;
		} );

		if( ( h >= 240 ) && ( h < 300 ), {
			color.red = x;
			color.green = 0;
			color.blue = c;
		} );

		if( ( h >= 300 ) && ( h < 360 ), {
			color.red = c;
			color.green = 0;
			color.blue = x;
		} );

		color.red = color.red + m;
		color.green = color.green + m;
		color.blue = color.blue + m;

		color;
	};

	h_slider.drawFunc_( { | view |
		Pen.width = 1;
		view.bounds.height.do( { | index |
			Pen.strokeColor_(
				hsv_to_color.value(
					[
						index.linlin(
							0,
							view.bounds.height,
							0,
							360
						),
						1,
						1
					];
				);
			);
			Pen.moveTo( Point( 0, index ) );
			Pen.lineTo( Point( view.bounds.width, index ) );
			Pen.stroke;
		} );
	} );

	h_slider.mouseDownAction_( { | view, x, y |
		y = y.linlin( 0, view.bounds.height, 0, 360 );
		hsv[0] = y;
		color = hsv_to_color.value( hsv );
		if( binded_function != nil, { binded_function.value( color ) } );
		helper.refresh;
		sv_slider.refresh;
	} );
	h_slider.mouseMoveAction_( h_slider.mouseDownAction );

	sv_slider.drawFunc_( { | view |

		view.bounds.width.do( { | index_x |
			Pen.addRect(
				Rect(
					index_x,
					0,
					1,
					view.bounds.height
				)
			);
			Pen.fillAxialGradient(
				Point(0,0),
				Point(0,view.bounds.height),
				Color.black,
				hsv_to_color.value(
					[
						hsv[0],
						index_x.linlin( 0, view.bounds.width, 0, 1 ),
						1
					]
				)
			);

		} );
	} );

	sv_slider.mouseDownAction_( { | view, x, y |
		x = x.linlin( 0, view.bounds.width, 0, 1 );
		y = y.linlin( 0, view.bounds.height, 0, 1 );
		hsv[1] = x;
		hsv[2] = y;
		color = hsv_to_color.value( hsv );
		if( binded_function != nil, { binded_function.value( color ) } );
		helper.refresh;
	} );
	sv_slider.mouseMoveAction_( sv_slider.mouseDownAction );

	helper.drawFunc_( { | view |
		Pen.fillColor_( color );
		Pen.fillRect(
			Rect(
				0,
				0,
				view.bounds.width,
				view.bounds.height
			)
		)
	} );

	color_picker_view.background_( Color( 0.5, 0.5, 0.5 ) );
	color_picker_view.layout_(
		VLayout(
			[ helper, stretch:1 ],
			[ HLayout(
				[ h_slider, stretch: 1 ],
				[ sv_slider, stretch: 2 ]
			), stretch:2 ]
		);
	);

	color_picker_view.addUniqueMethod( \bindFunction, { | object, function | binded_function = function } );

	color_picker_view
};


color_picker = get_color_picker.value();
outer_view.drawFunc = { | view |
	Pen.stringCenteredIn(
		"Hey !,\nI'm located OUTSIDE of\nthe color picker !\n:3",
		Rect(
			0,
			0,
			view.bounds.width,
			view.bounds.height
		),
		color: modulated_color
	)
};

//That's probably what you are looking for :
modulate_color_via_picker = { | color |
	modulated_color = color;
	outer_view.refresh;
};
color_picker.bindFunction( modulate_color_via_picker );

win.layout_(
	VLayout(
		[ outer_view, stretch:1 ],
		[ color_picker, stretch:1 ]
	)
);

win.front;

)
raw 4610 chars (focus & ctrl+a+c to copy)
reception
comments