Skip to content

Commit

Permalink
added ColourType to support colour strings and RGB tuples, added link…
Browse files Browse the repository at this point in the history
… fill colour setting
  • Loading branch information
cracked-machine committed Mar 6, 2024
1 parent 28c0f3a commit 0c83fa6
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 58 deletions.
25 changes: 21 additions & 4 deletions doc/example/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@
"legend_width": 30,
"bgcolour": "white",
"void_fill_colour": "white",
"void_line_colour": "grey",
"title_fill_colour": "blanchedalmond",
"title_line_colour": "grey",
"void_line_colour": [
192,
192,
192
],
"title_fill_colour": [
224,
224,
224
],
"title_line_colour": [
32,
32,
32
],
"memory_maps": {
"eMMC": {
"memory_regions": {
Expand Down Expand Up @@ -50,5 +62,10 @@
},
"indent_scheme": "alternate",
"region_alpha": 192,
"link_alpha": 96
"link_alpha": 96,
"link_fill_colour": [
224,
224,
224
]
}
6 changes: 3 additions & 3 deletions doc/example/test_generate_doc_example_collisions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![memory map diagram](test_generate_doc_example_collisions_cropped.png)
|name|origin|size|free Space|collisions
|:-|:-|:-|:-|:-|
|<span style='color:(16, 41, 21, 255)'>dtb</span>|0x90|0x30|0x328|{'rootfs': '0x90'}|
|<span style='color:(33, 22, 21, 255)'>rootfs</span>|0x50|0x50|-0x10|{'kernel': '0x50', 'dtb': '0x90'}|
|<span style='color:(36, 43, 13, 255)'>kernel</span>|0x10|0x60|-0x20|{'rootfs': '0x50'}|
|<span style='color:(38, 52, 35)'>dtb</span>|0x90|0x30|0x328|{'rootfs': '0x90'}|
|<span style='color:(35, 54, 1)'>rootfs</span>|0x50|0x50|-0x10|{'kernel': '0x50', 'dtb': '0x90'}|
|<span style='color:(26, 52, 68)'>kernel</span>|0x10|0x60|-0x20|{'rootfs': '0x50'}|
Binary file modified doc/example/test_generate_doc_example_collisions_redux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions doc/example/test_generate_doc_example_normal.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![memory map diagram](test_generate_doc_example_normal_cropped.png)
|name|origin|size|free Space|collisions
|:-|:-|:-|:-|:-|
|<span style='color:(28, 39, 64, 255)'>dtb</span>|0x190|0x30|0x228|{}|
|<span style='color:(5, 32, 67, 255)'>rootfs</span>|0x50|0x30|0x110|{}|
|<span style='color:(33, 46, 5, 255)'>kernel</span>|0x10|0x30|0x10|{}|
|<span style='color:(32, 59, 22)'>dtb</span>|0x190|0x30|0x228|{}|
|<span style='color:(65, 30, 40)'>rootfs</span>|0x50|0x30|0x110|{}|
|<span style='color:(63, 33, 16)'>kernel</span>|0x10|0x30|0x10|{}|
Binary file modified doc/example/test_generate_doc_example_normal_redux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions doc/example/test_generate_doc_example_three_maps.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
![memory map diagram](test_generate_doc_example_three_maps_cropped.png)
|name|origin|size|free Space|collisions
|:-|:-|:-|:-|:-|
|<span style='color:(22, 21, 50, 255)'>Blob5</span>|0x78|0x20|0x350|{}|
|<span style='color:(37, 26, 22, 255)'>Blob3</span>|0x50|0x20|0x8|{}|
|<span style='color:(2, 41, 38, 255)'>Blob7</span>|0x50|0x20|0x378|{}|
|<span style='color:(45, 53, 33, 255)'>Blob4</span>|0x28|0x20|0x8|{}|
|<span style='color:(26, 44, 56, 255)'>Blob6</span>|0xa|0x3c|0xa|{}|
|<span style='color:(66, 25, 5, 255)'>Blob1</span>|0x0|0x20|0x3c8|{}|
|<span style='color:(35, 25, 10, 255)'>Blob2</span>|0x0|0x20|0x8|{}|
|<span style='color:(57, 20, 4)'>Blob5</span>|0x78|0x20|0x350|{}|
|<span style='color:(54, 23, 42)'>Blob3</span>|0x50|0x20|0x8|{}|
|<span style='color:(40, 39, 17)'>Blob7</span>|0x50|0x20|0x378|{}|
|<span style='color:(5, 35, 6)'>Blob4</span>|0x28|0x20|0x8|{}|
|<span style='color:(40, 60, 13)'>Blob6</span>|0xa|0x3c|0xa|{}|
|<span style='color:(56, 48, 6)'>Blob1</span>|0x0|0x20|0x3c8|{}|
|<span style='color:(13, 29, 29)'>Blob2</span>|0x0|0x20|0x8|{}|
Binary file modified doc/example/test_generate_doc_example_three_maps_redux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions doc/example/test_generate_doc_example_two_maps.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
![memory map diagram](test_generate_doc_example_two_maps_cropped.png)
|name|origin|size|free Space|collisions
|:-|:-|:-|:-|:-|
|<span style='color:(56, 14, 48, 255)'>Blob4</span>|0x100|0x10|0x2d8|{}|
|<span style='color:(0, 29, 5, 255)'>Blob3</span>|0x50|0x10|0x388|{}|
|<span style='color:(40, 20, 61, 255)'>Blob5</span>|0x30|0x10|0x10|{}|
|<span style='color:(8, 47, 29, 255)'>Blob1</span>|0x10|0x10|0xe0|{}|
|<span style='color:(21, 57, 18, 255)'>Blob2</span>|0x10|0x10|0x10|{}|
|<span style='color:(54, 17, 68)'>Blob4</span>|0x100|0x10|0x2d8|{}|
|<span style='color:(19, 28, 38)'>Blob3</span>|0x50|0x10|0x388|{}|
|<span style='color:(3, 58, 37)'>Blob5</span>|0x30|0x10|0x10|{}|
|<span style='color:(59, 51, 7)'>Blob1</span>|0x10|0x10|0xe0|{}|
|<span style='color:(46, 57, 8)'>Blob2</span>|0x10|0x10|0x10|{}|
Binary file modified doc/example/test_generate_doc_example_two_maps_redux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion mm/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def draw_diagram_img_redux(self):
head_width=25,
tail_len=75,
tail_width=20,
fill="yellow"
fill=Diagram.model.link_fill_colour
)
final_diagram_img = arrow.overlay(final_diagram_img, mm.image.Point(arrow.pos.x, arrow.pos.y), Diagram.model.link_alpha)

