Thursday 7 May 2009

Sine in action - part 4

The getColourValue() function

In our main program instead of setting the point (x,f(x)) to a particular colour, we are choosing to set every other point (x,y) based on the proximity of y to f(x); where f(x) is defined as above.

In order to set the colour of pixel y based on its proximity to
f(x) we need a function based on y and f(x) to calculate this. We have chosen to do this by creating another function getColourValue() that calculates how the range of colours should be distributed around f(x) and returns a value that represents the position of y within this colour range.

We pass y as the argument value, and
f(x) as the argument maxValue to getColourValue(). The function then returns a value between 0 and 255 depending on how close the argument value is to the argument maxValue. The function scaleValue() we discussed above is used to scale the return values appropriately so that they are in this range.

At value = maxValue the function returns 255. As value moves further from maxValue then the return value is decreased. The rate of the decrease is set so that at least one of value = 0 or value = height will return 0; which of these returns 0 is dictated by which is furthest from maxValue. So that if maxValue is centred the decrease will be even on both sides. If maxValue is offset, either above or below the centre then only one side will reach zero, and the minimum colour. The other side will decrease at the same rate but won’t reach the minimum. This is shown in Figure 5.


Figure 5 Centred and offset colour distribution graphs

For

float h = img.height;

then the line

minValue = min(0, 2* maxValue - h);

is used to determine whether the minimum will be reached at 0 or at h.

If maxValue is above the centre of the screen then 0 will be the minimum point, minValue. The colours will be plotted with red at maxValue and blue at 0, with the colours changing at the same rate from maxValue to h so that the minimum colour isn’t reached before h.

If maxValue is below the centre of the screen, then the minimum point, minValue, will be below 0 at 2* maxValue – h. This means that if we continued to plot colours below 0 then the minimum colour blue would be reached at minValue. Our choice of minimum here ensures that as we approach h then getColorVal() approaches zero and we plot the full range of colours from red at maxValue and through to blue at h.

The variable float rangeColor is defined as
rangeColor = max(maxValue, h - maxValue);

This is the distance that colours will range over, it is the maximum of the distance from zero to maxValue (or f(x)) and the distance from h to maxValue (or f(x)). In common with to finding the minimum, if maxValue is above the centre of the screen then rangeColor = maxValue, otherwise rangeColor = h- maxValue, this is illustrated in Figure 6.

Figure 6 Plotting the colour range

The variable rangeColor is used to scale the return value so that it returns values from 0 to 255 over the distance rangeColor. This is done using the scaleValue() function we described earlier with the minimum as 0 and the maximum as rangeColor.

temp = scaleValue(temp, 0, rangeColor);

When combined with the function getColor(), the output of getColorValue() can be plotted as shown Figure 7 with f(x) as a constant.

Figure 7 Colour distribution plotted around a constant

No comments: