Color Conversion

P. Goodman--Koyaanisqatsi ptgrunner at earthlink.net
Tue Feb 8 00:55:22 EST 2005


green_man wrote:

> Do any of you web masters know of a simple tool for converting color 
> values from rgb to hex, and vice versa ?
> Some program where I could just plug in a color value like 0,128,128 
> and it would tell me that it's [this is a guess] 008888 in Hex.
>

hi greenman,

Old habits die hard. I typically rarely looked for tools to do something 
simple or not-so-simple--I usually coded the solution.  The following c 
program will do what you want and is easily extensible for ease of use. 
I'm embarrassed that it took me more than 30 sec. to write, but it's 
been a long time since I've bothered to program _anything_. As an aside, 
SGI's GL (evolved to OpenGL) once  forced one to specify r(ed), g(reen), 
b(lue), a(lpha) as abgr, 8 bits per color component vs. the more logical 
order of rgba.

Anyway, the program should give you some insight into how it can be 
done. BTW, given the executable name "to_hex", the command "to_hex 0 128 
128" would yield "008080". Your "guess" wasn't too bad.

pete

*********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
  int red, green, blue;
  int hex_value;

  /*no input error checking*/

  red=atoi(argv[1]);  /*should check for proper range here--[0,255]*/
  green=atoi(argv[2]);
  blue=atoi(argv[3]);

  hex_value=0;

  hex_value = (red<<16);
  hex_value += (green<<8);
  hex_value += blue;

  printf("hex_rgb= %06x\n",
    hex_value);

  return 0;
}
***************************************************************************

 ****************************************************************
 * "Reality is that which, when one stops believing in it, does *
 *  not go away."--????                                         *
 ****************************************************************




More information about the nflug mailing list