From: George Foot Newsgroups: comp.os.msdos.djgpp Subject: Re: Colors Date: 11 Feb 1998 20:58:21 GMT Organization: Oxford University, England Lines: 43 Message-ID: <6bt3dd$42g$4@news.ox.ac.uk> References: <887209728 DOT 858627 AT nn1> NNTP-Posting-Host: sable.ox.ac.uk Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Wed, 11 Feb 1998 16:11:15 +0100 in comp.os.msdos.djgpp Anders Clerwall wrote: : Hi,, I have tried to convert rgb values to a color value in 800x600 (64k : cols) using the formula: (r << 11) | (g << 5) | b; what's wrong?!.. when I : put in for example the rgb values 60,60, 60.. the pixel on the screen is : blue!.. it should be white, no? I have never done anything in high colour depths, but your formula implies that R is a 5-bit number, G is a 6-bit number and B is a 5-bit number. So, R ranges from 0 to 31, G from 0 to 63 and B from 0 to 31 also. Note: (1) G has a different range to R and B so you'll need to scale its values accordingly, and (2) the ranges aren't from 0 to 63 as for a 6-bit DAC in 256 colour mode. So, make sure your R, G and B values are clipped to the correct ranges. You might like to express them all as 8-bit quantities then scale them down. /* untested code -- for illustration only */ #define MAKE_IN_RANGE(x,l,h) x = (x < (l)) ? (l) : ((x > (h)) ? (h) : x) int get_colour (int r, int g, int b) { MAKE_IN_RANGE (r, 0, 255); MAKE_IN_RANGE (g, 0, 255); MAKE_IN_RANGE (b, 0, 255); r >>= 3; g >>= 2; b >>= 3; return (r << 11) + (g << 5) + b; } Then you can call `get_colour' with r, g and b strengths all ranging from 0 to 255 (automatically restricted to that range) to get your colour. I believe this is something like what Allegro does. Using ranges from 0 to 255 makes it simple to write 15-bit and 24/32-bit equivalent functions. -- george DOT foot AT merton DOT oxford DOT ac DOT uk ko tavla fo la lojban