Skip to content

Commit

Permalink
Merge pull request #166 from g4bri3lDev/main
Browse files Browse the repository at this point in the history
Added more shapes
  • Loading branch information
g4bri3lDev committed Aug 24, 2024
2 parents 998a4b0 + cfdca5b commit 1a261a3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 8 deletions.
62 changes: 54 additions & 8 deletions custom_components/open_epaper_link/imagegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import pprint
import math
import json
from math import radians

import requests
import qrcode
import shutil
Expand Down Expand Up @@ -152,11 +154,14 @@ def customimage(entity_id, service, hass):
if element["type"] == "rectangle":
check_for_missing_required_arguments(element, ["x_start", "x_end", "y_start", "y_end"], "rectangle")
img_rect = ImageDraw.Draw(img)
fill = getIndexColor(element['fill']) if 'fill' in element else None
outline = getIndexColor(element['outline']) if 'outline' in element else "black"
width = element['width'] if 'width' in element else 1
img_rect.rectangle([(element['x_start'], element['y_start']), (element['x_end'], element['y_end'])],
fill=fill, outline=outline, width=width)
rect_fill = getIndexColor(element['fill']) if 'fill' in element else None
rect_outline = getIndexColor(element['outline']) if 'outline' in element else "black"
rect_width = element['width'] if 'width' in element else 1
radius = element['radius'] if 'radius' in element else 10 if 'corners' in element else 0
corners = rounded_corners(element['corners']) if 'corners' in element else rounded_corners("all") if 'radius' in element else (False, False, False, False)

img_rect.rounded_rectangle([(element['x_start'], element['y_start']), (element['x_end'], element['y_end'])],
fill=rect_fill, outline=rect_outline, width=rect_width, radius=radius, corners=corners)
# rectangle pattern
if element["type"] == "rectangle_pattern":
check_for_missing_required_arguments(element, ["x_start", "x_size",
Expand All @@ -167,14 +172,36 @@ def customimage(entity_id, service, hass):
fill = getIndexColor(element['fill']) if 'fill' in element else None
outline = getIndexColor(element['outline']) if 'outline' in element else "black"
width = element['width'] if 'width' in element else 1
radius = element['radius'] if 'radius' in element else 10 if 'corners' in element else 0
corners = rounded_corners(element['corners']) if 'corners' in element else rounded_corners("all") if 'radius' in element else (False, False, False, False)

for x in range(element["x_repeat"]):
for y in range(element["y_repeat"]):
img_rect_pattern.rectangle([(element['x_start'] + x * (element['x_offset'] + element['x_size']),
img_rect_pattern.rounded_rectangle([(element['x_start'] + x * (element['x_offset'] + element['x_size']),
element['y_start'] + y * (element['y_offset'] + element['y_size'])),
(element['x_start'] + x * (element['x_offset'] + element['x_size'])
+ element['x_size'], element['y_start'] + y * (element['y_offset']
+ element['y_size'])+element['y_size'])],
fill=fill, outline=outline, width=width)
fill=fill, outline=outline, width=width, radius=radius, corners=corners)

# circle
if element["type"] == "circle":
check_for_missing_required_arguments(element, ["x", "y", "radius"], "circle")
img_circle = ImageDraw.Draw(img)
fill = getIndexColor(element['fill']) if 'fill' in element else None
outline = getIndexColor(element['outline']) if 'outline' in element else "black"
width = element['width'] if 'width' in element else 1
img_circle.circle((element['x'], element['y']), element['radius'], fill=fill, outline=outline, width=width)

# ellipse
if element["type"] == "ellipse":
check_for_missing_required_arguments(element, ["x_start", "x_end", "y_start", "y_end"], "ellipse")
img_ellipse = ImageDraw.Draw(img)
fill = getIndexColor(element['fill']) if 'fill' in element else None
outline = getIndexColor(element['outline']) if 'outline' in element else "black"
width = element['width'] if 'width' in element else 1
img_ellipse.ellipse([(element['x_start'], element['y_start']), (element['x_end'], element['y_end'])],
fill=fill, outline=outline, width=width)

# text
if element["type"] == "text":
Expand Down Expand Up @@ -800,4 +827,23 @@ def check_for_missing_required_arguments(element, required_keys, func_name):
if key not in element:
missing_keys.append(key)
if missing_keys:
raise HomeAssistantError(f"Missing required argument(s) '{', '.join(missing_keys)}' in '{func_name}'")
raise HomeAssistantError(f"Missing required argument(s) '{', '.join(missing_keys)}' in '{func_name}'")

def rounded_corners(corner_string):
if corner_string == "all":
return True, True, True, True

corners = corner_string.split(",")
corner_map = {
"top_left": 0,
"top_right": 1,
"bottom_right": 2,
"bottom_left": 3
}
result = [False] * 4
for corner in corners:
corner = corner.strip()
if corner in corner_map:
result[corner_map[corner]] = True

return tuple(result)
46 changes: 46 additions & 0 deletions docs/drawcustom/supported_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ Draws a rectangle.
- **outline** (optional) default black
- **width** (optional) width of outline, default 1
- **visible** (optional) show element, default: True
- **radius** (optional) default 0, if set, the rectangle will be drawn with rounded corners
- **corners** (optional) if set, the rectangle will be drawn with rounded corners, possible values are `all`, or `top_left`, `top_right`, `bottom_left`, `bottom_right` separated by a comma, if set without radius the default radius will be 10

### rectangle pattern

Expand Down Expand Up @@ -206,6 +208,50 @@ Draws rectangles that are repeated in x and y direction.
- **x_repeat** (required) number of rectangles in x direction
- **y_repeat** (required) number of rectangles in y direction
- **visible** (optional) show element, default: True
- **radius** (optional) default 0, if set, the rectangle will be drawn with rounded corners, if set without corners all corners will be rounded
- **corners** (optional) if set, the rectangle will be drawn with rounded corners, possible values are `all`, or `top_left`, `top_right`, `bottom_left`, `bottom_right` separated by a comma, if set without radius the default radius will be 10

### circle

Draws a circle around a center point.

```yaml
- type: circle
x: 50
y: 50
radius: 20
```

#### Parameters:

- **x** (required) x position of the center
- **y** (required) y position of the center
- **radius** (required) radius of the circle
- **fill** (optional) default `null`, use `null` to not draw the inside
- **outline** (optional) default black
- **width** (optional) width of outline, default 1

### ellipse

Draws an ellipse inside the bounding box.

```yaml
- type: ellipse
x_start: 50
x_end: 100
y_start: 50
y_end: 100
```

#### Parameters:

- **x_start** (required) x position of the upper left corner
- **y_start** (required) y position of the upper left corner
- **x_end** (required) x position of the lower right corner
- **y_end** (required) y position of the lower right corner
- **fill** (optional) default `null`, use `null` to not draw the inside
- **outline** (optional) default black
- **width** (optional) width of outline, default 1

### icon

Expand Down

0 comments on commit 1a261a3

Please sign in to comment.