How do I make this work correctly?

I'm trying to make a program that has 6 colors on the side, which can be clicked to change the color of the canvas. This is built just for three colors though. How do I make it six colrs??

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
  #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] = {{RGBRED}, {RGBGREEN}, {RGBBLUE}, {RGBYELLOW}, {RGBMAGENTA}, {RGBCYAN}};

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]);
      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]);
    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;
}
How do I make it six colrs??
It doesn't matter whether you have 3,6,or 600 colors. The color itself still consist of the three rgb values.

Make some small changes and you can have an arbitrary number of colors without changing the code:
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;
}
Topic archived. No new replies allowed.