Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

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

+2
−0

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+1
−0

I ended up implementing it like this:

#define RGB_HEX_STRING_LENGTH 8

gchar *
gdkrgba_to_rgb_hex_string (const GdkRGBA *const from)
{
  guchar red = roundf (from->red * 255.0f);
  guchar green = roundf (from->green * 255.0f);
  guchar blue = roundf (from->blue * 255.0f);
  gchar *to = malloc (RGB_HEX_STRING_LENGTH);

  snprintf (to, RGB_HEX_STRING_LENGTH, "#%02hhx%02hhx%02hhx", red, green, blue);

  return to;
}

This works since the properties of the GdkRGBA struct are public, so we can access them directly.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »