Re: GdkColor to Hexadecimal conversion

2005-08-17 Thread Iago Rubio
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

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GdkColor to Hexadecimal conversion

2005-08-17 Thread David Necas (Yeti)
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?
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list