Skip to content

Add convenience function gdk_rgba_new_from_string()

FeRD (Frank Dana) requested to merge ferdnyc/gtk:rgba-new-from-string into main

Particularly from bindings (like in Python, for example), having to first construct an "empty" Gdk.RGBA, then give it a value by calling its parse() method, feels a bit... off. It feels more natural to create it directly from the string value, all in one go.

So, this MR adds a convenience constructor, Gdk.RGBA.new_from_string(), which takes as its argument any valid spec string that Gdk.RGBA.parse() will accept, and returns either a pointer to an allocated, populated Gdk.RGBA if parsing the string was successful, or NULL otherwise. The newly-heap-allocated Gdk.RGBA is documented as needing to be freed using gdk_rgba_free().

A pair of tests, for both a parseable and unparseable string argument, are added to testsuite/gdk/rgba.c as test_alloc_from_string.

Using the GI bindings, this allows Python code like the following:

color = Gdk.RGBA()
res = color.parse("#ff0000")
if not res:
    return

...to be written more naturally as:

color = Gdk.RGBA.new_from_string("#ff0000")
if color is None:
    return
Edited by FeRD (Frank Dana)

Merge request reports