You are designing an element for a control panel that displays a temperature value between 0 and

Question:

You are designing an element for a control panel that displays a temperature value between 0 and 100. The element’s color should vary continuously from blue (when the temperature is 0) to red (when the temperature is 100). Write a method public static int colorForValue(double temperature) that returns a color value for the given temperature. Colors are encoded as red/green/blue values, each between 0 and 255. The three colors are combined into a single integer, using the formula color = 65536 × red + 256 × green + blue Each of the intermediate colors should be fully saturated; that is, it should be on the outside of the color cube, along the path that goes from blue through cyan, green, and yellow to red.

You need to know how to interpolate between values. In general, if an output y should vary from c to d as an input x varies from a to b, then y is computed as follows:

z = (x – a) / (b – a)
y = c (1 – z) + dz

If the temperature is between 0 and 25 degrees, interpolate between blue and cyan, whose (red, green, blue) components are (0, 0, 255) and (0, 255, 255). For temperature values between 25 and 50, interpolate between (0, 255, 255) and (0, 255, 0), which represents the color green. Do the same for the remaining two path segments. You need to interpolate each color component separately and then combine the interpolated colors to a single integer. Be sure to use appropriate helper methods to solve your task.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: