File: colorsliders.py

package info (click to toggle)
python-visual 3.2.9-4.1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,796 kB
  • ctags: 2,664
  • sloc: cpp: 11,958; sh: 8,185; python: 3,709; ansic: 480; makefile: 311
file content (96 lines) | stat: -rw-r--r-- 3,657 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from visual import *

print """
Click and drag a ball (with mouse button down) on an RGB or HSV slider.
Click one of the colored objects to print RGB and HSV values.
"""

scene.userspin = 0
grey = (0.85, 0.85, 0.85)

class slider:
    def __init__(self, pos=vector(0,0,0), axis=vector(0,1.,0), value=None,
                 width=0.15, min=0, max=100., color=(1,0,0)):
        pos = vector(pos)
        axis = vector(axis)
        if value == None:
            value = min
        self.min = min
        self.max = max
        self.value = value
        self.shaft = cylinder(pos=pos, axis=axis, radius=width/4., color=grey)
        self.start = pos
        self.axis = axis
        self.control = sphere(pos=self.start+(self.min+value)/(self.max-self.min)*self.axis,
                        radius=width/2., color = color)
        self.label = label(pos=self.control.pos, text="%0.2f" % value,
                           opacity=0, box=0, line=0)

    def getslider(self, pos=None):
        pos = vector(pos)
        value = self.min+(self.max-self.min)*(pos.y-self.start.y)/mag(self.axis)
        self.setslider(value)
        return value

    def setslider(self, value=None):
        if value > self.max:
            value = self.max
        if value < self.min:
            value = self.min
        self.value = value
        self.control.pos = self.start+(self.min+value)/(self.max-self.min)*self.axis
        self.label.pos = self.control.pos
        self.label.text="%0.2f" % value

scene.width = 800
scene.height = 400
scene.center = (0,0.5,0)
scene.title = "RGB and HSV color"
wcube = 0.2
rgb = (1,0,0)
hsv = color.rgb_to_hsv((1,0,0))
ctrl = [slider(pos=(-1.5,0,0), color=(1,0,0), max=1., value=rgb[0]),
           slider(pos=(-1.25,0,0), color=(0,1,0), max=1., value=rgb[1]),
           slider(pos=(-1.0,0,0), color=(0,0,1), max=1., value=rgb[2]),
        slider(pos=(+1.0,0,0), color=(1,0,0), max=1., value=hsv[0]),
           slider(pos=(+1.25,0,0), color=(1,1,1), max=1., value=hsv[1]),
           slider(pos=(+1.5,0,0), color=(0.5,0.5,0.5), max=1., value=hsv[2])]
panel = box(pos=(0,0.5,0), length=1.5, height=1, width=0.1, color=rgb)
ball = sphere(pos=(0,0.5,0), radius=0.5, color=rgb)
cube = box(pos=(0,1.2,0), axis=(1,1,1),
           length=wcube, width=wcube, height=wcube, color=rgb)
dragobj = None

while 1:
    rate(50)
    cube.rotate(angle=0.1, axis=scene.up)
    if scene.mouse.events:
        m = scene.mouse.getevent()
        if m.click == "left" and m.pick in [panel, ball, cube]:
            print "RGB = (%0.3f,%0.3f,%0.3f)" % (ctrl[0].value,ctrl[1].value,ctrl[2].value)
            print "HSV = (%0.3f,%0.3f,%0.3f)" % (ctrl[3].value,ctrl[4].value,ctrl[5].value)
            continue
        elif m.drop == "left":
            dragobj = None
        elif m.drag == "left":
            for index in range(6):
                s = ctrl[index]
                if m.pick is s.control:
                    pos = m.project(normal=(0,0,1))
                    dragobj = s
                    break
    newpos = scene.mouse.project(normal=(0,0,1))
    if dragobj and (newpos != pos):
        pos = newpos
        dragobj.getslider(pos)
        if index <= 2: # rgb sliders
            cube.color = ball.color = panel.color = (ctrl[0].value,ctrl[1].value,ctrl[2].value)
            hsv = color.rgb_to_hsv(ball.color)
            for nn in range(3):
                ctrl[nn+3].setslider(hsv[nn])
        else: # hsv sliders
            rgb = color.hsv_to_rgb((ctrl[3].value,ctrl[4].value,ctrl[5].value))
            cube.color = ball.color = panel.color = rgb
            for nn in range(3):
                ctrl[nn].setslider(rgb[nn])