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
27 #include "swfdec_color.h"
28 #include "swfdec_debug.h"
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);
41 start_ratio
= 65535 - 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
);
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);
60 swfdec_color_apply_transform (unsigned int in
, const SwfdecColorTransform
* trans
)
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
);
87 * swfdec_color_transform_init_identity:
88 * @trans: a #SwfdecColorTransform
90 * Initializes the color transform so it doesn't transform any colors.
93 swfdec_color_transform_init_identity (SwfdecColorTransform
* trans
)
95 g_return_if_fail (trans
!= NULL
);
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
116 swfdec_color_transform_init_color (SwfdecColorTransform
*trans
, SwfdecColor color
)
119 trans
->rb
= SWF_COLOR_R (color
);
121 trans
->gb
= SWF_COLOR_G (color
);
123 trans
->bb
= SWF_COLOR_B (color
);
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.
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
;