Skip to content

Commit

Permalink
add DXFGraphic.rgb deleter #1116
Browse files Browse the repository at this point in the history
- implement DXFGraphic.rgb deleter method.
- add tests for DXFGraphic.rgb
- update docs and changelog
  • Loading branch information
mozman committed Jun 27, 2024
1 parent 4c6ad92 commit 10617b6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
9 changes: 5 additions & 4 deletions docs/source/dxfentities/dxfgfx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ Subclass of :class:`ezdxf.entities.DXFEntity`

.. attribute:: rgb

Get/set DXF attribute :attr:`dxf.true_color` as (r, g, b) tuple, returns
Get/set/delete DXF attribute :attr:`dxf.true_color` as (r, g, b) tuple, returns
``None`` if attribute :attr:`dxf.true_color` is not set.

.. code-block:: python
entity.rgb = (30, 40, 50)
r, g, b = entity.rgb
entity.rgb = (30, 40, 50) # set as tuple[int, int, int] or color.RGB
r, g, b = entity.rgb # returns tuple[int, int, int] or None
del entity.rgb # discard true color value, no exception if not exist
This is the recommend method to get/set RGB values, when ever possible
This is the recommend method to get/set/delete RGB values, when ever possible
do not use the DXF low level attribute :attr:`dxf.true_color`.


Expand Down
2 changes: 2 additions & 0 deletions notes/pages/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
- ((65ed4f6c-edc8-4390-880c-c604a3fa5ec0))
- NEW: `recover` module can load DXF R12 files with subclass markers in table entries
- {{discussion 1106}}
- NEW: `DXFGraphic.rgb` deleter
- {{pr 1113}}
- BUGFIX: `numpy` v2.0 adaptation, `str()` function returns `"np.float64(...)"` for numpy floats
- This affects the following add-on modules:
- `ezdxf.addons.dxf2code`
Expand Down
9 changes: 7 additions & 2 deletions src/ezdxf/entities/dxfgfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def post_new_hook(self) -> None:
)

@property
def rgb(self) -> Optional[clr.RGB]:
def rgb(self) -> Optional[clr.RGB | tuple[int, int, int]]:
"""Returns RGB true color as (r, g, b) tuple or None if true_color is
not set.
"""
Expand All @@ -237,10 +237,15 @@ def rgb(self) -> Optional[clr.RGB]:
return None

@rgb.setter
def rgb(self, rgb: clr.RGB) -> None:
def rgb(self, rgb: clr.RGB | tuple[int, int, int]) -> None:
"""Set RGB true color as (r, g , b) tuple e.g. (12, 34, 56)."""
self.dxf.set("true_color", clr.rgb2int(rgb))

@rgb.deleter
def rgb(self) -> None:
"""Delete RGB true color value."""
self.dxf.discard("true_color")

@property
def transparency(self) -> float:
"""Get transparency as float value between 0 and 1, 0 is opaque and 1
Expand Down
3 changes: 2 additions & 1 deletion src/ezdxf/render/abstract_mtext_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Sequence, Optional
import abc
from ezdxf.lldxf import const
from ezdxf import colors
from ezdxf.entities.mtext import MText, MTextColumns
from ezdxf.enums import (
MTextParagraphAlignment,
Expand Down Expand Up @@ -198,7 +199,7 @@ def make_mtext_context(self, mtext: MText) -> MTextContext:
ctx.aci = mtext.dxf.color
rgb = mtext.rgb
if rgb is not None:
ctx.rgb = rgb
ctx.rgb = colors.RGB(*rgb)
return ctx

def get_font(self, ctx: MTextContext) -> fonts.AbstractFont:
Expand Down
38 changes: 35 additions & 3 deletions tests/test_01_dxf_entities/test_112a_dxfgfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ezdxf.lldxf.tags import Tags, DXFTag
from ezdxf.entities.dxfns import recover_graphic_attributes
from ezdxf.lldxf.const import DXFValueError
from ezdxf import colors


@pytest.fixture
Expand Down Expand Up @@ -41,9 +42,7 @@ def test_color_name(entity):


def test_transparency(entity):
entity.dxf.transparency = (
0x020000FF # 0xFF = opaque; 0x00 = 100% transparent
)
entity.dxf.transparency = 0x020000FF # 0xFF = opaque; 0x00 = 100% transparent
assert 0x020000FF == entity.dxf.transparency
# recommend usage: helper property ModernGraphicEntity.transparency
assert 0.0 == entity.transparency # 0. = opaque; 1. = 100% transparent
Expand Down Expand Up @@ -196,6 +195,39 @@ def test_recover_acdb_entity_tags_ignores_unknown_tags():
assert unprocessed_tags[0] == (99, "Unknown")


class TestRrbAttribute:
def test_set_rgb_from_tuple(self, entity: DXFGraphic):
entity.rgb = (1, 2, 3)
assert entity.dxf.true_color == 66051

def test_set_rgb_from_rgb(self, entity: DXFGraphic):
entity.rgb = colors.RGB(1, 2, 3)
assert entity.rgb == (1, 2, 3)

def test_setting_none_raises_type_error(self, entity: DXFGraphic):
with pytest.raises(TypeError):
entity.rgb = None

def test_get_rgb(self, entity: DXFGraphic):
entity.rgb = (1, 2, 3)
assert entity.rgb == (1, 2, 3)

def test_get_rgb_unset_true_color(self, entity: DXFGraphic):
assert entity.rgb is None

def test_delete_rgb(self, entity: DXFGraphic):
entity.rgb = (1, 2, 3)
del entity.rgb
assert entity.dxf.hasattr("true_color") is False

def test_deleting_unset_value_does_not_raise_exception(self, entity: DXFGraphic):
del entity.rgb
assert entity.dxf.hasattr("true_color") is False


if __name__ == "__main__":
pytest.main([__file__])

LINE = """0
LINE
5
Expand Down

0 comments on commit 10617b6

Please sign in to comment.