Add Color.get_named_colors()#67752
Conversation
|
It seems to me that there should be a method in |
This comment was marked as outdated.
This comment was marked as outdated.
|
If |
mhilbrunner
left a comment
There was a problem hiding this comment.
PR review meeting: we'd like to gauge demand for this feature if its wanted, and the implementation isn't optimal (re-creating the dictionary on each call), but it may be hard to improve with current GDScript constraints, especially considering this dict may change at runtime due to e.g. GDExtension.
|
Didn't see this PR or #64830 until it was merged and one of my projects broke. I was using the named color functions to get random colors, as just picking purely random RGB or HSV values tends to give unappealing results.
That would be fine IMO if you just mention in the docs description that it can be slow, and suggest that users store the dictionary in a variable if they're going to reuse it. |
|
Oh, the problem is not actually that it is slow, it's that it's a new Dictionary every time. In the PR meeting it was discussed that the Dictionary should be initialised once when the function is called, then every call should return a reference to the same Dictionary. To have that, however, there are some complications. Namely, the dictionary would have to be read-only, while still supporting GDExtension(?). |
You can limit the range of possible component values and optionally round them to the grid. Detailsfunc random_color_rgb() -> Color:
return Color(randf(), randf(), randf())
func random_color_hsv() -> Color:
return Color.from_hsv(randf(), randf(), randf())
func my_random_color() -> Color:
return Color.from_hsv(
snapped(randf(), 1.0 / 12.0),
snapped(randf_range(0.6, 0.8), 0.1),
snapped(randf_range(0.8, 1.0), 0.1),
) |
|
I was thinking of just using something like |
|
Note that after all this time, typed dictionaries have appeared (method should return But I still wonder if we should introduce an API for getting member information for built-in types (since they are not registered in |
Lack of general interest means this won't be rebased, at least for now.
Most definitely. Sounds like something that these would cover, even if indirectly: |

See also #64830.
This PR implements the static method
Color.get_named_colors(). It returns a Dictionary, where each key is a name corresponds to a Color from the exposed engine API.The idea is that using the Dictionary over
find_named_color(),get_named_color_count(),get_named_color_name(),get_named_coloris favourable.The advantage is that it is much more accessible, because unlike them, no integer index is required or returned. It also opens up the door for more interesting manipulation. It needs to be said, however, that not everything can be replicated. Namely,
find_named_color()does some string manipulation to be case-insensitive and support spaces (theColor(String)constructor does the same thing)...However,
find_named_color()also returns a numerical index! AndColor.from_string()is the better equivalent, just returning the Color directly! Bah... Look at #64830.If an user, for some reason, needs an integer index, it's worth reminding that Dictionaries remember the insertion order. Therefore: