add blend mode tests
[swfdec.git] / swfdec / swfdec_color_as.c
blob2b688616053f945df2d157a6436c0ad3d6180b0a
1 /* Swfdec
2 * Copyright (C) 2006-2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "swfdec_as_context.h"
25 #include "swfdec_as_native_function.h"
26 #include "swfdec_as_strings.h"
27 #include "swfdec_debug.h"
28 #include "swfdec_internal.h"
29 #include "swfdec_as_internal.h"
30 #include "swfdec_movie.h"
32 /*** AS CODE ***/
34 static SwfdecMovie *
35 swfdec_movie_color_get_movie (SwfdecAsObject *object)
37 SwfdecAsValue val;
39 if (object == NULL)
40 return NULL;
42 swfdec_as_object_get_variable (object, SWFDEC_AS_STR_target, &val);
43 if (!SWFDEC_AS_VALUE_IS_MOVIE (val))
44 return NULL;
46 return SWFDEC_AS_VALUE_GET_MOVIE (val);
49 SWFDEC_AS_NATIVE (700, 2, swfdec_movie_color_getRGB)
50 void
51 swfdec_movie_color_getRGB (SwfdecAsContext *cx, SwfdecAsObject *obj,
52 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
54 int result;
55 SwfdecMovie *movie;
57 movie = swfdec_movie_color_get_movie (obj);
59 if (movie == NULL)
60 return;
62 result = (movie->color_transform.rb << 16) |
63 ((movie->color_transform.gb % 256) << 8) |
64 (movie->color_transform.bb % 256);
65 *ret = swfdec_as_value_from_integer (cx, result);
68 static void
69 add_variable (SwfdecAsObject *obj, const char *name, double value)
71 SwfdecAsValue val;
73 val = swfdec_as_value_from_number (obj->context, value);
74 swfdec_as_object_set_variable (obj, name, &val);
77 SWFDEC_AS_NATIVE (700, 3, swfdec_movie_color_getTransform)
78 void
79 swfdec_movie_color_getTransform (SwfdecAsContext *cx, SwfdecAsObject *obj,
80 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
82 SwfdecAsObject *ret;
83 SwfdecMovie *movie;
85 movie = swfdec_movie_color_get_movie (obj);
87 if (movie == NULL)
88 return;
90 ret = swfdec_as_object_new (cx, SWFDEC_AS_STR_Object, NULL);
92 add_variable (ret, SWFDEC_AS_STR_ra, movie->color_transform.ra * 100.0 / 256.0);
93 add_variable (ret, SWFDEC_AS_STR_ga, movie->color_transform.ga * 100.0 / 256.0);
94 add_variable (ret, SWFDEC_AS_STR_ba, movie->color_transform.ba * 100.0 / 256.0);
95 add_variable (ret, SWFDEC_AS_STR_aa, movie->color_transform.aa * 100.0 / 256.0);
96 add_variable (ret, SWFDEC_AS_STR_rb, movie->color_transform.rb);
97 add_variable (ret, SWFDEC_AS_STR_gb, movie->color_transform.gb);
98 add_variable (ret, SWFDEC_AS_STR_bb, movie->color_transform.bb);
99 add_variable (ret, SWFDEC_AS_STR_ab, movie->color_transform.ab);
100 SWFDEC_AS_VALUE_SET_OBJECT (rval, ret);
103 SWFDEC_AS_NATIVE (700, 0, swfdec_movie_color_setRGB)
104 void
105 swfdec_movie_color_setRGB (SwfdecAsContext *cx, SwfdecAsObject *obj,
106 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
108 guint color;
109 SwfdecMovie *movie;
111 if (argc < 1)
112 return;
114 movie = swfdec_movie_color_get_movie (obj);
116 if (movie == NULL)
117 return;
119 color = swfdec_as_value_to_integer (cx, argv[0]);
121 movie->color_transform.ra = 0;
122 movie->color_transform.rb = (color & 0xFF0000) >> 16;
123 movie->color_transform.ga = 0;
124 movie->color_transform.gb = (color & 0xFF00) >> 8;
125 movie->color_transform.ba = 0;
126 movie->color_transform.bb = color & 0xFF;
127 swfdec_movie_invalidate_last (movie);
130 static void
131 parse_property (SwfdecAsObject *obj, const char *name, int *target, gboolean scale)
133 SwfdecAsValue val;
134 double d;
136 if (!swfdec_as_object_get_variable (obj, name, &val))
137 return;
138 d = swfdec_as_value_to_number (obj->context, val);
139 if (scale) {
140 *target = d * 256.0 / 100.0;
141 } else {
142 *target = d;
146 SWFDEC_AS_NATIVE (700, 1, swfdec_movie_color_setTransform)
147 void
148 swfdec_movie_color_setTransform (SwfdecAsContext *cx, SwfdecAsObject *obj,
149 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
151 SwfdecAsObject *parse;
152 SwfdecMovie *movie;
154 if (argc < 1)
155 return;
157 movie = swfdec_movie_color_get_movie (obj);
159 if (movie == NULL)
160 return;
162 if (!SWFDEC_AS_VALUE_IS_COMPOSITE (argv[0]))
163 return;
164 parse = SWFDEC_AS_VALUE_GET_COMPOSITE (argv[0]);
165 if (parse == NULL)
166 return;
167 parse_property (parse, SWFDEC_AS_STR_ra, &movie->color_transform.ra, TRUE);
168 parse_property (parse, SWFDEC_AS_STR_ga, &movie->color_transform.ga, TRUE);
169 parse_property (parse, SWFDEC_AS_STR_ba, &movie->color_transform.ba, TRUE);
170 parse_property (parse, SWFDEC_AS_STR_aa, &movie->color_transform.aa, TRUE);
171 parse_property (parse, SWFDEC_AS_STR_rb, &movie->color_transform.rb, FALSE);
172 parse_property (parse, SWFDEC_AS_STR_gb, &movie->color_transform.gb, FALSE);
173 parse_property (parse, SWFDEC_AS_STR_bb, &movie->color_transform.bb, FALSE);
174 parse_property (parse, SWFDEC_AS_STR_ab, &movie->color_transform.ab, FALSE);
175 swfdec_movie_invalidate_last (movie);