This blog is under construction

Saturday 14 September 2013

setpalette example in c

Header file:
    graphics.h

Synopsis:
      void setpalette(int colornum, int color);
     
Description:
     setpalette() changes the color of palette.  For example, setpalette(1, 5) changes the color number 1 in the current palette to color number 5.


setpalette function in c graphics



  #include <graphics.h>
  #include <stdlib.h>
  #include <stdio.h>
  #include <conio.h>
  #include <dos.h>

  int main(void) {
        /* request auto detection */
        int graphicDriver = DETECT, graphicMode;
        int color, err, x1, y1, x2, y2;
        struct palettetype pal;

        /* initialize graphics and local variables */
        initgraph(&graphicDriver, &graphicMode,
                                "C:/TURBOC3/BGI");

        /* read result of initialization */
        err = graphresult();

        if (err != grOk) {
                /* an error occurred */
                printf("Graphics Error: %s\n",
                                grapherrormsg(err));
                getch();
                return 0;
        }

        /* grab a copy of the palette */
        getpalette(&pal);
        x1 = 0, y1 = 0;
        x2 = 200, y2 = 30;

        /* display the default palette colors */
        for (color=1; color<=getmaxcolor(); color++) {
                /*
                 * draw a rectangle using current draw color
                 * fill it using current fill color
                 */
                setcolor(color);
                setfillstyle(SOLID_FILL, color);
                rectangle(x1, y1, x2, y2);
                floodfill(x1 + 1, y1 + 1, color);
                y1 = y1 + 30;
                y2 = y2 + 30;

                /* sleep for 400 milliseconds */
                delay(400);
        }

        /* black out the colors one by one */
        for (color=1; color<=getmaxcolor(); color = color + 2) {
                setpalette(color, BLACK);
                delay(300);
        }

        /* revert back the colors */
        for (color= 2; color<=getmaxcolor(); color = color + 2) {
                setpalette(color, 15);
                delay(300);
        }

        /* restore the palette colors */
        setallpalette(&pal);

        /* clean up */
        getch();
        closegraph();
        return 0;
  }





Output: (setpalette built-in function in c graphics.h)


No comments:

Post a Comment