gdk: Add gdk_rgba_print

This commit is contained in:
Matthias Clasen 2024-05-31 20:08:42 -04:00
parent c3083572fb
commit 3a4c69f370
2 changed files with 16 additions and 2 deletions

View File

@ -396,10 +396,18 @@ gboolean
*/
char *
gdk_rgba_to_string (const GdkRGBA *rgba)
{
return g_string_free (gdk_rgba_print (rgba, g_string_new ("")), FALSE);
}
GString *
gdk_rgba_print (const GdkRGBA *rgba,
GString *string)
{
if (rgba->alpha > 0.999)
{
return g_strdup_printf ("rgb(%d,%d,%d)",
g_string_append_printf (string,
"rgb(%d,%d,%d)",
(int)(0.5 + CLAMP (rgba->red, 0., 1.) * 255.),
(int)(0.5 + CLAMP (rgba->green, 0., 1.) * 255.),
(int)(0.5 + CLAMP (rgba->blue, 0., 1.) * 255.));
@ -410,12 +418,15 @@ gdk_rgba_to_string (const GdkRGBA *rgba)
g_ascii_formatd (alpha, G_ASCII_DTOSTR_BUF_SIZE, "%g", CLAMP (rgba->alpha, 0, 1));
return g_strdup_printf ("rgba(%d,%d,%d,%s)",
g_string_append_printf (string,
"rgba(%d,%d,%d,%s)",
(int)(0.5 + CLAMP (rgba->red, 0., 1.) * 255.),
(int)(0.5 + CLAMP (rgba->green, 0., 1.) * 255.),
(int)(0.5 + CLAMP (rgba->blue, 0., 1.) * 255.),
alpha);
}
return string;
}
static gboolean

View File

@ -72,5 +72,8 @@ _gdk_rgba_equal (gconstpointer p1,
rgba1->alpha == rgba2->alpha;
}
GString * gdk_rgba_print (const GdkRGBA *rgba,
GString *string);
G_END_DECLS