开发者

Calculate two int values by current time

I am a bit stuck with this. I want to determine two opacity int values. One should increase and the other should decrease by hour.

Imagine a day consists of 4 parts. Night, sunrise, day and sunset. Each of the parts is pictured by two images that are overlayed. Night has a night image and a sunrise image. At midnight night image has an opacity of lets say 255 while sunrise is actually invisible.

While the time elapses to e.g. 4 o'clock the night image's opacity gets lower while the sunrise's opacity increases. At 5 o'clock night is over with an image opacity of 0 while sunrise image has an opacity of 255.

So in each of the 4 parts I want to calculate two opacity values, one increasing the other decreasing. In each part the opacity goes from 255 to 0 and on the other side from 0 to 255.

This is how I seperate the parts in hours

switch (hour) {
        //night to sunrise
        case 23: case 0: case 1: case 2: case 3: case 4:
            [self setSetting:1 andOpacity:opacIn andOpacity:opacOut];
            break;
        //sunrise to day
        case 5: case 6: case 7: case 8: case 9: case 10:
            [self setSetting:1 andOpacity:opacIn andOpacity:opacOut];
            break;
        //day to sunset
        case 11: case 12: case 13: case 14: case 15: case 16:
            [self setSetting:4 andOpacity:opacIn andOpacity:opacOut];
      开发者_如何学C      break;
        //sunset to night
        case 17: case 18: case 19: case 20: case 21: case 22:
            [self setSetting:2 andOpacity:opacIn andOpacity:opacOut];
            break;
    }

I tried it with this solution

int opacIn = hour * 10;

if (opacIn >= 230) {
    opacIn += 25;
}

int opacOut = 255 - opacIn;

The problem with this is that this determines the opacity in reference to the whole day. So it goes from 255 to 0 only once. But I want it to be this way for each part of the day.

Any ideas on this? How could I determine the opacity for each part?


It looks like you need a dash of the old modular arithmetic. Your day is broken up into four segments of six hours each; each hour you want to change some value, and the corresponding hours in every segment have the same value.

So first, divide your target value by the number of steps:

// Avoid magic numbers
#define MAX_OPACITY 255
#define HOURS_PER_SEGMENT 6
#define OPACITY_STEP (MAX_OPACITY / HOURS_PER_SEGMENT)

Notice that, if you read the cases of your switch statement as columns, every value in a column has the same result modulo 6. This is the key; each time you check the time, find out what your position is in the current segment:

// For purposes of opacity, it's as if 11PM is the first hour of the day
int shifted_hour = hour + 1;
if( 24 == shifted_hour) shifted_hour = 0;
int segment_pos = shifted_hour % HOURS_PER_SEGMENT;

Now you get your opacity value quite simply:

int opacIn = segment_pos * OPACITY_STEP;
int opacOut = MAX_OPACITY - opacIn;

You could also do away with the switch:

int segment_index = shifted_hour / 6;

[self setSegment:segment_index withOpacityIn:opacIn withOpacityOut:opacOut];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