1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
#include <cmath>
using namespace std;
#define WIDTH 700
#define HEIGHT 600
#define MENUWIDTH 100
#define BOXHEIGHT (HEIGHT/6)
#define NCOLORS 6
#define RGBRED (1, 0, 0)
#define RGBGREEN (0, 1, 0)
#define RGBBLUE (0, 0, 1)
#define RGBYELLOW (1, 1, 0)
#define RGBMAGENTA (1, 0, 1)
#define RGBCYAN (0, 1, 1)
#define RGBWHITE (1, 1, 1)
#define R 0
#define G 1
#define B 2
#define Y 3
#define M 4
#define C 5
static float colormenu[][6 3] = {{RGBRED}, {RGBGREEN}, {RGBBLUE}, {RGBYELLOW}, {RGBMAGENTA}, {RGBCYAN}}; // Note: Still 3 rgb values
const int NCOLORS = sizeof(colormenu) / sizeof(*colormenu); // Note: this calculates the number of colors automatically
int incolormenu(int x, int y)
{
return (x >= 0 && x <= MENUWIDTH && y >= 0 && y <= HEIGHT);
}
int colormenuindex(int x, int y)
{
if(!incolormenu(x, y))
return -1;
else
return(y / BOXHEIGHT);
}
void handleButton(int button, int state, int x, int y)
{
static int menuindex = -1;
y = HEIGHT - y;
if(button != GLUT_LEFT_BUTTON)
return;
if(state == GLUT_DOWN)
{
if(incolormenu(x, y))
menuindex = colormenuindex(x, y);
}
else
{
if(incolormenu(x, y) && colormenuindex(x, y) == menuindex)
{
glColor3f(colormenu[menuindex][R], colormenu[menuindex][G], colormenu[menuindex][B], colormenu[menuindex][C], colormenu[menuindex][M], colormenu[menuindex][Y]); // Note: RGB
glRecti(MENUWIDTH, 0, WIDTH, HEIGHT);
}
glFlush();
}
}
void drawMenu()
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
for(i = 0; i < NCOLORS; i++)
{
glColor3f(colormenu[i][R], colormenu[i][G], colormenu[i][B], colormenu[i][C], colormenu[i][M], colormenu[i][Y]); // Note: RGB
glRecti(1, BOXHEIGHT * i + 1, MENUWIDTH - 1, BOXHEIGHT * (i + 1) - 1);
}
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Color Menu");
gluOrtho2D(0, WIDTH, 0, HEIGHT);
glutDisplayFunc(drawMenu);
glutMouseFunc(handleButton);
glClearColor(RGBWHITE, 1, 1, 1);
glutMainLoop();
return 0;
}
|