Expand Down
26 changes: 13 additions & 13 deletions mm/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import dataclasses



@dataclasses.dataclass
class Point:
x: float
Expand Down Expand Up @@ -93,27 +94,26 @@ def trim(self) -> None:
else:
logging.warning("Error trimming image")

def _pick_random_colour(self) -> Tuple[int, int, int, int]:
def _pick_random_colour(self) -> Tuple[int, int, int]:
"""Pick random RGBA colour band values"""
min_band = int("00", 16)
max_band = int("44", 16)
r =random.randint(min_band, max_band)
g =random.randint(min_band, max_band)
b =random.randint(min_band, max_band)
a =255
return (r, g, b, a)
return (r, g, b)

@typeguard.typechecked
class MapNameImage(Image):
"""Wrapper class for PIL.Image.Image object. Represents a MemoryMap sub diagram."""

def __init__(self, name: str, img_width: int, font_size: int, fill_colour:str, line_colour: str):
def __init__(self, name: str, img_width: int, font_size: int, fill_colour: mm.metamodel.ColourType, line_colour: mm.metamodel.ColourType):

super().__init__(name, None)

self._draw(img_width, font_size, fill_colour, line_colour)

def _draw(self, img_width: int, font_size: int, fill_colour:str, line_colour: str) -> None:
def _draw(self, img_width: int, font_size: int, fill_colour: mm.metamodel.ColourType, line_colour: mm.metamodel.ColourType) -> None:
"""Create the image for the region rectangle and its inset name label"""

