Software & Finance





Turbo C Graphics - setallpalette function





getdefaultpalette function is used to get the number of colors in the pallete and the color information filled in a struct palettetype. getpalettesize is used to get the number of palletes in the current graphics driver and getpalettte is used to retrieve the current palette information filled in a struct palettetype.

 

setallpalette function is used to make changes to all the palette colors as specified in the struct palettetype. setpalette function is used to make changes to only one color. It will accept the current color and the destination color.

 

In this sample code, all the colors and their palette information will be displayed on their own palette. Then each color will be reset with black color after pressing a key one by one. Then it will restore all the colors in one shot. Note the drawing text happens only once but colors get changed at run time.

 

Back to Turbo C Graphics Index



Source Code


#include <graphics.h>

#include <stdio.h>

#include <conio.h>

#include <dos.h>

#include <stdlib.h>

 

int InitGraphics()

{

      int grd, grm;

      int gresult;

      // Detect the graphics driver and mode

      detectgraph(&grd,&grm);

      // initialize the graphics mode with initgraph

      initgraph(&grd, &grm, "");

 

      gresult = graphresult();

      if(gresult != grOk)

      {

            printf(grapherrormsg(gresult));

            getch();

            return -1;

      }

 

      // set the background color

      setbkcolor(BLUE);

      // set the foreground color

      setcolor(WHITE);

      // draw a white color border with rectangle

      rectangle(0,0,getmaxx(),getmaxy());

      return 1;

}

 

void draw_image()

{

      int i, j, x, y, w;

      int left,top,right,bottom;

      int margin;

      char buf[128];

      struct  palettetype far * p, cp;

 

      margin = 50; // 100 pixel margin

      left = margin;

      top = margin;

      bottom = getmaxy() - margin;

      right = getmaxx() - margin;

 

      p = getdefaultpalette();

 

      setfillstyle(SOLID_FILL, BLUE);

      setcolor(WHITE);

      bar(left,top,right,bottom);

      rectangle(left,top,right,bottom);

 

      sprintf(buf, "Number of colors in Palette: %d", getpalettesize());

      outtextxy( left + 50, top + 10, buf);

 

      // draw the text in default palette color

      for(i = 0; i <= 15; i++)

      {

      setcolor(i);

      sprintf(buf, "Palette %d: 0x%x", i, p->colors[i]);

      outtextxy( left + 50, top + 30 + i * 20, buf);

      }

 

      // change the default color palette to BLACK for all colors except WHITE

      for(i = 0; i < 15; i++)

      {

      setpalette(i, BLACK);

      getch();

      }

 

      getpalette(&cp);

      sprintf(buf, "Blue Color Palette is: 0x%x", cp.colors[RED]);

      outtextxy(10, 10, buf);

 

      // Restore the original palette

      setallpalette(p);

 

      getch();

 

      getpalette(&cp);

      sprintf(buf, "Blue Color Palette is: 0x%x", cp.colors[RED]);

      outtextxy(10, 30, buf);

}

 

void main()

{

      if(InitGraphics() == -1)

          return;

      draw_image();

 

      getch();

      closegraph();

}

 

 

 

Output