// title: ColorPicker ( HSV to Color ), RAW Version // author: Dindoléon // description: // 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. // code: ( /* 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; )