«Color Picker, pretty version» by Dindoléon

on 14 Aug'21 19:16 in guitoolhsv

See https://sccode.org/1-5fa . Hue slider now updated when changing saturation. Strange bug causing the sliders colors to vary when window is resized, please comment if you figure out how to fix it...

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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
(

var win = Window( "Color Picker", Rect( 0, 300, 600, 300 ) );

var picker1;
var picker2;
var picker3;

~get_color_picker = {

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

	var color_picker_view = UserView(); // Main View

	var h_slider = UserView();
	var h_slider_position = 0;

	var sv_slider = UserView();

	var helper = UserView();

	var binded_function = nil;

	var margin; // Slider half dead zone

	// HSV to Color function
	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;
	};

	// Resizing adjustments
	color_picker_view.onResize = { | view |
		margin = ( view.bounds.width * 0.05 );
		h_slider.refresh;
		sv_slider.refresh;
	};

	// Hue Slider setup
	h_slider.background_( Color.black );
	h_slider.drawFunc_( { | view |
		Pen.width = 1;
		( view.bounds.height - ( margin * 2 ) ).do( { | index |
			Pen.strokeColor_(
				hsv_to_color.value(
					[
						index.linlin(
							0,
							( view.bounds.height - ( margin * 2 ) ),
							0,
							360
						),
						hsv[1],
						hsv[2]
					];
				);
			);

			Pen.moveTo( Point( margin, index + margin ) );
			Pen.lineTo( Point( view.bounds.width - margin, index + margin ) );
			Pen.stroke;
		} );

		Pen.addRect(
			Rect(
				margin / 2,
				margin / 2 + ( view.bounds.height - ( margin * 2 ) * h_slider_position ) + margin,
				view.bounds.width - margin,
				margin
			)
		);
		Pen.fillAxialGradient(
			Point( 0, margin / 2 + ( view.bounds.height - ( margin * 2 ) * h_slider_position ) + margin ),
			Point( 0, margin / 2 + ( view.bounds.height - ( margin * 2 ) * h_slider_position ) + ( margin * 2 ) ),
			Color( 0, 0, 0, 1 ),
			Color( 0, 0, 0, 0 )
		);

		Pen.addRect(
			Rect(
				margin / 2,
				margin / 2 + ( view.bounds.height - ( margin * 2 ) * h_slider_position ) - margin,
				view.bounds.width - margin,
				margin
			)
		);
		Pen.fillAxialGradient(
			Point( 0, margin / 2 + ( view.bounds.height - ( margin * 2 ) * h_slider_position ) - margin ),
			Point( 0, margin / 2 + ( view.bounds.height - ( margin * 2 ) * h_slider_position ) ),
			Color( 0, 0, 0, 0 ),
			Color( 0, 0, 0, 1 )
		);

		Pen.fillColor_( color );
		Pen.fillRect(
			Rect(
				margin / 2,
				margin / 2 + ( view.bounds.height - ( margin * 2 ) * h_slider_position ),
				view.bounds.width - margin,
				margin
			)
		)
	} );

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

	// Saturation Slider setup
	sv_slider.background_( Color.black );
	sv_slider.drawFunc_( { | view |

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

		Pen.addWedge(
			Point(
				view.bounds.width - ( margin * 2 ) * hsv[1] + margin,
				view.bounds.height - ( margin * 2 ) * hsv[2] + margin,
			),
			margin * 2,
			0,
			360
		);
		Pen.fillRadialGradient(
			Point(
				view.bounds.width - ( margin * 2 ) * hsv[1] + margin,
				view.bounds.height - ( margin * 2 ) * hsv[2] + margin,
			),
			Point(
				view.bounds.width - ( margin * 2 ) * hsv[1] + margin,
				view.bounds.height - ( margin * 2 ) * hsv[2] + margin,
			),
			margin,
			margin * 1.5,
			Color( 0, 0, 0, 1 ),
			Color( 0, 0, 0, 0 )
		);

		Pen.fillColor_( color );
		Pen.addWedge(
			Point(
				view.bounds.width - ( margin * 2 ) * hsv[1] + margin,
				view.bounds.height - ( margin * 2 ) * hsv[2] + margin,
			),
			margin,
			0,
			360
		);
		Pen.fill;
	} );

	sv_slider.mouseDownAction_( { | view, x, y |

		case
		{ x < margin } { hsv[1] = 0 }
		{ x > ( view.bounds.width + margin ) } { hsv[1] = 1 }
		{ ( ( x >= margin ) && ( x <= ( view.bounds.width - margin ) ) ) } {
			hsv[1] = x.linlin( margin, view.bounds.width - margin, 0, 1 );
		};

		case
		{ y < margin } { hsv[2] = 0 }
		{ y > ( view.bounds.height + margin ) } { hsv[2] = 1 }
		{ ( ( y >= margin ) && ( y <= ( view.bounds.height - margin ) ) ) } {
			hsv[2] = y.linlin( margin, view.bounds.height - margin, 0, 1 );
		};

		color = hsv_to_color.value( hsv );
		if( binded_function != nil, { binded_function.value( color ) } );
		helper.refresh;
		sv_slider.refresh;
		h_slider.refresh;
	} );
	sv_slider.mouseMoveAction_( sv_slider.mouseDownAction );

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

	// Background Color
	color_picker_view.background_( Color.black );
	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
};

picker1 = ~get_color_picker.value();
picker2 = ~get_color_picker.value();
picker3 = ~get_color_picker.value();

win.layout_( HLayout( picker1, picker2, picker3 ) );
win.background_( Color( 0.2, 0.2, 0.2 ) );
win.front

)
raw 6777 chars (focus & ctrl+a+c to copy)
reception
comments
grirgz user 30 Dec'21 19:20

very nice ! do you mind if I integrate it in my quark ? (https://github.com/grirgz/param) I need a color picker

Dindoléon user 03 Jan'22 18:32

No, no problem !