View Full Version : Instances of Color in C++

06-15-2003, 10:08 AM
I need to create instances of colors so i can display the color value at different positions(height). I have found a section of code in Java that pretty much does exactly what I want to do, but i need it in C++, and i know that the opengl function calls are not the same, but have no idea what they are.


private RGB blue = new RGB (0.0, 0.0, 1.0);
private RGB green = new RGB (0.0, 1.0, 0.0);
private RGB white = new RGB (1.0, 1.0, 1.0);

public RGB getColor (double i, double j) {
double a = getAltitude (i, j);
if (a < .5)
return blue.add (green.subtract (blue).scale ((a - 0.0) / 0.5));
return green.add (white.subtract (green).scale ((a - 0.5) / 0.5));

How can i create those instances in C++ and does c++ have functional calls, like the ones used in the java script, i.e. "blue.add", "green.subtract".

Thank you very much for your help.

06-16-2003, 12:08 PM
The RGB class in the Java example is not an OpenGL class per se. You could write the same thing in C++ but would probably overload the '+' and '-' operators to implement add and subtract. Here's a quick and dirty example:

class RGB {
float r;
float g;
float b;
friend RGB operator+(const RGB & lhs, const RGB & rhs)
RGB c;

c.r = lhs.r + rhs.r;
c.g = lhs.g + rhs.g;
c.b = lhs.b + rhs.b;

return c;