import ipywidgets as wd
from vpython import *
# This version uses Jupyter notebook slider
# See Color-RGB-HSV2 for a version that uses VPython slider
scene.userzoom = False
scene.userspin = False
scene.width = 400
scene.height = 200
scene.range = 1
box(pos=vector(10,0,0)) # Force creation of canvas; box is not seen because it is outside the canvas
scene.caption = 'You can Ctrl-C copy the RGB and HSV values shown above.'
C = ['Red', 'Green', 'Blue', 'Hue', 'Saturation', 'Value']
sliders = []
lastvalues = []
adjusting = False
def set_background(change):
global adjusting
if adjusting: return # in the process of resetting various slider values
s = []
N = -1
for i in range(6): # Get values for all 6 sliders
val = sliders[i].value
s.append(val)
if val != lastvalues[i]:
N = i
lastvalues[i] = val
adjusting = True # prevent circular updating of slider values
if N < 3:
rgb = vector(s[0],s[1],s[2])
hsv = color.rgb_to_hsv(rgb)
sliders[3].value = lastvalues[3] = int(1000*hsv.x)/1000 # reset HSV slider positions; display 3 figures
sliders[4].value = lastvalues[4] = int(1000*hsv.y)/1000
sliders[5].value = lastvalues[5] = int(1000*hsv.z)/1000
else:
hsv = vector(s[3],s[4],s[5])
rgb = color.hsv_to_rgb(hsv)
sliders[0].value = lastvalues[0] = int(1000*rgb.x)/1000 # reset RGB slider positions; display 3 figures
sliders[1].value = lastvalues[1] = int(1000*rgb.y)/1000
sliders[2].value = lastvalues[2] = int(1000*rgb.z)/1000
scene.background = rgb
# For readability, limit precision of display of quantities to 3 figures
f = "RGB = <{:1.3f}, {:1.3f}, {:1.3f}>, HSV = <{:1.3f}, {:1.3f}, {:1.3f}>"
scene.title = f.format(rgb.x, rgb.y, rgb.z, hsv.x, hsv.y, hsv.z)
adjusting = False
for i in range(6):
sliders.append(wd.FloatSlider(description=C[i], min=0, max=1, step=0.001, value=0))
lastvalues.append(0)
sliders[i].observe(set_background, names='value') # watch for changes in slider.value
display(wd.VBox(children=sliders)) # stack (default horizontal) sliders vertically
sliders[0].value = 1 # make the background red
Failed to display Jupyter Widget of type VBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.