Answered # graphics

Greywolf333 11

0

I'm writing a project in xlib and have a problem concerning the colors.I use unsigned long type variables for storing the color values.does anybody know how can i take the red green and blue values of each color?

Are you saying that the 24 bits of color (8 for each color component) are stored together in one 32 bit integer? If that's the case you can obtain the values using the logical AND operation to zero the other bits.

Let's say you start with

```
/*
alpha? r g b
00000000 10101010 10101010 10101010 your 32 bit integer might look like this
& logical AND operator
00000000 00000000 00000000 11111111 a bit mask
=
00000000 00000000 00000000 10101010 the result
so now your 32 bit integer only has the blue values.
To do this in codez...
*/
unsigned char B = (unsigned char) (your_integer & 0x000000ff) //000000ff is hex version of the bit mask
//but now what about the other two colors? you can't just apply a bit mask like 0000ff00 because 00000000000000001010101000000000 is much larger than 255.
//So you have to either divide the result by 256 to shift the bits right, or use >>8 to shift them to the right.
unsigned char G = (unsigned char) ((your_integer & 0x0000ff00) / 256)
unsigned char R = (unsigned char) ((your_integer & 0x00ff0000) / 256^2)
//or using the way I've used in the past... shifting before the mask.
unsigned char G = (unsigned char) ((your_integer >> 8) & 0x000000ff)
unsigned char R = (unsigned char) ((your_integer >> 16) & 0x000000ff)
```

This question has already been answered. Start a new discussion instead.

Recommended Articles

I'm trying to figure out how do i print a distinct enum value by using a conditional statement in my construction of array.

I have a enum declaration

`enum Animal {Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig};`

And i construct my array using a voidptr ...

I need to comput and display the sum and average of the cubes of the even natural numbers between 2 and n. Where n is entered by the user. Im stuck on how to average the sum of the cubes. Help Please. This is what I have so far.

`def ...`

I'm new to python, for my project I need to send mail from python.. I'm getting the following error can someone please help me how to get out of it..

```
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for ...
```