summaryrefslogtreecommitdiff
path: root/src/egg-color.c
blob: 2dcbca852fd8cf0916aade52dfdfb8155315c802 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <glib.h>
#include "egg-color.h"

/**
 * egg_color_from_rgb:
 * @red: The red value
 * @green: The green value
 * @blue: The blue value
 **/
guint32
egg_color_from_rgb (guint8 red, guint8 green, guint8 blue)
{
	guint32 color = 0;
	color += (guint32) red * 0x10000;
	color += (guint32) green * 0x100;
	color += (guint32) blue;
	return color;
}

/**
 * egg_color_to_rgb:
 * @red: The red value
 * @green: The green value
 * @blue: The blue value
 **/
void
egg_color_to_rgb (guint32 color, guint8 *red, guint8 *green, guint8 *blue)
{
	*red = (color & 0xff0000) / 0x10000;
	*green = (color & 0x00ff00) / 0x100;
	*blue = color & 0x0000ff;
}

/***************************************************************************
 ***                          MAKE CHECK TESTS                           ***
 ***************************************************************************/
#ifdef EGG_TEST
#include "egg-test.h"

void
egg_color_test (gpointer data)
{
	guint8 r, g, b;
	guint32 color;
	EggTest *test = (EggTest *) data;

	if (egg_test_start (test, "EggColor") == FALSE) {
		return;
	}

	/************************************************************/
	egg_test_title (test, "get red");
	egg_color_to_rgb (0xff0000, &r, &g, &b);
	if (r == 255 && g == 0 && b == 0) {
		egg_test_success (test, "got red");
	} else {
		egg_test_failed (test, "could not get red (%i, %i, %i)", r, g, b);
	}

	/************************************************************/
	egg_test_title (test, "get green");
	egg_color_to_rgb (0x00ff00, &r, &g, &b);
	if (r == 0 && g == 255 && b == 0) {
		egg_test_success (test, "got green");
	} else {
		egg_test_failed (test, "could not get green (%i, %i, %i)", r, g, b);
	}

	/************************************************************/
	egg_test_title (test, "get blue");
	egg_color_to_rgb (0x0000ff, &r, &g, &b);
	if (r == 0 && g == 0 && b == 255) {
		egg_test_success (test, "got blue");
	} else {
		egg_test_failed (test, "could not get blue (%i, %i, %i)", r, g, b);
	}

	/************************************************************/
	egg_test_title (test, "get black");
	egg_color_to_rgb (0x000000, &r, &g, &b);
	if (r == 0 && g == 0 && b == 0) {
		egg_test_success (test, "got black");
	} else {
		egg_test_failed (test, "could not get black (%i, %i, %i)", r, g, b);
	}

	/************************************************************/
	egg_test_title (test, "get white");
	egg_color_to_rgb (0xffffff, &r, &g, &b);
	if (r == 255 && g == 255 && b == 255) {
		egg_test_success (test, "got white");
	} else {
		egg_test_failed (test, "could not get white (%i, %i, %i)", r, g, b);
	}

	/************************************************************/
	egg_test_title (test, "set red");
	color = egg_color_from_rgb (0xff, 0x00, 0x00);
	if (color == 0xff0000) {
		egg_test_success (test, "set red");
	} else {
		egg_test_failed (test, "could not set red (%i)", color);
	}

	/************************************************************/
	egg_test_title (test, "set green");
	color = egg_color_from_rgb (0x00, 0xff, 0x00);
	if (color == 0x00ff00) {
		egg_test_success (test, "set green");
	} else {
		egg_test_failed (test, "could not set green (%i)", color);
	}

	/************************************************************/
	egg_test_title (test, "set blue");
	color = egg_color_from_rgb (0x00, 0x00, 0xff);
	if (color == 0x0000ff) {
		egg_test_success (test, "set blue");
	} else {
		egg_test_failed (test, "could not set blue (%i)", color);
	}

	/************************************************************/
	egg_test_title (test, "set white");
	color = egg_color_from_rgb (0xff, 0xff, 0xff);
	if (color == 0xffffff) {
		egg_test_success (test, "set white");
	} else {
		egg_test_failed (test, "could not set white (%i)", color);
	}

	egg_test_end (test);
}

#endif