{
   "labels" : [
      "gui",
      "tool",
      "hsv"
   ],
   "code" : "(\r\n\r\n/*\r\n\r\nDemonstration of a HSV Color Picker, raw version\r\n\r\nSteps :\r\n\r\n1) Copy/Paste get_color_picker() alongside a variable which will store the UserView.\r\n( color_picker = get_color_picker.value(); <- color_picker is an UserView )\r\n\r\n2) Create a function that the color picker will call when used. As is, only takes a Color() as arg.\r\n( modulate_color_via_picker = { | color | ... }; )\r\n\r\n3) Bind the two of them, using : userview_variable.bindFunction( function );\r\n( color_picker.bindFunction( modulate_color_via_picker ) )\r\n\r\nPink ftw, D.\r\n\r\n*/\r\n\r\nvar win = Window.new( bounds:Rect(300,300,300,600) );\r\n\r\nvar outer_view = UserView();\r\nvar modulated_color = Color.red;\r\nvar modulate_color_via_picker;\r\n\r\nvar color_picker;\r\nvar get_color_picker;\r\n\r\nget_color_picker = {\r\n\r\n\tvar color = Color( 1, 0, 0 );\r\n\tvar hsv = [ 0, 1, 1 ];\r\n\r\n\tvar color_picker_view = UserView();\r\n\tvar h_slider = UserView();\r\n\tvar sv_slider = UserView();\r\n\tvar helper = UserView();\r\n\r\n\tvar binded_function = nil;\r\n\r\n\tvar hsv_to_color = { | hsv |\r\n\r\n\t\tvar color = Color( 0, 0, 0 );\r\n\r\n\t\tvar h = hsv[0];\r\n\t\tvar s = hsv[1];\r\n\t\tvar v = hsv[2];\r\n\r\n\t\tvar c = s * v;\r\n\t\tvar x = c * ( 1 - ( ( h/60.0 )%2.0 -1 ).abs );\r\n\t\tvar m = v - c;\r\n\r\n\t\tif( ( h >= 0 ) && ( h < 60 ), {\r\n\t\t\tcolor.red = c;\r\n\t\t\tcolor.green = x;\r\n\t\t\tcolor.blue = 0;\r\n\t\t} );\r\n\r\n\t\tif( ( h >= 60 ) && ( h < 120 ), {\r\n\t\t\tcolor.red = x;\r\n\t\t\tcolor.green = c;\r\n\t\t\tcolor.blue = 0;\r\n\t\t} );\r\n\r\n\t\tif( ( h >= 120 ) && ( h < 180 ), {\r\n\t\t\tcolor.red = 0;\r\n\t\t\tcolor.green = c;\r\n\t\t\tcolor.blue = x;\r\n\t\t} );\r\n\r\n\t\tif( ( h >= 180 ) && ( h < 240 ), {\r\n\t\t\tcolor.red = 0;\r\n\t\t\tcolor.green = x;\r\n\t\t\tcolor.blue = c;\r\n\t\t} );\r\n\r\n\t\tif( ( h >= 240 ) && ( h < 300 ), {\r\n\t\t\tcolor.red = x;\r\n\t\t\tcolor.green = 0;\r\n\t\t\tcolor.blue = c;\r\n\t\t} );\r\n\r\n\t\tif( ( h >= 300 ) && ( h < 360 ), {\r\n\t\t\tcolor.red = c;\r\n\t\t\tcolor.green = 0;\r\n\t\t\tcolor.blue = x;\r\n\t\t} );\r\n\r\n\t\tcolor.red = color.red + m;\r\n\t\tcolor.green = color.green + m;\r\n\t\tcolor.blue = color.blue + m;\r\n\r\n\t\tcolor;\r\n\t};\r\n\r\n\th_slider.drawFunc_( { | view |\r\n\t\tPen.width = 1;\r\n\t\tview.bounds.height.do( { | index |\r\n\t\t\tPen.strokeColor_(\r\n\t\t\t\thsv_to_color.value(\r\n\t\t\t\t\t[\r\n\t\t\t\t\t\tindex.linlin(\r\n\t\t\t\t\t\t\t0,\r\n\t\t\t\t\t\t\tview.bounds.height,\r\n\t\t\t\t\t\t\t0,\r\n\t\t\t\t\t\t\t360\r\n\t\t\t\t\t\t),\r\n\t\t\t\t\t\t1,\r\n\t\t\t\t\t\t1\r\n\t\t\t\t\t];\r\n\t\t\t\t);\r\n\t\t\t);\r\n\t\t\tPen.moveTo( Point( 0, index ) );\r\n\t\t\tPen.lineTo( Point( view.bounds.width, index ) );\r\n\t\t\tPen.stroke;\r\n\t\t} );\r\n\t} );\r\n\r\n\th_slider.mouseDownAction_( { | view, x, y |\r\n\t\ty = y.linlin( 0, view.bounds.height, 0, 360 );\r\n\t\thsv[0] = y;\r\n\t\tcolor = hsv_to_color.value( hsv );\r\n\t\tif( binded_function != nil, { binded_function.value( color ) } );\r\n\t\thelper.refresh;\r\n\t\tsv_slider.refresh;\r\n\t} );\r\n\th_slider.mouseMoveAction_( h_slider.mouseDownAction );\r\n\r\n\tsv_slider.drawFunc_( { | view |\r\n\r\n\t\tview.bounds.width.do( { | index_x |\r\n\t\t\tPen.addRect(\r\n\t\t\t\tRect(\r\n\t\t\t\t\tindex_x,\r\n\t\t\t\t\t0,\r\n\t\t\t\t\t1,\r\n\t\t\t\t\tview.bounds.height\r\n\t\t\t\t)\r\n\t\t\t);\r\n\t\t\tPen.fillAxialGradient(\r\n\t\t\t\tPoint(0,0),\r\n\t\t\t\tPoint(0,view.bounds.height),\r\n\t\t\t\tColor.black,\r\n\t\t\t\thsv_to_color.value(\r\n\t\t\t\t\t[\r\n\t\t\t\t\t\thsv[0],\r\n\t\t\t\t\t\tindex_x.linlin( 0, view.bounds.width, 0, 1 ),\r\n\t\t\t\t\t\t1\r\n\t\t\t\t\t]\r\n\t\t\t\t)\r\n\t\t\t);\r\n\r\n\t\t} );\r\n\t} );\r\n\r\n\tsv_slider.mouseDownAction_( { | view, x, y |\r\n\t\tx = x.linlin( 0, view.bounds.width, 0, 1 );\r\n\t\ty = y.linlin( 0, view.bounds.height, 0, 1 );\r\n\t\thsv[1] = x;\r\n\t\thsv[2] = y;\r\n\t\tcolor = hsv_to_color.value( hsv );\r\n\t\tif( binded_function != nil, { binded_function.value( color ) } );\r\n\t\thelper.refresh;\r\n\t} );\r\n\tsv_slider.mouseMoveAction_( sv_slider.mouseDownAction );\r\n\r\n\thelper.drawFunc_( { | view |\r\n\t\tPen.fillColor_( color );\r\n\t\tPen.fillRect(\r\n\t\t\tRect(\r\n\t\t\t\t0,\r\n\t\t\t\t0,\r\n\t\t\t\tview.bounds.width,\r\n\t\t\t\tview.bounds.height\r\n\t\t\t)\r\n\t\t)\r\n\t} );\r\n\r\n\tcolor_picker_view.background_( Color( 0.5, 0.5, 0.5 ) );\r\n\tcolor_picker_view.layout_(\r\n\t\tVLayout(\r\n\t\t\t[ helper, stretch:1 ],\r\n\t\t\t[ HLayout(\r\n\t\t\t\t[ h_slider, stretch: 1 ],\r\n\t\t\t\t[ sv_slider, stretch: 2 ]\r\n\t\t\t), stretch:2 ]\r\n\t\t);\r\n\t);\r\n\r\n\tcolor_picker_view.addUniqueMethod( \\bindFunction, { | object, function | binded_function = function } );\r\n\r\n\tcolor_picker_view\r\n};\r\n\r\n\r\ncolor_picker = get_color_picker.value();\r\nouter_view.drawFunc = { | view |\r\n\tPen.stringCenteredIn(\r\n\t\t\"Hey !,\\nI'm located OUTSIDE of\\nthe color picker !\\n:3\",\r\n\t\tRect(\r\n\t\t\t0,\r\n\t\t\t0,\r\n\t\t\tview.bounds.width,\r\n\t\t\tview.bounds.height\r\n\t\t),\r\n\t\tcolor: modulated_color\r\n\t)\r\n};\r\n\r\n//That's probably what you are looking for :\r\nmodulate_color_via_picker = { | color |\r\n\tmodulated_color = color;\r\n\touter_view.refresh;\r\n};\r\ncolor_picker.bindFunction( modulate_color_via_picker );\r\n\r\nwin.layout_(\r\n\tVLayout(\r\n\t\t[ outer_view, stretch:1 ],\r\n\t\t[ color_picker, stretch:1 ]\r\n\t)\r\n);\r\n\r\nwin.front;\r\n\r\n)",
   "id" : "1-5fa",
   "is_private" : null,
   "author" : "Dindoléon",
   "name" : "ColorPicker ( HSV to Color ), RAW Version",
   "ancestor_list" : [],
   "description" : "Function that returns a color picker view, and allows to connect it to a function outside of the color picker itself.\r\nContains an HSV to Color() algorithm.\r\nNo embellishment."
}
