Search code examples
pythonpython-3.xvpython

vPython - gradually change colors


I wanted to make an object change its colors according to the rainbow. As this did not work out I tried to visualize the problem with visual.graph: visual.graph The color build out of the visualized colors can be seen below the x-axis. It does not change gradually. In case the bars should build a nice spectrum.

from visual.graph import *

def regenbogenfarben(ausgangsfarbe=(255,0,0)):
    "liefert ausgehend vom input die nächste Farbe im Regenbogen"
    # vergleiche http://www.mikrocontroller.net/topic/238304
    step = 51
    r,g,b = ausgangsfarbe       # split the tuple...
    if r==255 and b==0 and g!=255:
        g+=5                # mehr grün
    elif b==0 and g==255 and r!=0:
        r-=5                # weniger rot
    elif r==0 and g==255 and b!=255:
        b+=5                # mehr blau
    elif b==255 and r==0 and g!=0:
        g-=5                # weniger grün
    elif g==0 and b==255 and r!=255:
        r+=5                # mehr rot
    elif g==0 and r==255 and b!=0:
        b-=5                # weniger blau
    #print((r,g,b))
    return r,g,b            # tupel zurückgeben
gdisplay(background=color.white, foreground=color.black,
         ytitle="Farbanteil", xtitle="step")
r=gcurve(color=color.red)
g=gcurve(color=color.green)
b=gcurve(color=color.blue)      # drei Farbanteile veranschaulichen

rgb=(255,0,0)

farbe=gvbars(color=color.red)   # Farbe darstellen

for i in range(6*51+20):
    r.plot(pos=(i,rgb[0]))
    g.plot(pos=(i,rgb[1]))
    b.plot(pos=(i,rgb[2]))
    farbe.plot(pos=(i,-10),color=rgb)
    rgb=regenbogenfarben(rgb)

Solution

  • Okay, I found the mistake: vPython takes only numbers from 0 to 1 for colors. So dividing all colors by 255 works.

    enter image description here