4 from math
import pi
, cos
, sin
7 x
, y
= WIDTH
/ 2, HEIGHT
/ 2
9 radius
= WIDTH
/ 2 - lwidth
10 radiusplus
= radius
+ lwidth
/ 2
11 radiusminus
= radius
- lwidth
/ 2
12 radiusminus2
= radius
- lwidth
13 radiusminus3
= radius
- lwidth
* 3 / 2
14 radiusint
= (radius
- lwidth
/ 2) * 0.25
20 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, phases
* WIDTH
, HEIGHT
* 4)
21 ctx
= cairo
.Context(surface
)
22 ctx
.set_source_rgba(0.75, 0.75, 0.75, 0)
23 ctx
.rectangle(0, 0, phases
* WIDTH
, 4 * HEIGHT
)
26 for variant
in range(0, 4):
28 y
= HEIGHT
* (variant
+ 0.5)
29 for phase
in range(0, phases
):
30 # Draw out the triangle using absolute coordinates
31 value
= phase
* 1.0 / (phases
- 1)
33 sangle
= (180 - 45)*pi
/180
34 eangle
= (360 + 45)*pi
/180
38 eangle
= (270 + 360)*pi
/180
40 vangle
= sangle
+ value
* (eangle
- sangle
)
41 c
, s
= cos(vangle
), sin(vangle
)
43 midled
= (nleds
- 1) / 2
44 midphase
= (phases
- 1) / 2
45 thresholdP
= midled
+ 1 + ((phase
- midphase
- 1) * 1.0 * (nleds
- midled
- 2) / (phases
- midphase
- 2))
46 thresholdN
= midled
- 1 - ((midphase
- 1 - phase
) * 1.0 * (nleds
- midled
- 2) / (midphase
- 1))
49 for led
in range(0, nleds
):
51 adelta
= (eangle
- sangle
) / (nleds
)
53 adelta
= (eangle
- sangle
- spacing
) / (nleds
- 1)
58 lvalue
= led
* 1.0 / (nleds
- 1)
59 pvalue
= phase
* 1.0 / (phases
- 1)
61 # XXXKF works only for phases = 2 * leds
63 lit
= led
== exled
or (phase
== phases
- 1 and led
== 0)
64 glowlit
= led
== (exled
+ 0.5) or led
== (exled
- 0.5)
66 hilite
= (phase
% ((phases
- 1) / 4)) == 0
67 if variant
== 0: lit
= (pvalue
== 1.0) or pvalue
> lvalue
70 lit
= (phase
== midphase
)
71 #glowlit = (phase < midphase and thresholdN >= midled - 1) or (phase > midphase and thresholdP <= midled + 1)
74 elif led
> midled
and phase
> midphase
:
75 # led = [midled + 1, nleds - 1]
76 # phase = [midphase + 1, phases - 1]
77 lit
= led
<= thresholdP
78 glowlit
= led
<= thresholdP
+ 1
80 elif led
< midled
and phase
< midphase
:
81 lit
= led
>= thresholdN
82 glowlit
= led
>= thresholdN
- 1
86 if variant
== 2: lit
= pvalue
== 0 or pvalue
< lvalue
89 ctx
.set_source_rgb(0, 0, 0)
91 ctx
.set_source_rgb(1 * glowval
, 0.5 * glowval
, 0)
94 ctx
.set_source_rgb(1, 1, 0)
96 ctx
.set_source_rgb(1, 0.5, 0)
100 ctx
.arc(x
, y
, radius
, sangle
+ adelta
* led
, sangle
+ adelta
* led
+ spacing
)
103 #ctx.set_line_width(lwidth)
104 #ctx.set_source_rgb(1, 0.5, 0)
105 #ctx.arc(x, y, radius, sangle, vangle)
106 #ctx.line_to(x + radiusint * c, y + radiusint * s)
109 grad
= cairo
.LinearGradient(x
- radius
/ 2, y
- radius
/ 2, x
+ radius
/ 2, y
+ radius
/ 2)
110 #grad.add_color_stop_rgb(0.0, 0.5, 0.5, 0.5)
111 #grad.add_color_stop_rgb(1.0, 0.8, 0.8, 0.8)
112 grad
.add_color_stop_rgb(0.0, 0.5, 0.5, 0.5)
113 grad
.add_color_stop_rgb(1.0, 0.8, 0.8, 0.8)
115 # ctx.set_source_rgb(0.8, 0.8, 0.8)
116 ctx
.set_line_width(2)
117 ctx
.arc(x
, y
, radiusminus2
, 0, 2 * pi
)
120 grad
= cairo
.LinearGradient(x
- radius
/ 2, y
- radius
/ 2, x
+ radius
/ 2, y
+ radius
/ 2)
121 grad
.add_color_stop_rgb(0.0, 0.8, 0.8, 0.8)
122 grad
.add_color_stop_rgb(1.0, 0.5, 0.5, 0.5)
124 ctx
.arc(x
, y
, radiusminus3
, 0, 2 * pi
)
126 ctx
.set_source_rgb(0, 0, 0)
127 ctx
.set_line_width(2)
128 ctx
.arc(x
, y
, radiusminus2
, 0, 2 * pi
)
131 ctx
.set_source_rgba(0, 0, 0, 0.5)
132 ctx
.set_line_width(1)
133 mtx
= ctx
.get_matrix()
134 ctx
.translate(x
+ radiusminus2
* c
, y
+ radiusminus2
* s
)
137 ctx
.line_to(-radius
/2, 0)
142 #ctx.set_source_rgb(1, 0.5, 0)
143 #ctx.line_to(x + radiusplus * c, y + radiusplus * s)
147 surface
.write_to_png("knob.png")