def test_alpha_roundtrip():
    c0 = QuPathColor(1, 2, 3, 255)
    j0 = c0.to_java_rgba()
    c1 = QuPathColor.from_java_rgba(j0)

    assert c0 == c1
    assert c1.alpha == 255 == c0.alpha
def test_rgb_via_java_rgba():
    c0 = QuPathColor(1, 2, 3, 4)
    j0 = c0.to_java_rgba()
    c1 = QuPathColor.from_java_rgb(j0)

    assert c0.red == c1.red
    assert c0.green == c1.green
    assert c0.blue == c1.blue
    assert c0.alpha == 4 and c1.alpha == 255
def test_hexcolor():
    c0 = "#ff00ff"
    qc = QuPathColor.from_hex(c0)
    assert qc.to_rgb() == (255, 0, 255)
    assert c0 == qc.to_hex()

    with pytest.raises(ValueError):
        QuPathColor.from_hex("abc")

    assert qc == QuPathColor.from_any(c0)
def test_from_java_typecheck():
    QuPathColor.from_java_rgb(0)
    QuPathColor.from_java_rgba(0)
    with pytest.raises(TypeError):
        QuPathColor.from_java_rgb(None)
    with pytest.raises(TypeError):
        QuPathColor.from_java_rgba(None)
def test_color_repr():
    c = QuPathColor.from_hex("#123456")
    assert repr_html(c)
    def __init__(self,
                 name: str,
                 color: Optional[ColorType] = None,
                 parent: Optional['QuPathPathClass'] = None,
                 **_kwargs) -> None:
        """create a QuPathPathClass

        The QuPathPathClasses are wrappers around singletons defined by their
        name and their ancestors. Internally there's only one java PathClass
        instance per ancestor chain + name. (The internal unique id is chaining
        names via ':', which is why ':' is an unsupported name character)

        Parameters
        ----------
        name:
            the name of your path class
        color:
            a color (r,g,b) or (r,g,b,a) with 0 < x < 255 or a QuPathColor
            if color is None, a color calculated from the name is used
        parent:
            the parent of the class

        Returns
        -------
        path_class:
            the QuPathPathClass
        """
        # internal: check if a java path class was already provided
        _java_path_class = _kwargs.pop('_java_path_class', None)
        if _java_path_class is not None:
            self.java_object = _java_path_class
            return

        # called by user
        if name is None:
            if parent is None:
                # note:
                #   parent=None for name=None creates the root node on the qupath java side
                #   will have to consider if we expose this here.
                raise NotImplementedError(
                    "creating Root PathClass is currently not supported")
            else:
                raise ValueError(
                    "cannot create derived QuPathPathClass with name=None")
        elif isinstance(name, str):
            if ":" in name or "\n" in name:
                raise ValueError("PathClass names cannot contain ':' or '\n'")
            name = String(name)
        else:
            raise TypeError(f"name requires type 'str' got '{type(name)}'")

        # get the parent class if requested
        java_parent = None
        if parent is not None:
            if not isinstance(parent, QuPathPathClass):
                raise TypeError("parent must be a QuPathPathClass")
            java_parent = parent.java_object

        # set the color if requested
        java_color = None
        if color is not None:
            java_color = QuPathColor.from_any(
                color).to_java_rgba()  # use rgba?

        path_class = PathClassFactory.getDerivedPathClass(
            java_parent, name, java_color)
        self.java_object = path_class
 def color(self, rgb: Optional[ColorType]) -> None:
     """set the path color"""
     if rgb is not None:
         rgb = QuPathColor.from_any(rgb).to_java_rgb()  # maybe use argb?
     self.java_object.setColor(rgb)
 def color(self) -> Optional[QuPathColor]:
     """return the path color"""
     rgb = self.java_object.getColor()
     if rgb is None:
         return None
     return QuPathColor.from_java_rgb(rgb)
def test_incorrect_color():
    # NOTE THIS DOESNT CRASH BY DESIGN CURRENTLY!
    c = QuPathColor(300, 300, 300)
    assert not c.is_valid()
    c = QuPathColor(30, 30, 30)
    assert c.is_valid()
def test_from_any_error():
    with pytest.raises(TypeError):
        # noinspection PyTypeChecker
        QuPathColor.from_any(object())
def test_color_from_color():
    c = QuPathColor(0, 0, 0, 0)
    assert QuPathColor.from_any(c) == c
def test_repr():
    repr(QuPathColor(0, 0, 0, 255))
    repr(QuPathColor(0, 0, 0, 0))
def test_color_mpl_rgba():
    assert QuPathColor(255, 255, 255).to_mpl_rgba() == (1, 1, 1, 1)
def test_color_roundtrip():
    c0 = (64, 64, 64)
    j0 = QuPathColor(*c0).to_java_rgb()
    c1 = QuPathColor.from_java_rgb(j0).to_rgb()
    assert c0 == c1