Re: GdkColor to Hexadecimal conversion



On Wed, Aug 17, 2005 at 12:40:13PM +0200, Iago Rubio wrote:
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;
}

Isn't that a complicated way to calculate a biased result?
I mean: To get the same rounding, you could just use

    rgb = g_strdup_printf("#%02x%02x%02x",
                          color->red*255/65535,
                          color->green*255/65535,
                          color->blue*255/65535);

and don't bother with floats at all.

But such a rounding seems biased.  There are 257 values that
give 0, another 257 give 1, etc., but only one (65535) gives
255.  Even simple

    rgb = g_strdup_printf("#%02x%02x%02x",
                          color->red >> 8,
                          color->green >> 8,
                          color->blue >> 8);

maps the same number of values (256) to each of the numbers
0..255.  Or is there any specific reason for rounding to zero?
I can imagine reasons for discrimination of both border
values (0 and 255), but why just 255?

Yeti


--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



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