Re: GdkColor to Hexadecimal conversion



On Wed, 2005-08-17 at 20:21 +1000, Nick Watts wrote:
Easiest way to convert a GdkColor to its hexadecimal equivalent?

If you mean an #RRGGBB string for web use: 

gchar* // please free me when you're done
gdk_color_to_rgb_hex_string(GdkColor* color)
{
        gchar* rgb;
        gfloat r, g, b;

        r=color->red;
        g=color->green;
        b=color->blue;

        r=(r/65535)*255;
        g=(g/65535)*255;
        b=(b/65535)*255;

        rgb = g_strdup_printf("#%02x%02x%02x",(gint) r,(gint) g,(gint) b);

        return rgb;
}

If you meant convert it to a numeric value:

gint
gdk_color_to_integer_color( GdkColor* color )
{
        gdouble r, g, b;
        gint red, gree, blue;
        gint color;

        r=color->red;
        g=color->green;
        b=color->blue;

        red =   (gint) ((r/65535)*255);
        green = (gint) ((g/65535)*255);
        blue =  (gint) ((b/65535)*255);

        color = red | (green << 8) | (blue << 16);

        return color;
}


It may be some loose of accuracy in both functions, on the gfloat ->
gint conversion.

-- 
Iago Rubio




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]