It's a scoping problem. Your rrr
, ggg
, and bbb
variables are defined in a scope that's not visible when you use them in the Color
constructor.
Declare them earlier in your program.
public static int combineBrighter(Color c1, Color c2) {
int r = c1.getRed();
int g = c1.getGreen();
int b = c1.getBlue();
int rr = c2.getRed();
int gg = c2.getGreen();
int bb = c2.getBlue();
int rrr; // declare them in this scope
int ggg;
int bbb;
if(r >= rr){
rrr = r; // assign values later
} else {
rrr = rr;
}
...
Color f = new Color(rrr, ggg, bbb);
return f;
Here's a bit more information about block scope: http://www.cs.umd.edu/~clin/MoreJava/ControlFlow/block.html
However, you could declare and initialise them at the same time by using the ternary ?
operator.
int rrr = r >= rr ? r : rr;
int ggg = g >= gg ? r : gg;
int bbb = b >= bb ? r : bb;
But, why re-invent the wheel? This one is much easier to understand:
int maxRed = Math.max(c1.getRed(), c2.getRed());
int maxGreen = Math.max(c1.getGreen(), c2.getGreen());
int maxBlue = Math.max(c1.getBlue(), c2.getBlue());