Re: Canvas, Ellipses, and RGBA




> > > GnomeColorPicker, but thats gives me _4_ values in various forms, i.e. 
> > > double, guint8, guint16, etc?) anyway, how do I combine values that I 
> > > gain from the GnomeColorPicker into /ONE/ guint32 for one guint32 RGBA 
> > > value. Havoc's book is not very helpful with this, since he directly 
> > > writes the RGBA values - Also, it appears to be a 'hex' value (i.e. it's 
> > > something like 0x32ab9FFF which I find weird for a guint32). Help!
> > 
> > which means it is : 
> > 0x32 ab 9F FF
> >   R  G  B  A
> > 
> > ex : 0x00FF0080
> > this a green which is semi-transparent
> > 
> > moving your original values to this format is straightforward:
> > 
> > use the << and >> operators
> 
> This is 'still' not really clear - Okay say I have a variable 'i' do I do
> i = 0;
> and then use the << and >> operators, or do I just do it without any 
> initialization?
> When do I use << and when do I use >>? What order must they be in?
> 
> Secondly - the RGBA values you gave are confusin - how do I get them from 
> a GnomeColorPicker - do I take the 'gdouble' values, do I take the 
> 'guint8' or the 'guint16'?
> 
> Can you give me an example? Like 
> guint8 R,G,B,A;
> guint32 fill_color = 0;
> 
> fill_color = fill_color << R << G << B << A;
> 
> Will the above work correctly? If not what should I do?

Eeek, no no.  Go read your K&R book dude.  << is a shift operator.

You need to think in bits/bytes and 32 bit ints here.

A 32 bit int is (obviously) a 4 byte number.  Since RGBA is also 4 bytes
it fits neatly into a 32 bit number.  This means that in bit terms
you have:

   RRRRRRRR GGGGGGGG BBBBBBBB AAAAAAAA
   31    24 23    16 15     8 7      0

With the bit numbers shown below.  You can use the << operator to align
the bits.

All you do is store each byte in the corresponding bit range - there
are a dozen ways to do this, but a simple way is:

 fill_colour = R << 24 |
	G << 16 |
	B << 8 |
	A;

Thats it.  That will work portably across different endianness
architectures, as the canvas will take care of such details.

You could also store it as a byte array, but this is then not portable
across different endianness,
 e.g. on big endian architectures:

  ((unsigned char *)(&fill_colour))[0] = R;
  ((unsigned char *)(&fill_colour))[1] = G;
  ((unsigned char *)(&fill_colour))[2] = B;
  ((unsigned char *)(&fill_colour))[3] = A;

 or little endian:

  ((unsigned char *)(&fill_colour))[3] = R;
  ((unsigned char *)(&fill_colour))[2] = G;
  ((unsigned char *)(&fill_colour))[1] = B;
  ((unsigned char *)(&fill_colour))[0] = A;

So basically dont even bother trying this - use shifts.  I explain
it here only because thats basically what you are doing and how
it gets used.  It is taken as a single uint32, but in reality treated
as a byte quad.

As for which values you want from the colour picker - well you need
8 bit values (0-255), so use the 8 bit values (guint8).  (there isn't
room in a 32 bit value for 4 16 bit values, or 4 floats).

 Michael



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