chexdata-conversiongdk

How do I get a hexadecimal string representation of a GdkRGBA struct in C?


I have a program that reads a hexadecimal string representation from a configuration file, turns it into a GdkRGBA struct from Gdk, uses gtk_color_dialog_button_set_rgba () to set it as the default colour of a GtkColorDialogButton, and then listens to user changes to the aforementioned button.

On user change, I want to write back the chosen value to the configuration file so that it is persistent across program launches, and I need it to be a hexadecimal string representation for legacy compatibility reasons (the configuration file is ~/.Xresources, so it needs to be compatible with many existing programs). However, while Gdk provides a gdk_rgba_parse () function to parse a hexadecimal string representation of a colour into a GdkRGBA struct, I can't find anything to do the inverse, i.e., turn a GdkRGBA struct into a hexadecimal string representation. There is gdk_rgba_to_string (), but it only returns strings of the form rgb(r,g,b) or rgba(r,g,b,a), whereas I want #rrggbb.

How can I get the hexadecimal string representation of a GdkRGBA struct's value in C?


Solution

  • There is gdk_rgba_to_string(), but it only returns strings of the form rgb(r,g,b) or rgba(r,g,b,a), whereas I want #rrggbb.

    So parse the string returned by gdk_rgba_to_string() and format the result as you need. There are many ways you could implement that. For example,

        char *rgba_str = gdk_rgba_to_string(rgba);
    
        unsigned int r, g, b;
        // rgba_str has the form "rgb(r,g,b)" or "rgba(r,g,b,a)", where r, g, b, a are
        // decimal integers in the range 0 - 255.
        int fields = sscanf(rgba_str, "%*[^(](%u,%u,%u", &r, &g, &b);
    
        // the caller of gdk_rgba_to_string() is responsible for freeing the result
        free(rgba_str);
    
        if (fields < 3) {
            // handle malformed rgba_str ...
        }
    
        // if paranoid then validate that r, g, and b are within the expected range ...
    
        char result[8];
    
        sprintf(result, "#%02x%02x%02x", r, g, b);
    

    Although [s]scanf can be problematic for interactive input, it will do very nicely for input as regularly formatted as you can rely on gdk_rgba_to_string() to produce.

    Alternatively, type GdkRGBA is documented, and it is not opaque to your code. Instead of going through gdk_rgba_to_string()'s string representation, you could read the structure members directly, convert from float to properly-ranged integers, and format the results as above. For example,

        unsigned int r, g, b;
    
        r = (unsigned int) (0.5 + rgba->red * 255);
        g = (unsigned int) (0.5 + rgba->green * 255);
        b = (unsigned int) (0.5 + rgba->blue * 255);
    
        char result[8];
    
        sprintf(result, "#%02x%02x%02x", r, g, b);
    

    Those conversions from float are based on the GdkRGBA documentation that the color values are in the range 0.0 - 1.0, inclusive, and for such values they match the conversions performed by Gdk itself (thanks, @IanAbbott). You could make it a bit safer by clamping out of range values into that range.

    I note that this conversion has the unfortunate property that the ranges of values that map to the two endpoints are each half the size of the ranges that map to other integer values, but you're kind of stuck with the way Gdk does things. That shouldn't matter just for going back and forth between integer and floating point, but it has some minor effects relative to assigning same-size ranges to all color values for colors that are been manipulated in the floating-point space.