Re: how to get the color of a pixel(x,y) on the window?



Am Donnerstag, den 04.06.2009, 10:12 +0200 schrieb Mark Roberts:
> Dear Chunlong84,
> 
> > I use gtkmm on Windows platform,and have tried three method ,but the
> > three methods return three different result,and none of them is
> > correct,wh?
> 
> > (1)
> >  gushort red=(pixel& 0xFF000000)>>16;
> >  gushort green=(pixel& 0x00FF0000)>>8;
> >  gushort blue =(pixel& 0x0000FF00);

I don't think it is valid to interpret a Gdk::Image "pixel" directly as
RGB triplet.  It can be RGB, but it could also be a palette index
instead.  The representation depends on the X visual.

> > (2)
> >  gushort red=p[0]<<8;
> >  gushort green=p[1]<<8;
> >  gushort blue=p[2]<<8;

This should be

gushort red   = p[0] * 0x0101u;
gushort green = p[1] * 0x0101u;
gushort blue  = p[2] * 0x0101u;

(Multiplying by 0x0101 is like shifting the value 8 bits to the left and
also assigning the original value to the lower 8 bits.)

The reason to do it that way is to make sure that white (0xFF) stays
white (0xFFFF) and doesn't turn into a very bright shade of gray
(0xFF00).

Mathematically, it would be

    c16 = (c8 / 255.0) * 65535.0;

> I don't know the first thing about images in GTK. But it seems strange to 
> me that your values for red, green and blue are always multiples 
> of 0x100 and range from 0x0 to 0xffff. Should they not range from 0x0 to 
> 0xff?
> 
> My question to the list is: is this correct, and why?

As Long Chun (correct me if I got the name wrong) already said, the
color components are 16 bit wide in the GDK API.

--Daniel




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