Sunday, April 9, 2017

C Program to define palette of colors

Graphics in C - Program to Define Palette of Colors


Now something about colors and color palette in graphics mode. Please  type this program in your editor.

#include<graphics.h>
#include<stdlib.h>
#include<time.h>
void main()
{
        int gd = DETECT, gm, maxcolor;
        int maxx, maxy, i, j, y, height;
        struct palettetype p;
        initgraph(&gd, &gm, "c:\\tc\\bgi");
        maxx = getmaxx();
        maxy = getmaxy();
        y = 10;
        getpalette(&p);
        maxcolor = getmaxcolor();
        height =  maxy/maxcolor - 5;
        randomize();
        for(j = 1; j <= maxcolor; j++)
        {
                for(i = 0; i <= maxcolor; i++)
                {
                       setfillstyle(SOLID_FILL, i);
                       bar(maxx/2 - 50, y, maxx/2 + 50, y + height);
                       y += height;
                }
                outtextxy(maxx/2 - 50, y + 10, "Press any key...");
                y = 10;
                getch();
                setpalette(j, random(maxcolor + 1));
        }
        setallpalette(&p);
        getch();
        closegraph();
        restorecrtmode();
}

Compile and execute this program. The output would be a series of colored rectangles stacked on each other forming a color palette. The number of rectangles would depend on your computer’s graphic’s driver, the color palette’s size and color. At the end, a message will prompt you to press a key. On every key press you will see change in one color of palette.
On my computer, 16 rectangles are stacked on each other. First rectangle, which is of black color, is not visible due to black background of screen.
In the program, first thing I have done is to declare a structure palettetype variable ‘p’. This variable ‘p’ has been used to save the computer’s original color palette. Structure ‘palettetype’ contains the palette information for the current graphics driver.

Declaration:

struct palettetype
{
          unsigned char size;
          signed char colors[maxcolor+1];
};

where ‘size’ gives the number of colors in the palette in the current graphics driver and mode. ‘colors’ is an array of size bytes containing the color numbers for each entry. ‘maxcolor’ is the highest color value.

The statement:

struct palettetype p;

would declare a variable ‘p’ of struct palettetype.

The statement:

getpalette(&p);

would save the actual color palette information in variable ‘p’. (I have passed the address of variable ‘p’ in the function getpalette()). I have done so, because the next instructions will change the palette’s original settings. To restore the original settings later, I have saved these in the variable ‘p’.

Function getpalette() gets information about the current palette.

Declaration:

void far getpalette(struct palettetype far *palette);

Function getpalette() fills the palettetype structure *palette with information about current palette’s size and colors.

Syntax for calling this function is:

getpalette(&palette);

The statement:

maxcolor = getmaxcolor();

gets the maximum color value and stores this value in variable ‘maxcolor’. Function getmaxcolor() returns the highest color value of the palette.

Declaration:

int far getmaxcolor(void);

Syntax for calling this function is:

getmaxcolor();

For creating color palette I have taken two nested ‘for’ loops. Outer ‘for’ loop loops through value ‘1’ to ‘maxcolor’. Inner ‘for’ loop loops through value ‘0’ to ‘maxcolor’. In every iteration of outer ‘for’ loop, inner ‘for’ loop creates a color palette. For creating the palette, I have used the functions bar() and setfillstyle(). In every iteration of inner ‘for’ loop function bar() draws a rectangle, filled with color and fill style specified by function setfillstyle().

setfillstyle(SOLID_FILL, i);
bar(maxx/2 - 50, y, maxx/2 + 50, y + height);

First time, the original color palette is shown. In the next iteration of outer ‘for’ loop color palette changes. One color of the palette changes in every iteration of outer ‘for’ loop. After every iteration of outer ‘for’ loop, user is prompted to press any key.

To change one color of palette, I have used the function setpalette().

Declaration:

void far setpalette(int colornum, int color);

where ‘colornum’ is the position of color to be changed in the current color palette, and ‘color’ is the either enumerated value or color constant in the original color palette. Function setpalette() changes the ‘colornum’ entry in the palette to ‘color’.

For example, the statement setpalette(2, 6) will change the third color(counting starts from 0) in the current palette to color number 6(BROWN) as in the original color palette.

Thus the statement:

setpalette(j, random(maxcolor + 1));

will set the  (j+1)th color entry of current pallete to a color randomly selected by the function random()Function random() returns a random number.

Declaration:

int random(int num);

Function random() returns a random number between 0 and (num-1). This function is declared in the header file ‘stdlib.h’, that’s why I have included this file at the start of the program.

Syntax for calling this function is:

random(num);

Thus, the statement:

random(maxcolor + 1);

returns a number between ‘0’ to ‘maxcolor’.  ‘maxcolor’ is the highest color value I have obtained previously in the program. I have added ‘1’ in the function call, because function random() takes the value between ‘0’ and (num-1).

Before using function random(), I have used the randomize(). randomize() is a macro, which initializes the random number generator with a random value. I have included ‘time.h’ header file for this macro.

Declaration:

void randomize(void);

Syntax for calling this macro is:

randomize();

In the last, I have restored the original color palette. Outside the both loops, I have called the function setallpalette() and passed the address of struct palettetype variable ‘p’. The original palette settings have been saved in the struct palettetype variable ‘p’.

setallpalette(&p);

Function setallpalette() changes all palette colors as specified.

Declaration:

void far setallpalette(struct palettetype far *palette);

Function setallpalette() sets the current palette to the values given in the palettetype structure *palette.

Syntax for calling this function is:

setallpalette(&palette);

Thus, the statement:

setallpalette(&p);

sets the whole palette with the values, saved previously in the struct palettetype variable ‘p’.


No comments:

Post a Comment

Please give your comment