Skip to content

Commit

Permalink
Add Jupyter HTML output representation
Browse files Browse the repository at this point in the history
Closes #429
  • Loading branch information
facelessuser committed Aug 11, 2024
1 parent 2d8a88f commit c0e5c54
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions coloraide/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,35 @@ def __repr__(self) -> str:

__str__ = __repr__

def _repr_html_(self) -> str: # pragma: no cover
"""
Return an HTML representation of the color for Jupyter and other aware libraries.
Colors are not gamut mapped, but returned as is.
"""

svg = ''.join(
[
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' fill-opacity='0.1'>",
"<rect width='50' height='50'/>",
"<rect x='50' y='50' width='50' height='50'/>",
"</svg>"
]
)

return ''.join(
[
'<div style="margin: 10px 0; text-align: center;">',
'<div style="box-sizing: border-box; width: 52px; height: 52px; border: 1px solid hsl(0, 0%, 80%);',
f'background: url(&quot;data:image/svg+xml,{svg}&quot;) 0 0 / 0.75em 0.75em #fefefe; margin: 0 auto;">',
'<div style="box-sizing: border-box; width: 50px; height: 50px; '
f'background-color: {self.convert("oklab").to_string()}; border: 2px solid hsl(0, 0%, 90%);"></div>',
'</div>',
f'<div>{self.to_string(fit=False)}</div>'
'</div>'
]
)

def white(self, cspace: str = 'xyz') -> Vector:
"""Get the white point."""

Expand Down
1 change: 1 addition & 0 deletions docs/src/dictionary/en-custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ JSON
Jacobian
Jsh
Judd
Jupyter
Jz
JzCzhz
JzMzhz
Expand Down
1 change: 1 addition & 0 deletions docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 4.0

- **NEW**: Officially support Python 3.13.
- **NEW**: Define HTML output representation for Jupyter via `_repr_html_`.
- **NEW**: Remove deprecated `model` parameter from `cam16` ∆E method. Space should be used instead.
- **NEW**: Remove deprecated `algebra.npow` function. `algebra.spow` should be used instead.
- **BREAK**: Finalize the `raytrace` gamut mapping algorithm implementation and remove experimental label.
Expand Down

0 comments on commit c0e5c54

Please sign in to comment.