Skip to content

Commit

Permalink
fixing background_color in graphics, and use it to show failures in r…
Browse files Browse the repository at this point in the history
…ender primitives
  • Loading branch information
mmatera committed Jan 19, 2024
1 parent c88cfc0 commit a79690c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
5 changes: 5 additions & 0 deletions mathics/builtin/box/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ def _prepare_elements(self, elements, options, neg_y=False, max_width=None):
if evaluation is None:
evaluation = self.evaluation
elements = GraphicsElements(elements[0], evaluation, neg_y)
if hasattr(elements, "background_color"):
self.background_color = elements.background_color
if hasattr(elements, "tooltip_text"):
self.tooltip_text = elements.tooltip_text

axes = [] # to be filled further down

def calc_dimensions(final_pass=True):
Expand Down
31 changes: 29 additions & 2 deletions mathics/builtin/box/graphics3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
no_doc = True

import json
import logging
import numbers

from mathics.builtin.box.graphics import (
Expand All @@ -15,7 +16,12 @@
PointBox,
PolygonBox,
)
from mathics.builtin.colors.color_directives import Opacity, RGBColor, _ColorObject
from mathics.builtin.colors.color_directives import (
ColorError,
Opacity,
RGBColor,
_ColorObject,
)
from mathics.builtin.drawing.graphics3d import Coords3D, Graphics3DElements, Style3D
from mathics.builtin.drawing.graphics_internals import (
GLOBALS3D,
Expand Down Expand Up @@ -51,7 +57,11 @@ def _prepare_elements(self, elements, options, max_width=None):
):
self.background_color = None
else:
self.background_color = _ColorObject.create(background)
try:
self.background_color = _ColorObject.create(background)
except ColorError:
logging.warning(f"{str(background)} is not a valid color spec.")
self.background_color = None

evaluation = options["evaluation"]

Expand Down Expand Up @@ -227,6 +237,11 @@ def _prepare_elements(self, elements, options, max_width=None):
raise BoxExpressionError

elements = Graphics3DElements(elements[0], evaluation)
# If one of the primitives or directives fails to be
# converted into a box expression, then the background color
# is set to pink, overwritting the options.
if hasattr(elements, "background_color"):
self.background_color = elements.background_color

def calc_dimensions(final_pass=True):
if "System`Automatic" in plot_range:
Expand Down Expand Up @@ -356,6 +371,16 @@ def boxes_to_json(self, elements=None, **options):
boxscale,
) = self._prepare_elements(elements, options)

# TODO: Handle alpha channel
background = (
self.background_color.to_css()[:-1]
if self.background_color is not None
else "rgbcolor(100%,100%,100%)"
)
tooltip_text = (
elements.tooltip_text if hasattr(elements, "tooltip_text") else ""
)

js_ticks_style = [s.to_js() for s in ticks_style]

elements._apply_boxscaling(boxscale)
Expand All @@ -370,6 +395,8 @@ def boxes_to_json(self, elements=None, **options):
json_repr = json.dumps(
{
"elements": format_fn(elements, **options),
"background_color": background,
"tooltip_text": tooltip_text,
"axes": {
"hasaxes": axes,
"ticks": ticks,
Expand Down
10 changes: 6 additions & 4 deletions mathics/builtin/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
DEFAULT_POINT_FACTOR = 0.005


ERROR_BOX_FACE_COLOR = RGBColor(components=[1, 0, 0, 0.25])
ERROR_BOX_FACE_COLOR = RGBColor(components=[1, 0.3, 0.3, 0.25])
ERROR_BOX_EDGE_COLOR = RGBColor(components=[1, 0, 0, 1.0])


Expand Down Expand Up @@ -1213,15 +1213,17 @@ def convert(content, style):
failed.append(head)
continue

if failed:
yield build_error_box2(style)
# raise BoxExpressionError(messages)
# if failed:
# yield build_error_box2(style)
# raise BoxExpressionError(messages)

self.elements = list(convert(content, self.style_class(self)))
if failed:
messages = "\n".join(
[f"{str(h)} is not a valid primitive or directive." for h in failed]
)
self.tooltip_text = messages
self.background_color = ERROR_BOX_FACE_COLOR
logging.warn(messages)

def create_style(self, expr):
Expand Down
6 changes: 4 additions & 2 deletions mathics/format/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,19 @@ def graphics_box(self, elements=None, **options: dict) -> str:
self.boxwidth = options.get("width", self.boxwidth)
self.boxheight = options.get("height", self.boxheight)

tooltip_text = self.tooltip_text if hasattr(self, "tooltip_text") else ""
if self.background_color is not None:
# FIXME: tests don't seem to cover this secton of code.
# Wrap svg_elements in a rectangle
svg_body = f"""
<rect x="{xmin:f}" y="{ymin:f}" width="{self.boxwidth:f}" height="{self.boxheight:f}" style="fill: {self.background_color}"></rect>
<rect
x="{xmin:f}" y="{ymin:f}"
width="{self.boxwidth:f}"
height="{self.boxheight:f}"
style="fill:{self.background_color.to_css()[0]}
style="fill:{self.background_color.to_css()[0]}"><title>{tooltip_text}</title></rect>
{svg_body}
/>"""
"""

if options.get("noheader", False):
return svg_body
Expand Down

0 comments on commit a79690c

Please sign in to comment.