/**
 * colourswatch.c - A simple SVGAlib program to display the RGB values
 *                of colours in the 16 colour palette
 *
 *  Author: David Cannings <david@edeca.net>
 *    Date: 27/01/04
 * Version: 0.1
 *
 * To compile: gcc -lvga -lvgagl colourswatch.c -o colourswatch
 *   Requires: svgalib
 *
 * This program uses SVGAlib to print a swatch of colours 0 to 16 down
 * the left-hand side of the screen.  Alongside, it prints the colour
 * number with the corresponding R, G, B values.
 *
 * It was made so that I could convert an old SVGAlib program into
 * Java code as I needed the RGB values for each of the colours.
 *
 * The manpages for the various gl_* functions were an invaluable
 * reference when coding this.  For more information on function calls
 * please see:
 * 
 * $ man 7 svgalib
 * or
 * $ man 3 gl_<function>
 *
 * Note: The colour values will not be perfect as SVGAlib uses a 6 bit 
 *       palette and we are displaying them as an 8 bit number
 *
 *       Roughly speaking, 84 = 80, 168 = 160, 252 = 255
 *
 * Note: This may not work "out of the box" on every system as the mode
 *       "G640x480x16" may not be available.  If it isn't available,
 *       try changing for "G1024x768x16" instead.
 *
 * Note: You will need root access to run this (on Linux, at least) or
 *       your SVGAlib install will need to be installed suid root.
 *
 * Thanks to "amphi" from irc.freenode.net/#linux for helping with the
 * gl_getpalettecolor() function, in particular the shifting of bits
 * using '<< 2' which I had initially overlooked.
 */

#include <vga.h>
#include <vgagl.h>
#include <stdio.h>
#include <fcntl.h>

main() {
   int x, y, colour=0;
   int r, g, b;
   char buf[100];
   
   GraphicsContext physicalscreen;
   
   // Set the screen resolution and colour depth (the mode)
   vga_setmode(G640x480x16);
   gl_setcontextvga(G640x480x16);
   gl_getcontext(&physicalscreen);
   gl_setcontextvgavirtual(G640x480x16);
   
   // Set the font to use later with gl_write()
   gl_setfont(8, 8, gl_font8x8);
   gl_setwritemode(FONT_COMPRESSED + WRITEMODE_OVERWRITE);
   gl_setfontcolors(0, vga_white());							

   // This will give us a blank screen with black as the default
   // background colour
   gl_clearscreen(0);

   // Loop through all the colours 0 to 15 to draw swatches
   for (colour=0; colour<=15; colour++) {
  
      // Draw the swatches down the left of the screen
      for (x=0; x<10; x++) {
         for (y=colour * 10; y<(colour * 10) + 10; y++) {
            gl_setpixel(x, y, colour);
	 }
      }
      
      // Get the RGB value from the palette
      gl_getpalettecolor(colour,&r,&g,&b);
      
      // Print it all to screen, the palette is 6 bit so << 2
      sprintf(buf, "Colour: %d, R=%d, G=%d, B=%d", colour, r << 2, g << 2, b << 2);
      gl_write(15, (colour * 10) + 2, buf);
      
   }
   
   // Print program information to screen
   sprintf(buf, "Program: colourinfo, a simple program to display SVGAlib colour swatches");
   gl_write(0, 180, buf);

   sprintf(buf, " Author: David Cannings <david@edeca.net>");
   gl_write(0, 190, buf);

   sprintf(buf, "Please press any key to exit..");
   gl_write(0, 220, buf);
   
   // Copy to screen
   gl_copyscreen(&physicalscreen);

   // Wait for a keypress before closing
   while(!vga_getkey());
   
   return 0;
}  