txt_lbl = TextLabelImage(self.name, text=self.name, font_size=font_size)
Expand Down Expand Up @@ -258,15 +258,15 @@ def _draw(self):
@typeguard.typechecked
class VoidRegionImage(Image):

def __init__(self, mmap_parent: str, img_width: int, font_size: int, fill_colour:str, line_colour: str):
def __init__(self, mmap_parent: str, img_width: int, font_size: int, fill_colour: mm.metamodel.ColourType, line_colour: mm.metamodel.ColourType):
super().__init__("SKIPPED", mmap_parent)

self.size_as_hex: str = hex(40)
self.size_as_int: int = int(self.size_as_hex,16)

self._draw(img_width, font_size, fill_colour, line_colour)

def _draw(self, img_width: int, font_size: int, fill_colour:str, line_colour: str):
def _draw(self, img_width: int, font_size: int, fill_colour: mm.metamodel.ColourType, line_colour: mm.metamodel.ColourType):

logging.info(self)

Expand All @@ -291,8 +291,8 @@ def __init__(self,
parent: str,
text: str,
font_size: int,
font_colour: str = "black",
fill_colour: Tuple[int, int, int, int] | str = "white",
font_colour: mm.metamodel.ColourType = "black",
fill_colour: mm.metamodel.ColourType = "white",
padding_width: int = 0):


Expand Down Expand Up @@ -347,8 +347,8 @@ def __init__(self,
head_width: int = 20,
tail_len: int = 75,
tail_width: int = 50,
line: str = "black",
fill: str = "white",
line: mm.metamodel.ColourType = "black",
fill: mm.metamodel.ColourType = "white",
show_outline: bool = False):
"""
src: coords for start of arrow
Expand Down Expand Up @@ -455,8 +455,8 @@ def __init__(
w: int,
h: int,
dash: Tuple[int, int, int, int],
fill: Tuple[int, int, int, int] | str,
line: str = "black",
fill: mm.metamodel.ColourType,
line: mm.metamodel.ColourType = "black",
stroke: float = 1):
"""dash is 4-tuple of top, right, bottom, left edges, set to 0 or 1 to disable.
Fill is an RGBA tuple or colour string"""
Expand Down
24 changes: 15 additions & 9 deletions mm/metamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import pydantic
import pathlib
import json
from typing import Tuple, Literal
from typing import Tuple, Literal, Union
from typing_extensions import Annotated
import logging
import enum
import decimal

ColourType = Union[str | Tuple[int, int, int]]

class ConfigParent(pydantic.BaseModel):
model_config = pydantic.ConfigDict(
validate_assignment=True,
Expand Down Expand Up @@ -109,24 +111,24 @@ class Diagram(ConfigParent):
pydantic.Field(30, description="The percentage width of the diagram legend")
]
bgcolour: Annotated[
str,
ColourType,
pydantic.Field("white", description="The background colour used for the diagram")
]
void_fill_colour: Annotated[
str,
ColourType,
pydantic.Field("white", description="Fill colour for the void region blocks")
]
void_line_colour: Annotated[
str,
pydantic.Field("grey", description="Line colour for the void region blocks")
ColourType,
pydantic.Field((192,192,192), description="Line colour for the void region blocks")
]
title_fill_colour: Annotated[
str,
pydantic.Field("blanchedalmond", description="Fill colour for the memory region title blocks")
ColourType,
pydantic.Field((224,224,224), description="Fill colour for the memory region title blocks")
]
title_line_colour: Annotated[
str,
pydantic.Field("grey", description="Line colour for the emory region title blocks")
ColourType,
pydantic.Field((32,32,32), description="Line colour for the emory region title blocks")
]
memory_maps: Annotated[
dict[str, MemoryMap],
Expand All @@ -144,6 +146,10 @@ class Diagram(ConfigParent):
int,
pydantic.Field(96, description="Transparency value for all link arrow images.", gt=-1, lt=256)
]
link_fill_colour: Annotated[
ColourType,
pydantic.Field((224,224,224), description="Line colour for the emory region title blocks")
]

@pydantic.field_validator("name")
@classmethod
Expand Down
Loading

0 comments on commit 0c83fa6

Please sign in to comment.