Blame gdata/gdata-types.c

Packit 4b6dd7
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
Packit 4b6dd7
/*
Packit 4b6dd7
 * GData Client
Packit 4b6dd7
 * Copyright (C) Philip Withnall 2008-2009 <philip@tecnocode.co.uk>
Packit 4b6dd7
 *
Packit 4b6dd7
 * GData Client is free software; you can redistribute it and/or
Packit 4b6dd7
 * modify it under the terms of the GNU Lesser General Public
Packit 4b6dd7
 * License as published by the Free Software Foundation; either
Packit 4b6dd7
 * version 2.1 of the License, or (at your option) any later version.
Packit 4b6dd7
 *
Packit 4b6dd7
 * GData Client is distributed in the hope that it will be useful,
Packit 4b6dd7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4b6dd7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 4b6dd7
 * Lesser General Public License for more details.
Packit 4b6dd7
 *
Packit 4b6dd7
 * You should have received a copy of the GNU Lesser General Public
Packit 4b6dd7
 * License along with GData Client.  If not, see <http://www.gnu.org/licenses/>.
Packit 4b6dd7
 */
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * SECTION:gdata-types
Packit 4b6dd7
 * @short_description: miscellaneous data types
Packit 4b6dd7
 * @stability: Stable
Packit 4b6dd7
 * @include: gdata/gdata-types.h
Packit 4b6dd7
 *
Packit 4b6dd7
 * The structures here are used haphazardly across the library, describing
Packit 4b6dd7
 * various small data types.
Packit 4b6dd7
 */
Packit 4b6dd7
Packit 4b6dd7
#include <glib.h>
Packit 4b6dd7
#include <glib-object.h>
Packit 4b6dd7
#include <string.h>
Packit 4b6dd7
#include <stdlib.h>
Packit 4b6dd7
#include <libsoup/soup.h>
Packit 4b6dd7
Packit 4b6dd7
#include "gdata-types.h"
Packit 4b6dd7
Packit 4b6dd7
static gpointer
Packit 4b6dd7
gdata_color_copy (gpointer color)
Packit 4b6dd7
{
Packit 4b6dd7
	return g_memdup (color, sizeof (GDataColor));
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
GType
Packit 4b6dd7
gdata_color_get_type (void)
Packit 4b6dd7
{
Packit 4b6dd7
	static GType type_id = 0;
Packit 4b6dd7
Packit 4b6dd7
	if (type_id == 0) {
Packit 4b6dd7
		type_id = g_boxed_type_register_static (g_intern_static_string ("GDataColor"),
Packit 4b6dd7
		                                        (GBoxedCopyFunc) gdata_color_copy,
Packit 4b6dd7
		                                        (GBoxedFreeFunc) g_free);
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	return type_id;
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * gdata_color_from_hexadecimal:
Packit 4b6dd7
 * @hexadecimal: a hexadecimal color string
Packit 4b6dd7
 * @color: (out caller-allocates): a #GDataColor
Packit 4b6dd7
 *
Packit 4b6dd7
 * Parses @hexadecimal and returns a #GDataColor describing it in @color.
Packit 4b6dd7
 *
Packit 4b6dd7
 * @hexadecimal should be in the form <literal>#<replaceable>rr</replaceable><replaceable>gg</replaceable><replaceable>bb</replaceable></literal>,
Packit 4b6dd7
 * where <replaceable>rr</replaceable> is a two-digit hexadecimal red intensity value, <replaceable>gg</replaceable> is green
Packit 4b6dd7
 * and <replaceable>bb</replaceable> is blue. The hash is optional.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Return value: %TRUE on success, %FALSE otherwise
Packit 4b6dd7
 */
Packit 4b6dd7
gboolean
Packit 4b6dd7
gdata_color_from_hexadecimal (const gchar *hexadecimal, GDataColor *color)
Packit 4b6dd7
{
Packit 4b6dd7
	gint temp;
Packit 4b6dd7
Packit 4b6dd7
	g_return_val_if_fail (hexadecimal != NULL, FALSE);
Packit 4b6dd7
	g_return_val_if_fail (color != NULL, FALSE);
Packit 4b6dd7
Packit 4b6dd7
	if (*hexadecimal == '#')
Packit 4b6dd7
		hexadecimal++;
Packit 4b6dd7
	if (strlen (hexadecimal) != 6)
Packit 4b6dd7
		return FALSE;
Packit 4b6dd7
Packit 4b6dd7
	/* Red */
Packit 4b6dd7
	temp = g_ascii_xdigit_value (*(hexadecimal++)) * 16;
Packit 4b6dd7
	if (temp < 0)
Packit 4b6dd7
		return FALSE;
Packit 4b6dd7
	color->red = temp;
Packit 4b6dd7
	temp = g_ascii_xdigit_value (*(hexadecimal++));
Packit 4b6dd7
	if (temp < 0)
Packit 4b6dd7
		return FALSE;
Packit 4b6dd7
	color->red += temp;
Packit 4b6dd7
Packit 4b6dd7
	/* Green */
Packit 4b6dd7
	temp = g_ascii_xdigit_value (*(hexadecimal++)) * 16;
Packit 4b6dd7
	if (temp < 0)
Packit 4b6dd7
		return FALSE;
Packit 4b6dd7
	color->green = temp;
Packit 4b6dd7
	temp = g_ascii_xdigit_value (*(hexadecimal++));
Packit 4b6dd7
	if (temp < 0)
Packit 4b6dd7
		return FALSE;
Packit 4b6dd7
	color->green += temp;
Packit 4b6dd7
Packit 4b6dd7
	/* Blue */
Packit 4b6dd7
	temp = g_ascii_xdigit_value (*(hexadecimal++)) * 16;
Packit 4b6dd7
	if (temp < 0)
Packit 4b6dd7
		return FALSE;
Packit 4b6dd7
	color->blue = temp;
Packit 4b6dd7
	temp = g_ascii_xdigit_value (*(hexadecimal++));
Packit 4b6dd7
	if (temp < 0)
Packit 4b6dd7
		return FALSE;
Packit 4b6dd7
	color->blue += temp;
Packit 4b6dd7
Packit 4b6dd7
	return TRUE;
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * gdata_color_to_hexadecimal:
Packit 4b6dd7
 * @color: a #GDataColor
Packit 4b6dd7
 *
Packit 4b6dd7
 * Returns a string describing @color in hexadecimal form; in the form <literal>#<replaceable>rr</replaceable><replaceable>gg</replaceable>
Packit 4b6dd7
 * <replaceable>bb</replaceable></literal>, where <replaceable>rr</replaceable> is a two-digit hexadecimal red intensity value,
Packit 4b6dd7
 * <replaceable>gg</replaceable> is green and <replaceable>bb</replaceable> is blue. The hash is always present.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Return value: the color string; free with g_free()
Packit 4b6dd7
 *
Packit 4b6dd7
 * Since: 0.3.0
Packit 4b6dd7
 */
Packit 4b6dd7
gchar *
Packit 4b6dd7
gdata_color_to_hexadecimal (const GDataColor *color)
Packit 4b6dd7
{
Packit 4b6dd7
	g_return_val_if_fail (color != NULL, NULL);
Packit 4b6dd7
	return g_strdup_printf ("#%02x%02x%02x", color->red, color->green, color->blue);
Packit 4b6dd7
}