Skip to content

Commit a2575ad

Browse files
committed
Adding radial background and decoration (ellipse) for the bar ends
1 parent 6065e39 commit a2575ad

File tree

4 files changed

+105
-19
lines changed

4 files changed

+105
-19
lines changed

library/lcd/lcd_comm.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,29 @@ def DisplayLineGraph(self, x: int, y: int, width: int, height: int,
420420

421421
self.DisplayPILImage(graph_image, x, y)
422422

423+
def DrawRadialDecoration(self, draw: ImageDraw, angle: float, radius: float, width: float, color: Tuple[int, int, int] = (0, 0, 0)):
424+
i_cos = math.cos(angle*math.pi/180)
425+
i_sin = math.sin(angle*math.pi/180)
426+
x_f = (i_cos * (radius - width/2)) + radius
427+
if math.modf(x_f) == 0.5:
428+
if i_cos > 0:
429+
x_f = math.floor(x_f)
430+
else:
431+
x_f = math.ceil(x_f)
432+
else:
433+
x_f = math.floor(x_f + 0.5)
434+
435+
y_f = (i_sin * (radius - width/2)) + radius
436+
if math.modf(y_f) == 0.5:
437+
if i_sin > 0:
438+
y_f = math.floor(y_f)
439+
else:
440+
y_f = math.ceil(y_f)
441+
else:
442+
y_f = math.floor(y_f + 0.5)
443+
draw.ellipse([x_f - width/2, y_f - width/2, x_f + width/2, y_f - 1 + width/2 - 1], outline=color, fill=color, width=1)
444+
445+
423446
def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int,
424447
min_value: int = 0,
425448
max_value: int = 100,
@@ -438,7 +461,10 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
438461
background_color: Tuple[int, int, int] = (255, 255, 255),
439462
background_image: str = None,
440463
custom_bbox: Tuple[int, int, int, int] = (0, 0, 0, 0),
441-
text_offset: Tuple[int, int] = (0,0)):
464+
text_offset: Tuple[int, int] = (0,0),
465+
bar_background_color: Tuple[int, int, int] = (0, 0, 0),
466+
draw_bar_background: bool = False,
467+
bar_decoration: str = ""):
442468
# Generate a radial progress bar and display it
443469
# Provide the background image path to display progress bar with transparent background
444470

@@ -451,6 +477,9 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
451477
if isinstance(font_color, str):
452478
font_color = tuple(map(int, font_color.split(', ')))
453479

480+
if isinstance(bar_background_color, str):
481+
bar_background_color = tuple(map(int, bar_background_color.split(', ')))
482+
454483
if angle_start % 361 == angle_end % 361:
455484
if clockwise:
456485
angle_start += 0.1
@@ -502,6 +531,23 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
502531
ecart = 360 - angle_start + angle_end
503532
else:
504533
ecart = angle_end - angle_start
534+
535+
# draw bar background
536+
if draw_bar_background:
537+
if angle_end < angle_start:
538+
angleE = angle_start + ecart
539+
angleS = angle_start
540+
else:
541+
angleS = angle_start
542+
angleE = angle_start + ecart
543+
draw.arc([0, 0, diameter - 1, diameter - 1], angleS, angleE, fill=bar_background_color, width=bar_width)
544+
545+
# draw bar decoration
546+
if bar_decoration == "Ellipse":
547+
self.DrawRadialDecoration(draw = draw, angle = angle_end, radius = radius, width = bar_width, color = bar_background_color)
548+
self.DrawRadialDecoration(draw = draw, angle = angle_start, radius = radius, width = bar_width, color = bar_color)
549+
self.DrawRadialDecoration(draw = draw, angle = angle_start + pct * ecart, radius = radius, width = bar_width, color = bar_color)
550+
505551
#
506552
# solid bar case
507553
if angle_sep == 0:
@@ -535,6 +581,25 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
535581
ecart = angle_start - angle_end
536582
else:
537583
ecart = 360 - angle_end + angle_start
584+
585+
# draw bar background
586+
if draw_bar_background:
587+
if angle_end < angle_start:
588+
angleE = angle_start
589+
angleS = angle_start - ecart
590+
else:
591+
angleS = angle_start - ecart
592+
angleE = angle_start
593+
draw.arc([0, 0, diameter - 1, diameter - 1], angleS, angleE, fill=bar_background_color, width=bar_width)
594+
595+
596+
# draw bar decoration
597+
if bar_decoration == "Ellipse":
598+
self.DrawRadialDecoration(draw = draw, angle = angle_end, radius = radius, width = bar_width, color = bar_background_color)
599+
self.DrawRadialDecoration(draw = draw, angle = angle_start, radius = radius, width = bar_width, color = bar_color)
600+
self.DrawRadialDecoration(draw = draw, angle = angle_start - pct * ecart, radius = radius, width = bar_width, color = bar_color)
601+
602+
#
538603
# solid bar case
539604
if angle_sep == 0:
540605
if angle_end < angle_start:

library/stats.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ def display_themed_radial_bar(theme_data, value, min_size=0, unit='', custom_tex
179179
background_color=theme_data.get("BACKGROUND_COLOR", (0, 0, 0)),
180180
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None)),
181181
custom_bbox=theme_data.get("CUSTOM_BBOX", (0, 0, 0, 0)),
182-
text_offset=theme_data.get("TEXT_OFFSET", (0, 0))
182+
text_offset=theme_data.get("TEXT_OFFSET", (0, 0)),
183+
bar_background_color = theme_data.get("BAR_BACKGROUND_COLOR", (0, 0, 0)),
184+
draw_bar_background = theme_data.get("DRAW_BAR_BACKGROUND", False),
185+
bar_decoration = theme_data.get("BAR_DECORATION", "")
183186
)
184187

185188

1.56 KB
Loading

res/themes/Advanced Radials Test/theme.yaml

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ STATS:
3030
X: 120
3131
Y: 100
3232
RADIUS: 80
33-
WIDTH: 15
33+
WIDTH: 26
3434
MIN_VALUE: 0
3535
MAX_VALUE: 100
36-
ANGLE_START: 100
37-
ANGLE_END: 260
36+
ANGLE_START: 110
37+
ANGLE_END: 250
3838
ANGLE_STEPS: 10
39-
ANGLE_SEP: 5
39+
ANGLE_SEP: 0
4040
CLOCKWISE: True
4141
BAR_COLOR: 0, 250, 0
42+
BAR_BACKGROUND_COLOR: 0, 50, 0
43+
DRAW_BAR_BACKGROUND: True
44+
BAR_DECORATION: Ellipse
4245
SHOW_TEXT: True
4346
SHOW_UNIT: True
4447
FONT: roboto/Roboto-Bold.ttf
@@ -55,15 +58,18 @@ STATS:
5558
X: 300
5659
Y: 180
5760
RADIUS: 80
58-
WIDTH: 15
61+
WIDTH: 26
5962
MIN_VALUE: 0
6063
MAX_VALUE: 100
61-
ANGLE_START: 190
62-
ANGLE_END: 350
64+
ANGLE_START: 200
65+
ANGLE_END: 340
6366
ANGLE_STEPS: 10
64-
ANGLE_SEP: 5
67+
ANGLE_SEP: 0
6568
CLOCKWISE: True
6669
BAR_COLOR: 0, 250, 0
70+
BAR_BACKGROUND_COLOR: 0, 50, 0
71+
DRAW_BAR_BACKGROUND: True
72+
BAR_DECORATION: Ellipse
6773
SHOW_TEXT: True
6874
SHOW_UNIT: True
6975
FONT: roboto/Roboto-Bold.ttf
@@ -83,11 +89,13 @@ STATS:
8389
MIN_VALUE: 0
8490
MAX_VALUE: 100
8591
ANGLE_START: 220
86-
ANGLE_END: 220
92+
ANGLE_END: 210
8793
ANGLE_STEPS: 20
8894
ANGLE_SEP: 0
8995
CLOCKWISE: True
9096
BAR_COLOR: 200, 100, 50
97+
BAR_BACKGROUND_COLOR: 50, 0, 0
98+
DRAW_BAR_BACKGROUND: True
9199
SHOW_TEXT: True
92100
SHOW_UNIT: True
93101
FONT: roboto/Roboto-Bold.ttf
@@ -133,6 +141,8 @@ STATS:
133141
ANGLE_SEP: 25
134142
CLOCKWISE: True
135143
BAR_COLOR: 0, 0, 200
144+
BAR_BACKGROUND_COLOR: 0, 0, 50
145+
DRAW_BAR_BACKGROUND: True
136146
SHOW_TEXT: True
137147
SHOW_UNIT: True
138148
FONT: roboto/Roboto-Bold.ttf
@@ -148,15 +158,18 @@ STATS:
148158
X: 120
149159
Y: 100
150160
RADIUS: 80
151-
WIDTH: 15
161+
WIDTH: 26
152162
MIN_VALUE: 0
153163
MAX_VALUE: 100
154-
ANGLE_START: 80
155-
ANGLE_END: 280
164+
ANGLE_START: 70
165+
ANGLE_END: 290
156166
ANGLE_STEPS: 10
157-
ANGLE_SEP: 5
167+
ANGLE_SEP: 0
158168
CLOCKWISE: False
159169
BAR_COLOR: 255, 0, 0
170+
BAR_BACKGROUND_COLOR: 50, 0, 0
171+
DRAW_BAR_BACKGROUND: True
172+
BAR_DECORATION: Ellipse
160173
SHOW_TEXT: True
161174
SHOW_UNIT: True
162175
FONT: roboto/Roboto-Bold.ttf
@@ -195,15 +208,18 @@ STATS:
195208
X: 300
196209
Y: 180
197210
RADIUS: 80
198-
WIDTH: 15
211+
WIDTH: 26
199212
MIN_VALUE: 0
200213
MAX_VALUE: 100
201-
ANGLE_START: 170
202-
ANGLE_END: 10
214+
ANGLE_START: 160
215+
ANGLE_END: 20
203216
ANGLE_STEPS: 10
204-
ANGLE_SEP: 5
217+
ANGLE_SEP: 0
205218
CLOCKWISE: False
206219
BAR_COLOR: 255, 0, 0
220+
BAR_BACKGROUND_COLOR: 50, 0, 0
221+
DRAW_BAR_BACKGROUND: True
222+
BAR_DECORATION: Ellipse
207223
SHOW_TEXT: True
208224
SHOW_UNIT: True
209225
FONT: roboto/Roboto-Bold.ttf
@@ -228,6 +244,8 @@ STATS:
228244
ANGLE_SEP: 0
229245
CLOCKWISE: False
230246
BAR_COLOR: 200, 100, 50
247+
BAR_BACKGROUND_COLOR: 100, 50, 25
248+
DRAW_BAR_BACKGROUND: True
231249
SHOW_TEXT: True
232250
SHOW_UNIT: True
233251
FONT: roboto/Roboto-Bold.ttf

0 commit comments

Comments
 (0)