4 from math
import pi
, cos
, sin
7 background
= "knob3_bg.png"
9 x
, y
= WIDTH
/ 2, HEIGHT
/ 2
11 radius
= WIDTH
/ 2 - lwidth
12 radiusplus
= radius
+ lwidth
/ 2
13 radiusminus
= radius
- lwidth
/ 2
14 radiusminus2
= radius
- lwidth
15 radiusminus3
= radius
- lwidth
* 3 / 2
16 radiusint
= (radius
- lwidth
/ 2) * 0.25
22 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, phases
* WIDTH
, HEIGHT
* 4)
23 ctx
= cairo
.Context(surface
)
24 ctx
.set_source_rgba(0.75, 0.75, 0.75, 0)
25 ctx
.rectangle(0, 0, phases
* WIDTH
, 4 * HEIGHT
)
28 for variant
in range(0, 4):
30 y
= HEIGHT
* (variant
+ 0.5)
31 for phase
in range(0, phases
):
32 # Draw background image
33 bgimage
= cairo
.ImageSurface
.create_from_png(background
)
34 ctx
.set_source_surface(bgimage
, x
- WIDTH
/ 2, y
- HEIGHT
/ 2);
35 ctx
.rectangle(phase
* WIDTH
, variant
* HEIGHT
, WIDTH
, HEIGHT
)
38 # Draw out the triangle using absolute coordinates
39 value
= phase
* 1.0 / (phases
- 1)
41 sangle
= (180 - 45)*pi
/180
42 eangle
= (360 + 45)*pi
/180
46 eangle
= (270 + 360)*pi
/180
48 vangle
= sangle
+ value
* (eangle
- sangle
)
49 c
, s
= cos(vangle
), sin(vangle
)
51 midled
= (nleds
- 1) / 2
52 midphase
= (phases
- 1) / 2
53 thresholdP
= midled
+ 1 + ((phase
- midphase
- 1) * 1.0 * (nleds
- midled
- 2) / (phases
- midphase
- 2))
54 thresholdN
= midled
- 1 - ((midphase
- 1 - phase
) * 1.0 * (nleds
- midled
- 2) / (midphase
- 1))
57 for led
in range(0, nleds
):
59 adelta
= (eangle
- sangle
) / (nleds
)
61 adelta
= (eangle
- sangle
- spacing
) / (nleds
- 1)
66 lvalue
= led
* 1.0 / (nleds
- 1)
67 pvalue
= phase
* 1.0 / (phases
- 1)
69 # XXXKF works only for phases = 2 * leds
71 lit
= led
== exled
or (phase
== phases
- 1 and led
== 0)
72 glowlit
= led
== (exled
+ 0.5) or led
== (exled
- 0.5)
74 hilite
= (phase
% ((phases
- 1) / 4)) == 0
75 if variant
== 0: lit
= (pvalue
== 1.0) or pvalue
> lvalue
78 lit
= (phase
== midphase
)
79 #glowlit = (phase < midphase and thresholdN >= midled - 1) or (phase > midphase and thresholdP <= midled + 1)
82 elif led
> midled
and phase
> midphase
:
83 # led = [midled + 1, nleds - 1]
84 # phase = [midphase + 1, phases - 1]
85 lit
= led
<= thresholdP
86 glowlit
= led
<= thresholdP
+ 1
88 elif led
< midled
and phase
< midphase
:
89 lit
= led
>= thresholdN
90 glowlit
= led
>= thresholdN
- 1
94 if variant
== 2: lit
= pvalue
== 0 or pvalue
< lvalue
97 ctx
.set_source_rgb(0.0, 0.1, 0.1)
99 ctx
.set_source_rgb(0.0 * glowval
, 0.75 * glowval
, 1.0 * glowval
)
102 ctx
.set_source_rgb(0.3, 1.0, 1.0)
104 ctx
.set_source_rgb(0.0, 0.75, 1.0)
105 ctx
.set_line_width(3)
107 ctx
.set_line_width(4)
108 ctx
.arc(x
, y
, radius
, sangle
+ adelta
* led
, sangle
+ adelta
* led
+ spacing
)
111 ctx
.set_source_rgba(1, 1, 1, 1)
112 ctx
.set_line_width(2)
113 mtx
= ctx
.get_matrix()
114 ctx
.translate(x
+ radiusminus2
* c
, y
+ radiusminus2
* s
)
117 ctx
.line_to(-radius
/5, 0)
123 surface
.write_to_png(output
)