Fix few bugs in SwfdecGtkSocket that made it unusable
[swfdec.git] / swfdec / swfdec_color_as.c
blobb76de6699984b6ba09fdbebd61679da7645b716c
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;
38 SwfdecAsObject *target;
40 if (object == NULL)
41 return NULL;
43 swfdec_as_object_get_variable (object, SWFDEC_AS_STR_target, &val);
44 if (!SWFDEC_AS_VALUE_IS_OBJECT (&val))
45 return NULL;
47 target = SWFDEC_AS_VALUE_GET_OBJECT (&val);
48 if (!SWFDEC_IS_MOVIE (target))
49 return NULL;
51 return SWFDEC_MOVIE (target);
54 SWFDEC_AS_NATIVE (700, 2, swfdec_movie_color_getRGB)
55 void
56 swfdec_movie_color_getRGB (SwfdecAsContext *cx, SwfdecAsObject *obj,
57 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
59 int result;
60 SwfdecMovie *movie;
62 movie = swfdec_movie_color_get_movie (obj);
64 if (movie == NULL)
65 return;
67 result = (movie->color_transform.rb << 16) |
68 ((movie->color_transform.gb % 256) << 8) |
69 (movie->color_transform.bb % 256);
70 SWFDEC_AS_VALUE_SET_INT (ret, result);
73 static void
74 add_variable (SwfdecAsObject *obj, const char *name, double value)
76 SwfdecAsValue val;
78 SWFDEC_AS_VALUE_SET_NUMBER (&val, value);
79 swfdec_as_object_set_variable (obj, name, &val);
82 SWFDEC_AS_NATIVE (700, 3, swfdec_movie_color_getTransform)
83 void
84 swfdec_movie_color_getTransform (SwfdecAsContext *cx, SwfdecAsObject *obj,
85 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
87 SwfdecAsObject *ret;
88 SwfdecMovie *movie;
90 movie = swfdec_movie_color_get_movie (obj);
92 if (movie == NULL)
93 return;
95 ret = swfdec_as_object_new (cx);
97 add_variable (ret, SWFDEC_AS_STR_ra, movie->color_transform.ra * 100.0 / 256.0);
98 add_variable (ret, SWFDEC_AS_STR_ga, movie->color_transform.ga * 100.0 / 256.0);
99 add_variable (ret, SWFDEC_AS_STR_ba, movie->color_transform.ba * 100.0 / 256.0);
100 add_variable (ret, SWFDEC_AS_STR_aa, movie->color_transform.aa * 100.0 / 256.0);
101 add_variable (ret, SWFDEC_AS_STR_rb, movie->color_transform.rb);
102 add_variable (ret, SWFDEC_AS_STR_gb, movie->color_transform.gb);
103 add_variable (ret, SWFDEC_AS_STR_bb, movie->color_transform.bb);
104 add_variable (ret, SWFDEC_AS_STR_ab, movie->color_transform.ab);
105 SWFDEC_AS_VALUE_SET_OBJECT (rval, ret);
108 SWFDEC_AS_NATIVE (700, 0, swfdec_movie_color_setRGB)
109 void
110 swfdec_movie_color_setRGB (SwfdecAsContext *cx, SwfdecAsObject *obj,
111 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
113 guint color;
114 SwfdecMovie *movie;
116 if (argc < 1)
117 return;
119 movie = swfdec_movie_color_get_movie (obj);
121 if (movie == NULL)
122 return;
124 color = swfdec_as_value_to_integer (cx, &argv[0]);
126 movie->color_transform.ra = 0;
127 movie->color_transform.rb = (color & 0xFF0000) >> 16;
128 movie->color_transform.ga = 0;
129 movie->color_transform.gb = (color & 0xFF00) >> 8;
130 movie->color_transform.ba = 0;
131 movie->color_transform.bb = color & 0xFF;
132 swfdec_movie_invalidate_last (movie);
135 static void
136 parse_property (SwfdecAsObject *obj, const char *name, int *target, gboolean scale)
138 SwfdecAsValue val;
139 double d;
141 if (!swfdec_as_object_get_variable (obj, name, &val))
142 return;
143 d = swfdec_as_value_to_number (swfdec_gc_object_get_context (obj), &val);
144 if (scale) {
145 *target = d * 256.0 / 100.0;
146 } else {
147 *target = d;
151 SWFDEC_AS_NATIVE (700, 1, swfdec_movie_color_setTransform)
152 void
153 swfdec_movie_color_setTransform (SwfdecAsContext *cx, SwfdecAsObject *obj,
154 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
156 SwfdecAsObject *parse;
157 SwfdecMovie *movie;
159 if (argc < 1)
160 return;
162 movie = swfdec_movie_color_get_movie (obj);
164 if (movie == NULL)
165 return;
167 if (!SWFDEC_AS_VALUE_IS_OBJECT (&argv[0]))
168 return;
169 parse = SWFDEC_AS_VALUE_GET_OBJECT (&argv[0]);
170 parse_property (parse, SWFDEC_AS_STR_ra, &movie->color_transform.ra, TRUE);
171 parse_property (parse, SWFDEC_AS_STR_ga, &movie->color_transform.ga, TRUE);
172 parse_property (parse, SWFDEC_AS_STR_ba, &movie->color_transform.ba, TRUE);
173 parse_property (parse, SWFDEC_AS_STR_aa, &movie->color_transform.aa, TRUE);
174 parse_property (parse, SWFDEC_AS_STR_rb, &movie->color_transform.rb, FALSE);
175 parse_property (parse, SWFDEC_AS_STR_gb, &movie->color_transform.gb, FALSE);
176 parse_property (parse, SWFDEC_AS_STR_bb, &movie->color_transform.bb, FALSE);
177 parse_property (parse, SWFDEC_AS_STR_ab, &movie->color_transform.ab, FALSE);
178 swfdec_movie_invalidate_last (movie);