Convert from GdkColor to GdkRGBA

Hi, ALL,
According to documentation for GtkTextTag–foreground-gdk, the GdkColor tag is deprecated and I need to use the GdkRGBA one.

So is there a conversion function that converts one to another? Or I will have to rewrite all interfaces receiving the former?

Thank you.

A GdkRGBA expresses the color and alpha channels as fractional [0, 1] intervals, instead of integer [0, 65535] ones; to convert from GdkColor to GdkRGBA, you can use this simple snippet:

GdkRGBA color_rgba = {
  .red   = CLAMP ((double) color->red / 65535.0,   0.0, 1.0),
  .green = CLAMP ((double) color->green / 65535.0, 0.0, 1.0),
  .blue  = CLAMP ((double) color->blue / 65535.0,  0.0, 1.0),
  .alpha = 1.0,

You’re strongly encouraged to migrate all your code related to colors to GdkRGBA, but of course API taking a GdkColor will keep existing in GTK 3.x. It’s only going to be an issue if you’re going to port your code to GTK 4.

Hi, Emmanuelle,
I presume the color variable is of the type GdkColor *?

Is there a plan to put it inside the library to simplify the upgrades?

Thank you.

Yes, “color” is a GdkColor.

No, there’s no plan to add API, as GTK 3 is feature frozen. Plus, the conversion should be a one off.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.