GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-May-2009, 12:05
dirleyrls dirleyrls is offline
New Member
 
Join Date: Feb 2009
Posts: 10
dirleyrls will become famous soon enough

How to create a "default" 256 color palette?


Hello, there

I'm trying to make a program in wich I'll need to deal with bitmap files. I'm already familiarized with the bitmap format and I've already planned almost everything.

I've encountered just one problem until now: how to generate a default color palette? I mean, the MS Paint does it for you when you create a new image, leaves it blank and saves it in the 256 color bitmap format; it automatically generates a "default" palette and save it in the file. All I need to do is it.

I have a "palette_entry" structure:

CPP / C++ / C Code:
struct palette_entry {
	unsigned char r;
	unsigned char g;
	unsigned char b;
};

I want a code that creates a default palette, with default colors. Something like:

CPP / C++ / C Code:
struct palette_entry palette[256];

for (i=0; i<256; i++) {
	somecoolcolorvalue = [...]a cool color generator formula[...];
	palette[i].r = somecoolcolorvalue;
	palette[i].g = somecoolcolorvalue;
	palette[i].b = somecoolcolorvalue;
}

I've searched over the web about this 256 color palettes and I've found http://upload.wikimedia.org/wikipedia/commons/9/93/256colour.png that seems pretty beautiful and useful for me, but I don't mind how to generate it. I'll be thank if someone give me a hint, a reference, an article link or something like that.

Well, I apologize for my bad english and, consequently, my bad explanation.

Thank you all,
dirleyrls
  #2  
Old 19-May-2009, 14:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to create a "default" 256 color palette?


Quote:
Originally Posted by dirleyrls
H...how to generate a default color palette?....

I would probably start at the explanation on msdn: The Safety Palette

It shows not only "how" but "why" it is recommended to do things a certain way. Then, for purposes of demonstration, I might do something like the following:

CPP / C++ / C Code:
#include <stdio.h>

typedef struct {
    unsigned char r;
    unsigned char g;
    unsigned char b;
} color_triplet;

int main()
{
    int i;
    int red, green, blue;
    color_triplet table[256] = {{0,0,0}}; /* Initialize to all black */

    const color_triplet black = {0,0,0};
    const color_triplet white = {255,255,255};

    table[0]   = white; /* Windows reserves first color for white */
    table[255] = black; /* Windows reserves last color as black   */

    i = 20; /* first 20 and last 20 are reserved */
    for (red = 0; red <= 255; red+= 51) {/* the six values of red */
        for (green = 0; green <= 255; green += 51) {
            for (blue = 0; blue <= 255; blue+= 51) {
                table[i].r = red;
                table[i].g = green;
                table[i].b = blue;
                ++i;
            }
        }
    }
    /* 
       As far as applications are concerned, it's not really necessary
       to define first and last entries, since windows will ignore them,
       but for program purposes, I like to document them (and for
       non-windows applications, these are the values that I would
       probably select anyhow).
     */
    table[0]   = white; /* Windows reserves first color for white */
    table[255] = black; /* Windows reserves last color as black   */

    printf("Here are \"Safety Palette\" color table hexadecimal rgb entries\n");
    printf("for table values 20 through 235.\n");
    printf("(The first 20 and last 20 are reserved.)\n\n");
    printf("Reference:\n");
    printf("  http://msdn.microsoft.com/en-us/library/bb250466(VS.85).aspx\n\n");
    for (i = 20; i < 236; i++) {
        if ((i % 6) == 2) {
            printf("\n%3d: ", i);
        }
        printf(" %02x%02x%02x", table[i].r,table[i].g,table[i].b);
    }
    printf("\n");
    return 0;
}

Output
Code:
Here are "Safety Palette" color table hexadecimal rgb entries for table values 20 through 235. (The first 20 and last 20 are reserved.) Reference: http://msdn.microsoft.com/en-us/library/bb250466(VS.85).aspx 20: 000000 000033 000066 000099 0000cc 0000ff 26: 003300 003333 003366 003399 0033cc 0033ff 32: 006600 006633 006666 006699 0066cc 0066ff 38: 009900 009933 009966 009999 0099cc 0099ff 44: 00cc00 00cc33 00cc66 00cc99 00cccc 00ccff 50: 00ff00 00ff33 00ff66 00ff99 00ffcc 00ffff 56: 330000 330033 330066 330099 3300cc 3300ff 62: 333300 333333 333366 333399 3333cc 3333ff 68: 336600 336633 336666 336699 3366cc 3366ff 74: 339900 339933 339966 339999 3399cc 3399ff 80: 33cc00 33cc33 33cc66 33cc99 33cccc 33ccff 86: 33ff00 33ff33 33ff66 33ff99 33ffcc 33ffff 92: 660000 660033 660066 660099 6600cc 6600ff 98: 663300 663333 663366 663399 6633cc 6633ff 104: 666600 666633 666666 666699 6666cc 6666ff 110: 669900 669933 669966 669999 6699cc 6699ff 116: 66cc00 66cc33 66cc66 66cc99 66cccc 66ccff 122: 66ff00 66ff33 66ff66 66ff99 66ffcc 66ffff 128: 990000 990033 990066 990099 9900cc 9900ff 134: 993300 993333 993366 993399 9933cc 9933ff 140: 996600 996633 996666 996699 9966cc 9966ff 146: 999900 999933 999966 999999 9999cc 9999ff 152: 99cc00 99cc33 99cc66 99cc99 99cccc 99ccff 158: 99ff00 99ff33 99ff66 99ff99 99ffcc 99ffff 164: cc0000 cc0033 cc0066 cc0099 cc00cc cc00ff 170: cc3300 cc3333 cc3366 cc3399 cc33cc cc33ff 176: cc6600 cc6633 cc6666 cc6699 cc66cc cc66ff 182: cc9900 cc9933 cc9966 cc9999 cc99cc cc99ff 188: cccc00 cccc33 cccc66 cccc99 cccccc ccccff 194: ccff00 ccff33 ccff66 ccff99 ccffcc ccffff 200: ff0000 ff0033 ff0066 ff0099 ff00cc ff00ff 206: ff3300 ff3333 ff3366 ff3399 ff33cc ff33ff 212: ff6600 ff6633 ff6666 ff6699 ff66cc ff66ff 218: ff9900 ff9933 ff9966 ff9999 ff99cc ff99ff 224: ffcc00 ffcc33 ffcc66 ffcc99 ffcccc ffccff 230: ffff00 ffff33 ffff66 ffff99 ffffcc ffffff

Regards,

Dave
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FLTK && fluid In Motion cable_guy_67 FLTK Forum 4 20-Mar-2008 04:52
How do I Create a LookUp Table? toad78 MySQL / PHP Forum 0 21-Aug-2007 23:05
How create another window for MFC? Flavs83 MS Visual C++ / MFC Forum 1 05-Apr-2007 16:04
create array of my class amad1337 Java Forum 4 11-Nov-2006 09:26
Simple FLTK dialog box sample cable_guy_67 FLTK Forum 4 02-Nov-2004 09:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:20.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.