Write a public class named Color. This class will represent
a color the way a screen does: as an amount of red, green, and blue light
each between 0 (none) and 255 (as much as possible). But for now just
write the basic class definition.
Write a main method that does the traditional thing of
printing "hello, world!".
Add three int instance variables named red,
green, and blue. They should be—as most
instance variables are—private.
Write a constructor that takes three int arguments,
red, green, and blue, and
initializes the corresponding instance variables.
Write getters for all three instance variables, following the
normal getName pattern: getRed,
getGreen, and getBlue. Each takes no arguments
and returns the current value of the corresponding instance variable.
Write setters for all three instance variables, following the
normal setName pattern: setRed,
setGreen, and setBlue. Each takes
an int argument and stores it in the corresponding instance
variable.
Write a method, brightness, that takes no arguments and
returns a double saying how bright the color looks. You
might think that’s just the sum of the three values, but our eyes are
much more sensitive to green light than to red, and much more sensitive
to red than to blue. The standard formula for the
relative
luminance of a color weights the three values accordingly:
0.2126 * red + 0.7152 * green + 0.0722 * blue
So black, Color<0,0,0>, has brightness 0, white,
Color<255,255,255>, has brightness 255, and pure
green, Color<0,255,0>, is about ten times as bright as
pure blue, Color<0,0,255>. (Strictly speaking the
formula applies to the amount of light coming out of the screen, which
isn’t quite proportional to these 0–255 values—but it’s close enough for
us.)
Write a method, brighten, that takes an int
amount and adds it to each of the red, green, and blue values, changing
the Color itself. But no value can go above 255, so if adding
the amount would push a value past 255, set it to 255 instead. You can
use the Math.min
method, which takes two arguments and returns the smaller of them, or
an if.
Write a method, brightened, that takes an int
amount and returns a new Color that is the current
color brightened by that amount, using the same rules
as brighten. Unlike brighten, this method
should not change the current Color at all: build and
return a new Color whose values are computed the same
way brighten computes them.
Write a method, isBrighterThan, that takes
another Color as its single argument and returns
a boolean indicating whether the current color is brighter
than the other one, i.e. its brightness is greater than the
other Color’s brightness. (Hint: you already
wrote a method that computes brightness. Use it.)
Write a method, mix, that takes another Color
as its single argument and returns a new Color
that is the mix of the two: its red value is the average of the two red
values, and likewise for green and blue. Since the values
are ints, plain integer division is fine for the average.
Neither the current Color nor the other one should be
changed by this method.
Write a toString method that returns a String of
the form "Color<255,128,0>" where 255, 128, and 0 are the
red, green, and blue values of the Color.
In your main method add code to create two
Colors: a sunset color with red 250, green 100, and blue 20,
and a sky color with red 30, green 140, and blue 240. Then add code so
your output is of the form:
hello, world!
sunset: the sunset color
sky: the sky color
mixed: the mix of the two
sunset is brighter: whether the sunset color is brighter than the sky color
brightened: the sunset color after brightening it by 10
Note: the above shows how the output should be broken into lines. The “got” and “expected” columns in the test results will not show the proper line breaks.