summaryrefslogtreecommitdiffstats
path: root/src/st/st-icon-colors.c
blob: c6a082add37fb24cc4c42d83910342893ea66599 (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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
 * st-icon-colors.c: Colors for colorizing a symbolic icon
 *
 * Copyright 2010 Red Hat, Inc.
 * Copyright 2010 Florian Müllner
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "st-icon-colors.h"

/**
 * st_icon_colors_new:
 *
 * Creates a new #StIconColors. All colors are initialized to transparent black.
 *
 * Returns: a newly created #StIconColors. Free with st_icon_colors_unref()
 */
StIconColors *
st_icon_colors_new (void)
{
  StIconColors *colors;

  colors = g_new0 (StIconColors, 1);
  colors->ref_count = 1;

  return colors;
}

/**
 * st_icon_colors_ref:
 * @colors: a #StIconColors
 *
 * Atomically increments the reference count of @colors by one.
 *
 * Returns: the passed in #StIconColors.
 */
StIconColors *
st_icon_colors_ref (StIconColors *colors)
{
  g_return_val_if_fail (colors != NULL, NULL);
  g_return_val_if_fail (colors->ref_count > 0, colors);

  g_atomic_int_inc ((volatile int *)&colors->ref_count);
  return colors;
}

/**
 * st_icon_colors_unref:
 * @colors: a #StIconColors
 *
 * Atomically decrements the reference count of @colors by one.
 * If the reference count drops to 0, all memory allocated by the
 * #StIconColors is released.
 */
void
st_icon_colors_unref (StIconColors *colors)
{
  g_return_if_fail (colors != NULL);
  g_return_if_fail (colors->ref_count > 0);

  if (g_atomic_int_dec_and_test ((volatile int *)&colors->ref_count))
    g_free (colors);
}

/**
 * st_icon_colors_copy:
 * @colors: a #StIconColors
 *
 * Creates a new StIconColors structure that is a copy of the passed
 * in @colors. You would use this function instead of st_icon_colors_ref()
 * if you were planning to change colors in the result.
 *
 * Returns: a newly created #StIconColors.
 */
StIconColors *
st_icon_colors_copy (StIconColors *colors)
{
  StIconColors *copy;

  g_return_val_if_fail (colors != NULL, NULL);

  copy = st_icon_colors_new ();

  copy->foreground = colors->foreground;
  copy->warning = colors->warning;
  copy->error = colors->error;
  copy->success = colors->success;

  return copy;
}

/**
 * st_icon_colors_equal:
 * @colors: a #StIconColors
 * @other: another #StIconColors
 *
 * Check if two #StIconColors objects are identical.
 *
 * Returns: %TRUE if the #StIconColors are equal
 */
gboolean
st_icon_colors_equal (StIconColors *colors,
                      StIconColors *other)
{
  if (colors == other)
    return TRUE;

  if (colors == NULL || other == NULL)
    return FALSE;

  return clutter_color_equal (&colors->foreground, &other->foreground) &&
         clutter_color_equal (&colors->warning, &other->warning) &&
         clutter_color_equal (&colors->error, &other->error) &&
         clutter_color_equal (&colors->success, &other->success);
}

G_DEFINE_BOXED_TYPE (StIconColors,
                     st_icon_colors,
                     st_icon_colors_ref,
                     st_icon_colors_unref)