{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "require.undef(\"nbextensions/jquery-ui.custom.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/glow.2.1a.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/glow.2.1b.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/glowcomm\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require([\"nbextensions/glowcomm\"], function(){console.log(\"glowcomm loaded\");})" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "window.__context = { glowscript_container: $(\"#glowscript\").removeAttr(\"id\")}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import ipywidgets as wd\n", "from vpython import *\n", "# This version uses Jupyter notebook slider\n", "# See Color-RGB-HSV2 for a version that uses VPython slider\n", "\n", "scene.userzoom = False\n", "scene.userspin = False\n", "scene.width = 400\n", "scene.height = 200\n", "scene.range = 1\n", "box(pos=vector(10,0,0)) # Force creation of canvas; box is not seen because it is outside the canvas\n", "scene.caption = 'You can Ctrl-C copy the RGB and HSV values shown above.'\n", "\n", "C = ['Red', 'Green', 'Blue', 'Hue', 'Saturation', 'Value']\n", "sliders = []\n", "lastvalues = []\n", "adjusting = False\n", "\n", "def set_background(change):\n", " global adjusting\n", " if adjusting: return # in the process of resetting various slider values\n", " s = []\n", " N = -1\n", " for i in range(6): # Get values for all 6 sliders\n", " val = sliders[i].value\n", " s.append(val)\n", " if val != lastvalues[i]:\n", " N = i\n", " lastvalues[i] = val\n", " adjusting = True # prevent circular updating of slider values\n", " if N < 3:\n", " rgb = vector(s[0],s[1],s[2])\n", " hsv = color.rgb_to_hsv(rgb)\n", " sliders[3].value = lastvalues[3] = int(1000*hsv.x)/1000 # reset HSV slider positions; display 3 figures\n", " sliders[4].value = lastvalues[4] = int(1000*hsv.y)/1000\n", " sliders[5].value = lastvalues[5] = int(1000*hsv.z)/1000\n", " else:\n", " hsv = vector(s[3],s[4],s[5])\n", " rgb = color.hsv_to_rgb(hsv)\n", " sliders[0].value = lastvalues[0] = int(1000*rgb.x)/1000 # reset RGB slider positions; display 3 figures\n", " sliders[1].value = lastvalues[1] = int(1000*rgb.y)/1000\n", " sliders[2].value = lastvalues[2] = int(1000*rgb.z)/1000\n", " scene.background = rgb\n", " # For readability, limit precision of display of quantities to 3 figures\n", " f = \"RGB = <{:1.3f}, {:1.3f}, {:1.3f}>, HSV = <{:1.3f}, {:1.3f}, {:1.3f}>\"\n", " scene.title = f.format(rgb.x, rgb.y, rgb.z, hsv.x, hsv.y, hsv.z)\n", " adjusting = False\n", "\n", "for i in range(6):\n", " sliders.append(wd.FloatSlider(description=C[i], min=0, max=1, step=0.001, value=0))\n", " lastvalues.append(0)\n", " sliders[i].observe(set_background, names='value') # watch for changes in slider.value\n", "display(wd.VBox(children=sliders)) # stack (default horizontal) sliders vertically\n", "sliders[0].value = 1 # make the background red\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "VPython", "language": "python", "name": "vpython" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }