Uint8 r, g, b, r2, g2, b2, red, green, blue;
SDL_GetRGB(color2,screen->format, &r, &g, &b);
SDL_GetRGB(color3,screen->format, &r2, &g2, &b2);
red = r + (((int) (minutes)%60) / 60 ) * ( r2- r );
green = g + (((int) (minutes)%60) / 60 ) * (g2- g );
blue = b + (((int) (minutes)%60) / 60 ) * (b2 - b );
color = SDL_MapRGB( screen->format,red, green, blue);
interpolating between colors in SDL
In writing a dawn to dusk atmospheric light simulation, I am relating the current colors to the current minute of the day. Essentially each hour has its own color, and the colors between each hour are interpolated based on the number of minutes which have occured past that hour.
However, in trying to linearly interpolate between two colors, I am having trouble and for some reason it is not working. Below is essentially what I do. It seems like the colors are only changing every hour though, and not minute by minute.
The logic for example in the case of red is that r is the degree of red of the current hour and r2 is the degree of red for the next hour. By doing minutes%60 I get the amount of minutes after the last hour. By dividing that number by 60 I get a fractional amount which tells how much of the current hour is over. Thus I take the fractional amount of the difference between r and r2 and add it to r.
will not (minutes)%60) / 60 ) always give a zero? You probably want that to be a float.
ah I found the problem and it is a very stupid one at that, sorry for wasting your time....
original code:color2 = getpixel( gradient, minutes/60, 16-dist/16); color3 = getpixel( gradient, ( minutes < 960 ) ? minutes/60: 0, 16-dist/16 );fixed code:color2 = getpixel( gradient, minutes/60, 16-dist/16); color3 = getpixel( gradient, ( minutes < 960 ) ? minutes/60 + 1: 0, 16-dist/16 );Notice how in the fixed code, I had 1 to the x coordinate since color 3 is supposed to represent the next hour
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement