File: gfx-palette.c

package info (click to toggle)
libvisual-plugins 1%3A0.4.0%2Bdfsg1-16
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,656 kB
  • sloc: ansic: 9,358; sh: 8,809; cpp: 871; makefile: 222; sed: 16
file content (202 lines) | stat: -rw-r--r-- 5,560 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Libvisual-plugins - Standard plugins for libvisual
 * 
 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Dennis Smit <ds@nerds-incorporated.org>
 *
 * Authors: Dennis Smit <ds@nerds-incorporated.org>
 *
 * $Id: gfx-palette.c,v 1.6 2006/01/22 13:25:26 synap Exp $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <math.h>
#include <string.h>

#include <libvisual/libvisual.h>

#include "common.h"
#include "misc.h"
#include "screen.h"
#include "gfx-palette.h"

void _oink_gfx_palette_init (OinksiePrivate *priv)
{
	priv->pal_startup = 1;
	
	priv->pal_data.fade_start = 1;
	priv->pal_data.fade_steps = 100;
	priv->pal_data.fade_poststeps = priv->pal_data.fade_steps;
	priv->pal_data.fade_poststop = 1;
}

void _oink_gfx_palette_save_old (OinksiePrivate *priv)
{
	/* If we haven't set up an fade yet, don't save the next old.
	 * this protects us from more event handles per screen draw
	 */
	if (priv->pal_data.pal_new == 1 && priv->pal_data.fade_stepsdone == priv->pal_data.fade_steps)
		visual_palette_copy (&priv->pal_old, &priv->pal_cur);
	
	priv->pal_data.fade_start = 1;
}

void _oink_gfx_palette_transform (OinksiePrivate *priv)
{
	int i;

	if (priv->pal_data.fade_start == 1)
	{
		priv->pal_data.fade_stepsdone = 0;
		
		for (i = 0; i < 256; i++)
		{
			priv->pal_fades[i].r = (float) (priv->pal_cur.colors[i].r - priv->pal_old.colors[i].r) / (float) priv->pal_data.fade_steps;
			priv->pal_fades[i].g = (float) (priv->pal_cur.colors[i].g - priv->pal_old.colors[i].g) / (float) priv->pal_data.fade_steps;
			priv->pal_fades[i].b = (float) (priv->pal_cur.colors[i].b - priv->pal_old.colors[i].b) / (float) priv->pal_data.fade_steps;

			priv->pal_fades[i].r_cur = priv->pal_old.colors[i].r;
			priv->pal_fades[i].g_cur = priv->pal_old.colors[i].g;
			priv->pal_fades[i].b_cur = priv->pal_old.colors[i].b;
		}
	
		priv->pal_data.fade_start = 0;
	}

	for (i = 0; i < 256; i++)
	{
		priv->pal_fades[i].r_cur += priv->pal_fades[i].r;
		priv->pal_fades[i].g_cur += priv->pal_fades[i].g;
		priv->pal_fades[i].b_cur += priv->pal_fades[i].b;
	
		priv->pal_old.colors[i].r = priv->pal_fades[i].r_cur;
		priv->pal_old.colors[i].g = priv->pal_fades[i].g_cur;
		priv->pal_old.colors[i].b = priv->pal_fades[i].b_cur;
	}

	priv->pal_data.fade_stepsdone++;

	if (priv->pal_data.fade_stepsdone >= priv->pal_data.fade_poststeps)
	{
		visual_palette_copy (&priv->pal_cur, &priv->pal_old);

		priv->pal_data.pal_new = 0;
		priv->pal_data.fade_start = 1;
		priv->pal_startup = 0;
	}
}

void _oink_gfx_palette_build (OinksiePrivate *priv, uint8_t funky)
{
	/* save old palette for transistion */
	_oink_gfx_palette_save_old (priv);
	_oink_gfx_palette_build_gradient (priv, funky);

	/* FIXME, changing fadesteps can fuck this up */
	if (priv->pal_data.fade_poststop == 1 && priv->pal_startup == 0) {
		switch (visual_random_context_int_range (priv->rcontext, 0, 5)) {
		case 0:
		case 1:
			priv->pal_data.fade_poststeps = visual_random_context_int_range (priv->rcontext, 60, priv->pal_data.fade_steps);
			break;

		case 2:
			priv->pal_data.fade_poststeps = visual_random_context_int_range (priv->rcontext, 40, priv->pal_data.fade_steps / 2);
			break;

		case 3:
			priv->pal_data.fade_poststeps = visual_random_context_int_range (priv->rcontext, 20, priv->pal_data.fade_steps / 3);
			break;
		case 4:
			priv->pal_data.fade_poststeps = visual_random_context_int_range (priv->rcontext, 80, priv->pal_data.fade_steps);
			break;

		default:
			priv->pal_data.fade_poststeps = priv->pal_data.fade_steps;
			break;
		}

	}

	priv->pal_data.pal_new = 1;	
}

void _oink_gfx_palette_color (OinksiePrivate *priv, int color, int red, int green, int blue)
{
	priv->pal_cur.colors[color].r = red << 2;
	priv->pal_cur.colors[color].g = green << 2;
	priv->pal_cur.colors[color].b = blue << 2;
} 

uint8_t _oink_gfx_palette_gradient_gen (OinksiePrivate *priv, uint8_t i, int mode)
{
	static const float __magic = 2.0 * PI/256;

	switch (mode)
	{
	case 0:
		return (i * i * i) >> 16; 
		break;

	case 1:
		return (i * i) >> 8;
		break;

	case 2:
		return (uint8_t) i;
		break;

	case 3:
		return (uint8_t) (128 * fabs (sin ( __magic * i )));
		break;

	case 4:
		return 0;
		break;

	default:
		break;
	}

	return 0;
}

void _oink_gfx_palette_build_gradient (OinksiePrivate *priv, uint8_t funky)
{
	int i = 0;
	int j = 0;
	int k = 0;
	int l = 0;
 	
	if (funky == TRUE)
		i = 5;
	else
		i = 3;
	
	do
	{
		j = visual_random_context_int_range (priv->rcontext, 0, i - 1);
		k = visual_random_context_int_range (priv->rcontext, 0, i - 1);
		l = visual_random_context_int_range (priv->rcontext, 0, i - 1);		
		
	} while (j == k || j == l || l == k);
	
	for( i = 0; i <= 255; i++)
	{
		priv->pal_cur.colors[i].r = _oink_gfx_palette_gradient_gen (priv, i, j);
		priv->pal_cur.colors[i].g = _oink_gfx_palette_gradient_gen (priv, i, k);
		priv->pal_cur.colors[i].b = _oink_gfx_palette_gradient_gen (priv, i, l);
	}
}