RGB in C

Default palette size for normal graphics driver is 16 i.e) the color number rages from 0 to 15

To use rgb color modes we have to choose VGA or IBM 8514 as a graphics driver

For VGA, Palette size is 16 i.e) color number ranges from 0-16
For IBM 8514, Palette size is 256 i.e) color number ranges from 0-255, we can also say it as VGA with 256 palette size

By using setrgbpalette function we can generate RGB colors, we can generate colors with respect to Palette size. This function replaces the default palette colors with the generated Palette colors.

Syntax:
void setrgbpalette(int colornum, int red, int green, int blue);

colornum - color number in palette (0-15 or 0-255 depens on graphics driver)
red - color value for red (0-255)
green - color value for green (0-255)
blue - color value for blue (0-255)

Example:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
void main(){
int gd=VGA, gm=VGAHI, i, r, g, b , x, y, w ,h;
char *msg;
initgraph(&gd,&gm,"");
w = getmaxx()/4;
h = getmaxy()/4;
randomize();
for(i=0;i<16;i++){
  x = w * (i % 4);
  y = h * (i / 4);
  r = random(255);
  g = random(255);
  b = random(255);

  setrgbpalette(i,r,g,b);
  setfillstyle(SOLID_FILL,i);
  bar(x , y , x+w, y+h);
  sprintf(msg, "R:%d G:%d B:%d", r,g,b);
  outtextxy(x+10, y+10, msg);
}
getch();
}

Output:

Popular posts from this blog

Simple Animation Using C

Personal Diary - A Mini Project Written in Turbo C With Graphical Interface

Random Bouncing Ball Animation