implement breakpoint API (breakpoints don't actually trigger yet)
[swfdec.git] / libswfdec / swfdec_color.c
blob66b99c6347b1a4a05acc7da7250df43f15bb7e19
1 /* Swfdec
2 * Copyright (C) 2003-2006 David Schleef <ds@schleef.org>
3 * 2005-2006 Eric Anholt <eric@anholt.net>
4 * 2006 Benjamin Otte <otte@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <math.h>
27 #include "swfdec_color.h"
28 #include "swfdec_debug.h"
30 SwfdecColor
31 swfdec_color_apply_morph (SwfdecColor start, SwfdecColor end, unsigned int ratio)
33 unsigned int r, g, b, a;
34 unsigned int start_ratio, end_ratio;
36 g_assert (ratio < 65536);
37 if (ratio == 0)
38 return start;
39 if (ratio == 65535)
40 return end;
41 start_ratio = 65535 - ratio;
42 end_ratio = ratio;
43 r = (SWF_COLOR_R (start) * start_ratio + SWF_COLOR_R (end) * end_ratio) / 65535;
44 g = (SWF_COLOR_G (start) * start_ratio + SWF_COLOR_G (end) * end_ratio) / 65535;
45 b = (SWF_COLOR_B (start) * start_ratio + SWF_COLOR_B (end) * end_ratio) / 65535;
46 a = (SWF_COLOR_A (start) * start_ratio + SWF_COLOR_A (end) * end_ratio) / 65535;
48 return SWF_COLOR_COMBINE (r, g, b, a);
51 void
52 swfdec_color_set_source (cairo_t *cr, SwfdecColor color)
54 cairo_set_source_rgba (cr,
55 SWF_COLOR_R (color) / 255.0, SWF_COLOR_G (color) / 255.0,
56 SWF_COLOR_B (color) / 255.0, SWF_COLOR_A (color) / 255.0);
59 unsigned int
60 swfdec_color_apply_transform (unsigned int in, const SwfdecColorTransform * trans)
62 int r, g, b, a;
64 r = SWF_COLOR_R (in);
65 g = SWF_COLOR_G (in);
66 b = SWF_COLOR_B (in);
67 a = SWF_COLOR_A (in);
69 SWFDEC_LOG ("in rgba %d,%d,%d,%d", r, g, b, a);
71 r = (r * trans->ra >> 8) + trans->rb;
72 g = (g * trans->ga >> 8) + trans->gb;
73 b = (b * trans->ba >> 8) + trans->bb;
74 a = (a * trans->aa >> 8) + trans->ab;
76 r = CLAMP (r, 0, 255);
77 g = CLAMP (g, 0, 255);
78 b = CLAMP (b, 0, 255);
79 a = CLAMP (a, 0, 255);
81 SWFDEC_LOG ("out rgba %d,%d,%d,%d", r, g, b, a);
83 return SWF_COLOR_COMBINE (r, g, b, a);
86 /**
87 * swfdec_color_transform_init_identity:
88 * @trans: a #SwfdecColorTransform
90 * Initializes the color transform so it doesn't transform any colors.
91 **/
92 void
93 swfdec_color_transform_init_identity (SwfdecColorTransform * trans)
95 g_return_if_fail (trans != NULL);
97 trans->ra = 256;
98 trans->ga = 256;
99 trans->ba = 256;
100 trans->aa = 256;
101 trans->rb = 0;
102 trans->gb = 0;
103 trans->bb = 0;
104 trans->ab = 0;
108 * swfdec_color_transform_init_color:
109 * @trans: a #SwfdecColorTransform
110 * @color: a #SwfdecColor to transform to
112 * Initializes this color transform so it results in exactly @color no matter
113 * the input.
115 void
116 swfdec_color_transform_init_color (SwfdecColorTransform *trans, SwfdecColor color)
118 trans->ra = 0;
119 trans->rb = SWF_COLOR_R (color);
120 trans->ga = 0;
121 trans->gb = SWF_COLOR_G (color);
122 trans->ba = 0;
123 trans->bb = SWF_COLOR_B (color);
124 trans->aa = 0;
125 trans->ab = SWF_COLOR_A (color);
129 * swfdec_color_transform_chain:
130 * @dest: #SwfdecColorTransform to take the result
131 * @last: a #SwfdecColorTransform
132 * @first: a #SwfdecColorTransform
134 * Computes a color transform that has the same effect as if the color
135 * transforms @first and @last would have been applied one after another.
137 void
138 swfdec_color_transform_chain (SwfdecColorTransform *dest,
139 const SwfdecColorTransform *last, const SwfdecColorTransform *first)
141 g_return_if_fail (dest != NULL);
142 g_return_if_fail (last != NULL);
143 g_return_if_fail (first != NULL);
145 /* FIXME: CLAMP here? */
146 dest->ra = last->ra * first->ra >> 8;
147 dest->rb = (last->ra * first->rb >> 8) + last->rb;
148 dest->ga = last->ga * first->ga >> 8;
149 dest->gb = (last->ga * first->gb >> 8) + last->gb;
150 dest->ba = last->ba * first->ba >> 8;
151 dest->bb = (last->ba * first->bb >> 8) + last->bb;
152 dest->aa = last->aa * first->aa >> 8;
153 dest->ab = (last->aa * first->ab >> 8) + last->ab;