Lesson 7 of 15
Colors
Colors
Colors in the ray tracer are represented as (red, green, blue) triples where 0 is no intensity and 1 is full intensity. Values can exceed 1 (very bright) — they're clamped when writing to images.
The Color Class
class Color {
public:
double r, g, b;
Color(double r, double g, double b) : r(r), g(g), b(b) {}
};
Color Operations
Colors support the same arithmetic as tuples:
// Addition: mix two lights
Color addColors(Color a, Color b) {
return Color(a.r + b.r, a.g + b.g, a.b + b.b);
}
The Hadamard product (component-wise multiplication) blends a surface color with a light color:
// Hadamard product: surface color filtered by light color
Color hadamard(Color a, Color b) {
return Color(a.r * b.r, a.g * b.g, a.b * b.b);
}
Example
A red surface Color(1, 0, 0) illuminated by white light Color(1, 1, 1):
hadamard(red, white) = Color(1*1, 0*1, 0*1) = Color(1, 0, 0)
The surface appears red. Under a green light Color(0, 1, 0):
hadamard(red, green) = Color(1*0, 0*1, 0*0) = Color(0, 0, 0)
The surface appears black — no reflected light.
Your Task
Implement addColors and hadamard.
Expected output:
1 1 0
2 6 12JSCPP loading...
Loading...
Click "Run" to execute your code.