Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ColorLike type alias #856

Open
njourdane opened this issue Jan 10, 2025 · 1 comment
Open

Add ColorLike type alias #856

njourdane opened this issue Jan 10, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@njourdane
Copy link
Contributor

Creating a type alias like this:

from typing import TypeAlias
ColorLike: TypeAlias = (
    _.Color | # build123d color
    _.Quantity_ColorRGBA | # OCP color
    str | # name, ex: "red"
    tuple[str, int] | # name + alpha, ex: ("red", 0.5)
    tuple[float, float, float] | # rvb, ex: (1, 0, 0)
    tuple[float, float, float, int] | # rvb + alpha, ex: (1, 0, 0, 0.5)
    int | # hexa, ex: 0xff0000
    tuple[int, int] # hexa + alpha, ex: (0xff0000, 0x80)
)

and a convertion method like this:

class Color:
    @classmethod
    def from_color_like(color_like: ColorLike) -> Color: ...

and replacing Color method parameters by a ColorLike might be practical.

It will allow for instance this syntax:

obj = Solid(obj=box.wrapped, color="red")

Instead of explitly instantiating the Color object:

obj = Solid(obj=box.wrapped, color=Color("red"))

I can create a PR if you agree.

@gumyr
Copy link
Owner

gumyr commented Jan 11, 2025

Yes, a PR would be welcome, thanks for this - it reminded me to change over VectorLike and RotationLike to TypeAlias. The constructor of Vector and Rotation take VectorLike and RotationLike respectively so the constructor of Color should take ColorLike.

I need to spend some time with the docs so the APIs just show these "like" types and not the exploded values.

@gumyr gumyr added the enhancement New feature or request label Jan 14, 2025
@gumyr gumyr added this to the Not Gating Release 1.0.0 milestone Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants