summaryrefslogtreecommitdiff
path: root/libmetacity/meta-gradient-spec.c
blob: 79f1bd5218be5cd949f6f53ca54591384f571db2 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright (C) 2001 Havoc Pennington
 * Copyright (C) 2016 Alberts Muktupāvels
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib/gi18n-lib.h>

#include "meta-color-spec-private.h"
#include "meta-gradient-spec-private.h"
#include "meta-theme.h"

struct _MetaGradientSpec
{
  MetaGradientType  type;
  GSList           *color_specs;
};

struct _MetaAlphaGradientSpec
{
  MetaGradientType  type;
  guchar           *alphas;
  gint              n_alphas;
};


static cairo_pattern_t *
create_cairo_pattern_from_gradient_spec (const MetaGradientSpec      *spec,
                                         const MetaAlphaGradientSpec *alpha_spec,
                                         GtkStyleContext             *context)
{
  gint n_colors;
  cairo_pattern_t *pattern;
  GSList *tmp;
  gint i;

  n_colors = g_slist_length (spec->color_specs);
  if (n_colors == 0)
    return NULL;

  if (alpha_spec != NULL && alpha_spec->n_alphas != 1)
    g_assert (n_colors == alpha_spec->n_alphas);

  if (spec->type == META_GRADIENT_HORIZONTAL)
    pattern = cairo_pattern_create_linear (0, 0, 1, 0);
  else if (spec->type == META_GRADIENT_VERTICAL)
    pattern = cairo_pattern_create_linear (0, 0, 0, 1);
  else if (spec->type == META_GRADIENT_DIAGONAL)
    pattern = cairo_pattern_create_linear (0, 0, 1, 1);
  else
    g_assert_not_reached ();

  i = 0;
  tmp = spec->color_specs;
  while (tmp != NULL)
    {
      GdkRGBA color;

      meta_color_spec_render (tmp->data, context, &color);

      if (alpha_spec != NULL)
        {
          gdouble alpha;

          if (alpha_spec->n_alphas == 1)
            alpha = alpha_spec->alphas[0] / 255.0;
          else
            alpha = alpha_spec->alphas[i] / 255.0;

          cairo_pattern_add_color_stop_rgba (pattern, i / (gfloat) (n_colors - 1),
                                             color.red, color.green, color.blue,
                                             alpha);
        }
      else
        cairo_pattern_add_color_stop_rgb (pattern, i / (gfloat) (n_colors - 1),
                                          color.red, color.green, color.blue);

      tmp = tmp->next;
      ++i;
    }

  if (cairo_pattern_status (pattern) != CAIRO_STATUS_SUCCESS)
    {
      cairo_pattern_destroy (pattern);
      return NULL;
    }

  return pattern;
}

static void
free_color_spec (gpointer spec,
                 gpointer user_data)
{
  meta_color_spec_free (spec);
}

MetaGradientSpec *
meta_gradient_spec_new (MetaGradientType type)
{
  MetaGradientSpec *spec;

  spec = g_new (MetaGradientSpec, 1);

  spec->type = type;
  spec->color_specs = NULL;

  return spec;
}

void
meta_gradient_spec_free (MetaGradientSpec *spec)
{
  g_return_if_fail (spec != NULL);

  g_slist_foreach (spec->color_specs, free_color_spec, NULL);
  g_slist_free (spec->color_specs);

  g_free (spec);
}

void
meta_gradient_spec_add_color_spec (MetaGradientSpec *spec,
                                   MetaColorSpec    *color_spec)
{
  spec->color_specs = g_slist_append (spec->color_specs, color_spec);
}

void
meta_gradient_spec_render (const MetaGradientSpec      *spec,
                           const MetaAlphaGradientSpec *alpha_spec,
                           cairo_t                     *cr,
                           GtkStyleContext             *context,
                           gdouble                      x,
                           gdouble                      y,
                           gdouble                      width,
                           gdouble                      height)
{
  cairo_pattern_t *pattern;

  pattern = create_cairo_pattern_from_gradient_spec (spec, alpha_spec, context);
  if (pattern == NULL)
    return;

  cairo_save (cr);

  cairo_rectangle (cr, x, y, width, height);

  cairo_translate (cr, x, y);
  cairo_scale (cr, width, height);

  cairo_set_source (cr, pattern);
  cairo_fill (cr);
  cairo_pattern_destroy (pattern);

  cairo_restore (cr);
}

gboolean
meta_gradient_spec_validate (MetaGradientSpec  *spec,
                             GError           **error)
{
  g_return_val_if_fail (spec != NULL, FALSE);

  if (g_slist_length (spec->color_specs) < 2)
    {
      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
                   _("Gradients should have at least two colors"));

      return FALSE;
    }

  return TRUE;
}

MetaAlphaGradientSpec *
meta_alpha_gradient_spec_new (MetaGradientType type,
                              gint             n_alphas)
{
  MetaAlphaGradientSpec *spec;

  g_return_val_if_fail (n_alphas > 0, NULL);

  spec = g_new0 (MetaAlphaGradientSpec, 1);

  spec->type = type;
  spec->alphas = g_new0 (guchar, n_alphas);
  spec->n_alphas = n_alphas;

  return spec;
}

void
meta_alpha_gradient_spec_free (MetaAlphaGradientSpec *spec)
{
  g_return_if_fail (spec != NULL);

  g_free (spec->alphas);
  g_free (spec);
}

void
meta_alpha_gradient_spec_add_alpha (MetaAlphaGradientSpec *spec,
                                    gint                   n_alpha,
                                    gdouble                alpha)
{
  spec->alphas[n_alpha] = (guchar) (alpha * 255);
}

guchar
meta_alpha_gradient_spec_get_alpha (MetaAlphaGradientSpec *spec,
                                    gint                   n_alpha)
{
  return spec->alphas[n_alpha];
}

void
meta_alpha_gradient_spec_render (MetaAlphaGradientSpec *spec,
                                 GdkRGBA                color,
                                 cairo_t               *cr,
                                 gdouble                x,
                                 gdouble                y,
                                 gdouble                width,
                                 gdouble                height)
{
  if (!spec || spec->n_alphas == 1)
    {
      if (spec)
        color.alpha = spec->alphas[0] / 255.0;

      gdk_cairo_set_source_rgba (cr, &color);
      cairo_rectangle (cr, x, y, width, height);
      cairo_fill (cr);
    }
  else
    {
      cairo_pattern_t *pattern;
      gint n_alphas;
      gint i;

      /* Hardcoded in meta-theme-metacity.c */
      g_assert (spec->type == META_GRADIENT_HORIZONTAL);

      pattern = cairo_pattern_create_linear (0, 0, 1, 0);
      n_alphas = spec->n_alphas;

      for (i = 0; i < n_alphas; i++)
        cairo_pattern_add_color_stop_rgba (pattern, i / (gfloat) (n_alphas - 1),
                                           color.red, color.green, color.blue,
                                           spec->alphas[i] / 255.0);

      if (cairo_pattern_status (pattern) != CAIRO_STATUS_SUCCESS)
        {
          cairo_pattern_destroy (pattern);
          return;
        }

      cairo_save (cr);
      cairo_rectangle (cr, x, y, width, height);

      cairo_translate (cr, x, y);
      cairo_scale (cr, width, height);

      cairo_set_source (cr, pattern);
      cairo_fill (cr);

      cairo_pattern_destroy (pattern);
      cairo_restore (cr);
    }
}

cairo_pattern_t *
meta_alpha_gradient_spec_get_mask (const MetaAlphaGradientSpec *spec)
{
  gint n_alphas;
  cairo_pattern_t *pattern;
  gint i;

  /* Hardcoded in meta-theme-metacity.c */
  g_assert (spec->type == META_GRADIENT_HORIZONTAL);

  n_alphas = spec->n_alphas;
  if (n_alphas == 0)
    return NULL;

  if (n_alphas == 1)
    return cairo_pattern_create_rgba (0, 0, 0, spec->alphas[0] / 255.0);

  pattern = cairo_pattern_create_linear (0, 0, 1, 0);

  for (i = 0; i < n_alphas; i++)
    cairo_pattern_add_color_stop_rgba (pattern, i / (gfloat) (n_alphas - 1),
                                       0, 0, 0, spec->alphas[i] / 255.0);

  if (cairo_pattern_status (pattern) != CAIRO_STATUS_SUCCESS)
    {
      cairo_pattern_destroy (pattern);
      return NULL;
    }

  return pattern;
}