summaryrefslogtreecommitdiff
path: root/gdata/gdata-types.c
blob: ba7fd14394bb133f0dd9e24348163af695f36d6f (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * GData Client
 * Copyright (C) Philip Withnall 2008-2009 <philip@tecnocode.co.uk>
 *
 * GData Client 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.
 *
 * GData Client 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GData Client.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION:gdata-types
 * @short_description: miscellaneous data types
 * @stability: Stable
 * @include: gdata/gdata-types.h
 *
 * The structures here are used haphazardly across the library, describing
 * various small data types.
 **/

#include <glib.h>
#include <glib-object.h>
#include <string.h>
#include <stdlib.h>
#include <libsoup/soup.h>

#include "gdata-types.h"

static gpointer
gdata_color_copy (gpointer color)
{
	return g_memdup (color, sizeof (GDataColor));
}

GType
gdata_color_get_type (void)
{
	static GType type_id = 0;

	if (type_id == 0) {
		type_id = g_boxed_type_register_static (g_intern_static_string ("GDataColor"),
		                                        (GBoxedCopyFunc) gdata_color_copy,
		                                        (GBoxedFreeFunc) g_free);
	}

	return type_id;
}

/**
 * gdata_color_from_hexadecimal:
 * @hexadecimal: a hexadecimal color string
 * @color: (out caller-allocates): a #GDataColor
 *
 * Parses @hexadecimal and returns a #GDataColor describing it in @color.
 *
 * @hexadecimal should be in the form <literal>#<replaceable>rr</replaceable><replaceable>gg</replaceable><replaceable>bb</replaceable></literal>,
 * where <replaceable>rr</replaceable> is a two-digit hexadecimal red intensity value, <replaceable>gg</replaceable> is green
 * and <replaceable>bb</replaceable> is blue. The hash is optional.
 *
 * Return value: %TRUE on success, %FALSE otherwise
 **/
gboolean
gdata_color_from_hexadecimal (const gchar *hexadecimal, GDataColor *color)
{
	gint temp;

	g_return_val_if_fail (hexadecimal != NULL, FALSE);
	g_return_val_if_fail (color != NULL, FALSE);

	if (*hexadecimal == '#')
		hexadecimal++;
	if (strlen (hexadecimal) != 6)
		return FALSE;

	/* Red */
	temp = g_ascii_xdigit_value (*(hexadecimal++)) * 16;
	if (temp < 0)
		return FALSE;
	color->red = temp;
	temp = g_ascii_xdigit_value (*(hexadecimal++));
	if (temp < 0)
		return FALSE;
	color->red += temp;

	/* Green */
	temp = g_ascii_xdigit_value (*(hexadecimal++)) * 16;
	if (temp < 0)
		return FALSE;
	color->green = temp;
	temp = g_ascii_xdigit_value (*(hexadecimal++));
	if (temp < 0)
		return FALSE;
	color->green += temp;

	/* Blue */
	temp = g_ascii_xdigit_value (*(hexadecimal++)) * 16;
	if (temp < 0)
		return FALSE;
	color->blue = temp;
	temp = g_ascii_xdigit_value (*(hexadecimal++));
	if (temp < 0)
		return FALSE;
	color->blue += temp;

	return TRUE;
}

/**
 * gdata_color_to_hexadecimal:
 * @color: a #GDataColor
 *
 * Returns a string describing @color in hexadecimal form; in the form <literal>#<replaceable>rr</replaceable><replaceable>gg</replaceable>
 * <replaceable>bb</replaceable></literal>, where <replaceable>rr</replaceable> is a two-digit hexadecimal red intensity value,
 * <replaceable>gg</replaceable> is green and <replaceable>bb</replaceable> is blue. The hash is always present.
 *
 * Return value: the color string; free with g_free()
 *
 * Since: 0.3.0
 **/
gchar *
gdata_color_to_hexadecimal (const GDataColor *color)
{
	g_return_val_if_fail (color != NULL, NULL);
	return g_strdup_printf ("#%02x%02x%02x", color->red, color->green, color->blue);
}