Question: You are designing an element for a control panel that displays a temperature value between 0 and 100. The elements color should vary continuously from

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.

B Path 255 White (255, 255, 255) 255 G 255

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.

B Path 255 White (255, 255, 255) 255 G 255

Step by Step Solution

3.47 Rating (176 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Here is an implementation of ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Concepts Late Objects Questions!