/*
* Copyright (C) 2013 Patrick "P. J." McDermott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* .
*/
#include
#include
#include "palettes.h"
#include "logging.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static inline Uint8 cycle_palettes_cosine_channel(Uint8, Uint8, double);
static inline Uint8 cycle_palettes_linear_channel(Uint8, Uint8, double);
SDL_Palette *
cycle_palettes_cosine(Uint32 t, Uint32 t_max,
SDL_Palette *pal_start, SDL_Palette *pal_end,
SDL_Palette *pal_step)
{
double x;
int col_i;
if (pal_start->ncolors != pal_end->ncolors) {
return NULL;
}
if (pal_start->ncolors != pal_step->ncolors) {
return NULL;
}
x = (double) (t % t_max) / (double) t_max * M_PI;
for (col_i = 0; col_i < pal_step->ncolors; ++col_i) {
pal_step->colors[col_i].r = cycle_palettes_cosine_channel(
pal_start->colors[col_i].r,
pal_end->colors[col_i].r,
x);
pal_step->colors[col_i].g = cycle_palettes_cosine_channel(
pal_start->colors[col_i].g,
pal_end->colors[col_i].g,
x);
pal_step->colors[col_i].b = cycle_palettes_cosine_channel(
pal_start->colors[col_i].b,
pal_end->colors[col_i].b,
x);
}
return pal_step;
}
SDL_Palette *
cycle_palettes_linear(Uint32 t, Uint32 t_max,
SDL_Palette *pal_start, SDL_Palette *pal_end,
SDL_Palette *pal_step)
{
double x;
int col_i;
if (pal_start->ncolors != pal_end->ncolors) {
return NULL;
}
if (pal_start->ncolors != pal_step->ncolors) {
return NULL;
}
x = (double) (t % t_max) / (double) t_max;
for (col_i = 0; col_i < pal_step->ncolors; ++col_i) {
pal_step->colors[col_i].r = cycle_palettes_linear_channel(
pal_start->colors[col_i].r,
pal_end->colors[col_i].r,
x);
pal_step->colors[col_i].g = cycle_palettes_linear_channel(
pal_start->colors[col_i].g,
pal_end->colors[col_i].g,
x);
pal_step->colors[col_i].b = cycle_palettes_linear_channel(
pal_start->colors[col_i].b,
pal_end->colors[col_i].b,
x);
}
return pal_step;
}
static inline Uint8
cycle_palettes_cosine_channel(Uint8 y_start, Uint8 y_end, double x)
{
double m;
Uint8 b;
Uint8 y;
m = y_end - y_start;
b = y_start;
y = m * ((cos(x) / -2.0) + 0.5) + b;
return y;
}
static inline Uint8
cycle_palettes_linear_channel(Uint8 y_start, Uint8 y_end, double x)
{
double m;
Uint8 b;
Uint8 y;
m = y_end - y_start;
b = y_start;
y = m * x + b;
return y;
}