www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/11/21:15:26

From: George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
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
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Wed, 11 Feb 1998 16:11:15 +0100 in comp.os.msdos.djgpp Anders
Clerwall <anders DOT clerwall AT swipnet DOT se> 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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019